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
https://forums.phpfreaks.com/topic/89512-solved-sessions-question/
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>";
}
?>

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"));

}

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.