I try to access session variables stored on a different page. When I have cookies enabled it works, but the session variables are gone when I disable cookies.
Here’s an example:
<?php
//page1.php
session_start();
$_SESSION['startTime'] = time();
echo("<a href=\"page2.php?" . session_name() . "=" . session_id() . "\">Page2</a><br />\n");
echo("Session-ID: " . session_id() . "<br />");
echo("Session-Content: " . print_r($_SESSION));
// output:
// <a href="page2.php?PHPSESSID=ou1n4mrotvqc6dod41q8t0a432">Page2</a><br />
// Session-ID: ou1n4mrotvqc6dod41q8t0a432<br />Array
// (
// [startTime] => 1339177944
// )
// Session-Content: 1
?>
<?php
//page2.php
session_start();
session_id($_GET[session_name()]);
echo("Session-ID: " . session_id() . "<br />\n");
echo("Session-Content: " . print_r($_SESSION));
session_destroy();
// output
// Session-ID: ou1n4mrotvqc6dod41q8t0a432<br />
// Array
// (
// )
// Session-Content: 1
?>
Am I doing something wrong? How can I use session variables whith cookies disabled?
You will need to change the php.ini setting
session.use_only_cookiesto0to allow the session id to be passed in the querystring. You may also want to use the magic constantSIDinstead ofsession_name() . "=" . session_id().