Jump to content

[SOLVED] sessions question


affordit

Recommended Posts

Hello all

This is where I am setting the session variables

$query = "Select username, end_date, valid from  login Where username = '$username' and psword = '$psword' AND valid='1'";
$results = mysql_query($query) or die(mysql_error()."<br /><br />".$query);
$num=mysql_numrows($results);
if ($num >0) {

$username = mysql_fetch_array($results);
$_SESSION['username'] = $username['username'];

$end_date = mysql_fetch_array($results);
$_SESSION['end_date'] = $end_date['end_date'];
(header("location: header.php"));

} else {
echo "<table align='center' width='300'><tr><td align='center'><b>The Username and Password you entered do not match any of our records or your Email address has not been validated. Try logging in <a href='first_login.php'>HERE</a> to validate your Email before you contact us. Thank You</b></td></tr></table>";
}
?>

 

And this is where I am tring to retrieve them

<?php
session_start();
?>
<LINK REL=STYLESHEET TYPE="text/css" HREF="main1.css">
<LINK REL=STYLESHEET TYPE="text/css" HREF="formstyle.css">
<table align="center" width="99%" border="0" bgcolor="#FFFFCC">
<tr>
<td colspan="3"><h2><center>Affordable Everything - Klamath Falls</center></h2></td></tr>
<tr>
<td align="left"><font color="#FF6600"><B>
<?php

if( isset($username) ){
echo "Welcome: ". $_SESSION['username'];
echo "         
Your registration will expire: ". date('m-d-Y',strtotime($_SESSION['end_date']));
echo "</td>";

echo "<td align='right'><small><font color='#FF6600'><B>";
echo "<a href='login.php'>Renew your registration now</a>";
echo"</td>";
echo "<td align='right'><small><B>";
echo "<a href='logout.php'>Logout</a>  ";
echo"</td>";
echo"</tr>";
echo"</table>";
}
else
{
echo "<td align='left' valign='bottom'><font color='#FF6600'><B>";
echo "  Welcome: Guest";
echo "         Today is:  ". date('m-d-Y');
echo "         <a href='add_advertiser.php'>Get registered now</a></td>";
echo "<form action='auth.php' method=post>";
echo "<td align='right' valign='bottom'><B>
Username  <input type=text name='username' class='style1'>   Password   <input type=password name='psword' class='style1'>   <input type='image' name='submit' src='cbb.gif' border='0' />   </form>";
echo "</td></table>";
}

?>

 

The problem is it does not retrieve what is in the DB it shows todays date im lost.

Anyone see whats wrong?

Link to comment
Share on other sites

Yes here is the whole page

 

<?php 
// this starts the session 
session_start();
include("sharons_dbinfo.inc.php");
mysql_connect(mysql,$name,$password);
mysql_select_db($database) or die( "Unable to select database"); 

$query = "Select username, end_date, valid from  login Where username = '$username' and psword = '$psword' AND valid='1'";
$results = mysql_query($query) or die(mysql_error()."<br /><br />".$query);
$num=mysql_numrows($results);
if ($num >0) {

$username = mysql_fetch_array($results);
$_SESSION['username'] = $username['username'];

$end_date = mysql_fetch_array($results);
$_SESSION['end_date'] = $end_date['end_date'];
(header("location: header.php"));

} else {
echo "<table align='center' width='300'><tr><td align='center'><b>The Username and Password you entered do not match any of our records or your Email address has not been validated. Try logging in <a href='first_login.php'>HERE</a> to validate your Email before you contact us. Thank You</b></td></tr></table>";
}
?>

Link to comment
Share on other sites

My initial thought is that your $_SESSION variable isn't necessarily wrong, but that other things could be...

 

Just a quick note:  In your $query, you're selecting both "username" and "valid" where username = $username and valid = 1...you can get rid of selecting "username" and "valid", 'cause you already have values for them ($username and 1).  So...just select end_date WHERE username = '$username' AND psword = '$psword' AND valid = '1'";  Also...if valid is going to be a numeric (I'm GUESSING it would be a boolean...a 1 or a 0 for TRUE / FALSE...) change the datatype to TINYINT and get rid of the quotes around valid so that it's ...valid=1";

 

Now onto the problem...

 

//Original block of code
if ($num >0) {

$username = mysql_fetch_array($results); //fetches result row
$_SESSION['username'] = $username['username'];

$end_date = mysql_fetch_array($results); //fetches 'next' row...non existent
$_SESSION['end_date'] = $end_date['end_date'];
(header("location: header.php"));

}
//New block
if($num > 0) {

       $result = mysql_fetch_array($results);  //you should only pull the result once, otherwise it'll attempt to fetch the next row
       $_SESSION['username'] = $username; //Remember we're not fetching the username result anymore
       $_SESSION['end_date'] = $result['end_date'];
       (header("location: header.php"));

}

Link to comment
Share on other sites

The problem looks like it's here:

 

$query = "Select username, end_date, valid from  login Where username = '$username' and psword = '$psword' AND valid='1'";

 

You are setting username and psword equal to $username and $psword, but you have not defined $username and $psword anywhere. (I'm guessing those were supposed to be $_POST['username'] and $_POST['psword'].)

 

As a result, the result of your query is empty, meaning that empty values are submitted to your session variables.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.