Forum Moderators: coopster

Message Too Old, No Replies

Session is Not Staying Open.

Session is Not Staying Open...

         

jreeder

4:47 pm on Jun 20, 2008 (gmt 0)

10+ Year Member



I have a problem that I just can't seem to solve... I have a PHP script that uses sessions to allow an authenticated visitor to access various pages.

The sessions work EXCEPT on a page where I am running a WHILE statement. When I try to click on another page that requires the session, it thinks the session has ended. There is nothing in the code to end the session (except maybe the while statement).

Does a WHILE statement cause a session to close? The WHILE statement looks like this:

while(list($fname, $lname, $username, $password, $sequence) = mysql_fetch_array($result))
{
echo "stuff here...";
}

Your help is GREATLY appreciated!
Jack

cameraman

7:11 pm on Jun 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Something else is going on; it's not the while unless you're explicitly destroying the session within it or the loop takes longer to execute than the session's lifetime.
Is session.autostart on? If not, are you calling session_start() on every script that needs it? If you're using a different session name, are you remembering to call session_name()?

jreeder

8:24 pm on Jun 20, 2008 (gmt 0)

10+ Year Member



Here is a more compelete version of the script. I am calling session_start(); on each page. When I remove the WHILE statement it works!

It's driving me crazy!

This page displays fine and the mySQL querys work perfectly. But when you click on any link on the page, the next page indicates no session found/cannot log in.

Thanks a MILLION! :-)

<?php
session_start();

$dbhost = '#*$!#*$!#*$!';
$dbuser = 'yyyyyyyyyy';
$dbpass = 'zzzzzzzzz';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ("Cannot connect to server. <P>");
$dbname = 'aaaaaaaaaaaaa';
mysql_select_db($dbname) or die ("Cannot connect to database. <P>");

echo "
<a href=index.php>Home</a> ¦
<a href=link_1.php>link_1</a> ¦
<a href=link_2.php>link_2</a> ¦
<a href=link_3.php>link_3</a> ¦
<a href=logout.php>logout</a>
<br><br>";

// convert username and password from _POST or _SESSION
if($_POST){
$_SESSION['username']= mysql_real_escape_string($_POST["username"]);
$_SESSION['password']= mysql_real_escape_string($_POST["password"]);
}

// query for a user/pass match
$result=mysql_query("SELECT * FROM user
WHERE username='" . $_SESSION['username'] . "'
AND password='" . $_SESSION['password'] . "'");

// retrieve number of rows resulted
$num=mysql_num_rows($result);

// print login form and exit if failed.
if($num < 1){
echo "You are not authenticated. Please login.<br><br>

<form method=POST action=index.php>
username: <input type=text name=\"username\">
password: <input type=password name=\"password\">
<input type=submit>
</form>";

exit;
}

echo "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'>
<html>
<head>
<title>View Users</title>
<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>
</head>

<body>
<font color='#4162FE' size='4' face='Arial, Helvetica, sans-serif'><b>VIEW and EDIT USERS</b></font><br>
<br>
<font size='2' face='Arial, Helvetica, sans-serif'> <br>
<a href='userview.php'>To View Users</a>
";

$rowsPerPage = 12; // rows to show per page
$pageNum = 1; // by default we show first page
if(isset($_GET['page'])) // if $_GET['page'] defined, use it as page number
{
$pageNum = $_GET['page'];
}

$offset = ($pageNum - 1) * $rowsPerPage; // count the offset

$query = "SELECT * FROM user LIMIT $offset, $rowsPerPage";
$result = mysql_query($query) or die('Error, could not run query (Q1).');

// print header...
echo "<table border='0' cellspacing='1' cellpadding='2'><tr>
<td bgcolor='#C4D3FF'><b><font size='2' face='Arial, Helvetica, sans-serif'>Seq #</font</b></td>
<td bgcolor='#C4D3FF'><b><font size='2' face='Arial, Helvetica, sans-serif'>First Name</font</b></td>
<td bgcolor='#C4D3FF'><b><font size='2' face='Arial, Helvetica, sans-serif'>Last Name</font></b></td>
<td bgcolor='#C4D3FF'><b><font size='2' face='Arial, Helvetica, sans-serif'>Username</font></b></td>
<td width='100px' bgcolor='#C4D3FF'><b><font size='2' face='Arial, Helvetica, sans-serif'>Password</font></b></td>
<td width='100px' bgcolor='#C4D3FF'><b><font size='2' face='Arial, Helvetica, sans-serif'>Phone</font></b></td>
<td width='100px' bgcolor='#C4D3FF'><b><font size='2' face='Arial, Helvetica, sans-serif'>Email</font></b></td>
<td width='100px' bgcolor='#C4D3FF'><b><font size='2' face='Arial, Helvetica, sans-serif'>Date Open</font></b></td>
</tr>";

// WHEN I DELETE THE FOLLOWING WHILE STATEMENT, THE SESSION STAYS OPEN...
// print rows
while(list($fname,$lname,$username,$password,$sequence,$phone,$email,$dateopen) = mysql_fetch_array($result))
{
echo "<tr>
<td bgcolor='#C4D3FF'><font size='2' face='Arial, Helvetica, sans-serif'>$sequence</font></td>
<td bgcolor='#C4D3FF'><font size='2' face='Arial, Helvetica, sans-serif'>$fname</font></td>
<td bgcolor='#C4D3FF'><font size='2' face='Arial, Helvetica, sans-serif'>$lname</font></td>
<td bgcolor='#C4D3FF'><font size='2' face='Arial, Helvetica, sans-serif'>$username</font></td>
<td bgcolor='#C4D3FF'><font size='2' face='Arial, Helvetica, sans-serif'>$password</font></td>
<td bgcolor='#C4D3FF'><font size='2' face='Arial, Helvetica, sans-serif'>$phone</font></td>
<td bgcolor='#C4D3FF'><font size='2' face='Arial, Helvetica, sans-serif'>$email</font></td>
<td bgcolor='#C4D3FF'><font size='2' face='Arial, Helvetica, sans-serif'>$dateopen</font></td>
<td bgcolor='#C4D3FF'><font size='2' face='Arial, Helvetica, sans-serif'><a href='usereditLINK.php?username=$username'>Edit</a></font></td>
<td bgcolor='#C4D3FF'><font size='2' face='Arial, Helvetica, sans-serif'><a href='userdeleteLINK.php?username=$username'>Delete</a></font></td>
</tr>";
}

mysql_close();
?>

jreeder

9:08 pm on Jun 20, 2008 (gmt 0)

10+ Year Member



I fixed it!
Turns out I was using the username as the session id. When the while statement ran and listed the rows from the array, it changed the username with each row. When the routine was finished, the session id no longer validaed. Makes perfect sense (now)!

:-)

Thanks for your simple advice...it made me go back and look in the right place...not in a problem with the sessions themselves.

-Jack