Page 1 of 1

Sessions - Not Persisting

Posted: Tue Jan 16, 2007 2:28 pm
by maxd
I obviously don't understand sessions. Even though I thought I did.

I'm having trouble getting my authentication system to work, because the session is not persisting, so it continually defaults to the login, each time I hit a new page.

In order to test, I created a very simple 2 page session variable test.

Page 1:

Code: Select all

<?php session_start(); ?>

<?php
ini_set('error_reporting', 8191); 
	$_SESSION["sessiontracker"] = 1;
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
<head>
	<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
	<meta name="author" content="" />
	<meta name="keywords" content="" />
	<meta name="description" content="" />
	<meta name="robots" content="all" />

	<title>session test page 1</title>

</head>

<body>

<?php echo "Session= " . $_SESSION["sessiontracker"] . "<br /><br />"; ?>

<a href="sess_test2.php">Try to display Session Variable on next page</a><br /><br />

</body>
</html>
Page 2 (target page):

Code: Select all

<?php session_start(); ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" >
<head>
	<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
	<meta name="author" content="" />
	<meta name="keywords" content="" />
	<meta name="description" content="" />
	<meta name="robots" content="all" />

	<title>Session test</title>

</head>

<body>

<?php echo "Session= " . $_SESSION["sessiontracker"] . "<br /><br />"; ?>

<a href="sess_test.php">back to sess_test</a><br /><br />

</body>
</html>
Based on my (obviously limited) understanding of session variables, the target page should consist of the message:
Session=1

But it's just:
Session=

On the first page, it is appropriately echoing the session variable:
Session=1

What am I doing wrong here?

thanks,
max

Posted: Tue Jan 16, 2007 3:11 pm
by maxd
OK. Now I know something is wrong on the server side (I think :? ), because I picked up the code directly from the PHP.net example, and it doesn't work:

PAGE 1

Code: Select all

<?php
// page1.php

session_start();

echo 'Welcome to page #1';

$_SESSION['favcolor'] = 'green';
$_SESSION['animal']  = 'cat';
$_SESSION['time']    = time();

// Works if session cookie was accepted
echo '<br /><a href="page2.php">page 2</a>';

// Or maybe pass along the session id, if needed
echo '<br /><a href="page2.php?' . SID . '">page 2</a>';
?>
PAGE 2

Code: Select all

<?php
// page2.php

session_start();

echo 'Welcome to page #2<br />';

echo $_SESSION['favcolor']; // green
echo $_SESSION['animal'];  // cat
echo date('Y m d H:i:s', $_SESSION['time']);

// You may want to use SID here, like we did in page1.php
echo '<br /><a href="page1.php">page 1</a>';
?>
Not only does the session not pass from page1 to page2; even on page1, the . SID . is not appearing in the href.

This is on a Windows 2003/IIS installation. I do have root access to the server, but no idea what to modify to make this work.

help.

max

Posted: Tue Jan 16, 2007 3:31 pm
by feyd
Have you verified that your session settings are okay in phpinfo()? How about the path where sessions are saved, is it correct?

Posted: Tue Jan 16, 2007 3:35 pm
by maxd
I ran the same sample code from the PHP.net site on another site (shared server, same hosting company, also an IIS Windows box), and it worked fine.

So, for some reason the dedicated server (Windows NT DEDJ217 5.2 build 3790, IIS 6, PHP 5.2) is not allowing sessions.

Very annoying. Very opaque.

max

Posted: Tue Jan 16, 2007 3:38 pm
by maxd
I'm not sure what the session settings should be.

http://63.134.215.218/phpinfo.php

Have a look if you care to, and let me know if you see anything.

PS: Is it a bad idea to put this address up on the forum? :(

Posted: Tue Jan 16, 2007 3:40 pm
by feyd
Is there a directory C:\PHP\Sessions?

Posted: Tue Jan 16, 2007 3:51 pm
by maxd
feyd fixed it.

as usual.

Upon reviewing the Session info, as per his direction, then logging into the server remote desktop and finding the C:\PHP directory we found, sure enough, no Sessions folder.

Created a new directory, set permissions, and voila, fully functioning authentication system.

Only 8 hours later.

Is this something the hosting company should have configured when they installed PHP?

Anyway, thank you again, feyd. It may seem simple, but it was completely confounding me.

max

Posted: Tue Jan 16, 2007 3:59 pm
by feyd
If your host performs the maintenance and installation, they should have set it correctly.