[SOLVED] Session variables do not carry to next page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mottwsc
Forum Commoner
Posts: 55
Joined: Sun Dec 23, 2007 8:01 pm

[SOLVED] Session variables do not carry to next page

Post by mottwsc »

These two simple scripts are for a test of session variables. I can pass variables in a form, but not using $_SESSION, even though there is a session ID. Can anyone see what is wrong? Could this be an INI setting or configuration issue?

Code: Select all

sessionTest1.php >>>>
<?php
	session_start();
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Testing Sessions page 1</title>
</head>

<body>
<?php
	$PHPSESSID = session_id();
	$_SESSION['session_var'] = 'session testing';
	echo 	"This is a test of the sessions feature.
			<form action='sessionTest2.php' method='POST'>
			<input type='hidden' name='form_var' value='form testing'>
			<input type='hidden' name='PHPSESSID' value='$PHPSESSID'>
			<input type='submit' value='go to next page'>
			</form>";
?>
</body>
</html>

sessionTest2.php >>>>
<?php
	session_start();
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Testing Sessions page 2</title>
</head>

<body>
<?php
	echo	"PHPSESSID = {$_POST['PHPSESSID']}<br>\n";
	echo 	"session_var = {$_SESSION['session_var']}<br>\n";
	echo	"session_var (2) = ".$_SESSION['session_var']."<br>\n";
	echo 	"form_var = {$_POST['form_var']}<br>\n";
?>
</body>
</html>
Output of sessionTest2.php >>>>
PHPSESSID = qvhne96mabbj8re6thfmmh6ia4
session_var =
session_var (2) =
form_var = form testing

Thanks for any advice!
LiveFree
Forum Contributor
Posts: 258
Joined: Tue Dec 06, 2005 5:34 pm
Location: W-Town

Post by LiveFree »

Try putting session_write_close() at the end of your scripts
User avatar
crystal ship
Forum Commoner
Posts: 36
Joined: Wed Aug 29, 2007 5:45 am

Post by crystal ship »

Your code is generating the following results in sessionTest2.php which seems correct.

PHPSESSID = 023ec6f0cfc615ef34959e0e4bd4e8a7
session_var = session testing
session_var (2) = session testing
form_var = form testing
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Set your error reporting levels and display error directives to help you out. At the top of each script, put this little snippet:

Code: Select all

<?php
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 1);
?>
Then run them again and see if you are getting errors or not.
mottwsc
Forum Commoner
Posts: 55
Joined: Sun Dec 23, 2007 8:01 pm

error related to unknown session path; changed path

Post by mottwsc »

I tried using session_write_close(), but that did not make a difference. The problem seems to be an unknown path specified in the ini file. You can see from the error messages below from sessionTest1.php.

************
Warning: session_start() [function.session-start]: open(C:\DOCUME~1\MICHAE~1.DEL\LOCALS~1\Temp\php\session\sess_ns18p52l859pqdo660mhtbg274, O_RDWR) failed: No such file or directory (2) in C:\Inetpub\wwwroot\sessionTest1.php on line 4
This is a test of the sessions feature.

[button]

Warning: Unknown: open(C:\DOCUME~1\MICHAE~1.DEL\LOCALS~1\Temp\php\session\sess_ns18p52l859pqdo660mhtbg274, O_RDWR) failed: No such file or directory (2) in Unknown on line 0

Warning: Unknown: Failed to write session data (files). Please verify that the current setting of session.save_path is correct (C:\DOCUME~1\MICHAE~1.DEL\LOCALS~1\Temp\php\session) in Unknown on line 0

*************
I do not know why this path was specified on install. The path does not exist as far as I can tell. I had installed php in c:\php. So, I created a subdirectory c:\php\session - is this where the session directory should go given my installation directory? Then, I changed the INI file (session.save_path="C:\php\session").

Once that was changed, I tried to rerun the scripts, but I have the same original wrong path showing up in the error messages. Then, I created another script to destroy the session, cookie, etc. (see code below). However, after running this (and the cookie was deleted), I reran the original script in a new browser, but I'm still getting the errors associated with the original path. Help!?

Code: Select all

<?php 
ini_set('error_reporting', E_ALL); 
ini_set('display_errors', 1); 

// Initialize the session.
// If you are using session_name("something"), don't forget it now!
session_start();

// Unset all of the session variables.
$_SESSION = array();

// If it's desired to kill the session, also delete the session cookie.
// Note: This will destroy the session, and not just the session data!
if (isset($_COOKIE[session_name()])) {
	echo "cookie removed";
    setcookie(session_name(), '', time()-42000, '/');
}

// Finally, destroy the session.
session_destroy();
?>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Did you restart your server after changing your php.ini?
mottwsc
Forum Commoner
Posts: 55
Joined: Sun Dec 23, 2007 8:01 pm

[RESOLVED] session errors

Post by mottwsc »

After restarting the server, the session variables appear to be transferring fine based on the new INI file path.

Thanks to all and happy holidays!
Post Reply