Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

$_Session variable not passed to next page? 1

Status
Not open for further replies.

ronspree

Technical User
May 3, 2003
103
PH
What am I missing here?
Code:
<?php
//session1.php
session_start();
$_SESSION['webuser'] = 'Ronnie';
header("Location: session2.php");
?>

Code:
<?php
//session2.php
session_start();
echo "<br>Web user is:  " . $_SESSION['webuser']; //does not output "Ronnie"
?>
 
you've got a race condition.

fix it as follows

Code:
<?php
//session1.php
session_start();
$_SESSION['webuser'] = 'Ronnie';
[red]session_write_close();[/red]
header("Location: session2.php");
?>
 
Are you running PHP with IIS?

If so, it could be your use of "header('Location:..')" that's causing the problem. Some versions of IIS do not allow cookies to be sent in the same connection as a Location header. It's by design: Microsoft has a unique interpretation of the HTTP spec.

As a test, replace the header() invocation with the output of a <A> tag to link to the second script.



Want the best answers? Ask the best questions! TANSTAAFL!
 
Im using Apache. The addition of the "session_write_close()" did not help. I also tried taking out the headers, then manually browsed "session2.php". The $_SESSION['webuser'] is still not passed. Do i need to pass the session id to session2.php?
 
have you made sure that the web server process has read/write access to the directory specified for sessions in php.ini?
 
jpadie, i don't know what you mean exactly, but I have session.use_cookies = 1 in my php.ini. I can pass cookies but have to use the import_request_variables in session2.php.
 
create a file and put this inside

Code:
<?php
phpinfo();

you need to check in the sessions section.

key things to check for are
session.save_handler = files
session.save_path = some path

the path for session.save_path must exist and must be read/writable by the php process (the phpinfo will tell you under whose credentials the process is running).
 
jpadie! Its working now! The directory indicated on session.save_path did not exist. So is just created it! Now its working. Thank you very much.
 
Try this..

<?php
$login_user = "bob";
session_start();
$_SESSION["registered_user"] = $login_user;
?>

//// on the next page:

<?php
session_start();
$login_user = $_SESSION["registered_user"];

echo $login_user;
?>

/// the $login_user variable should output as "bob"
 
ipnpro,

that would not have worked before because my subdirectory indicated in "session.save_path" did not exist. However, now that it does exist, it will surely work. Thanks.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top