Session vars not lasting beyond a page
Moderator: General Moderators
Session vars not lasting beyond a page
Hi,
This issue may be related to my previous issue, regarding PHP creating a new session for a PHP script called via an IMG tag.
I have an admin script, called 'admin' (no PHP extension, Apache is configured to execute admin as a PHP script). When you first go to the page, the user is presented with a login page.
The login page has a 'session_start()' at the beginning. Looking at my cookies directory, when this page loads, PHP writes a cookie to my C:\Temp directory with a unique ID.
The script calls 'admin' again, which this time detects the $_POST variables sent by the login page, and accordingly writes a session variable indicating the user is logged on, with the intention of using this as a login test later on (the usual method, in other words).
However, expecting the session to be preserved between calls to the 'admin' script, PHP instead writes a new session. It correctly writes to this session, but as soon as you go off the page - for example by calling 'admin' directly again via the browser - it loses it, because it writes a whole new session again!
Why am I losing sessions between page loads - why aren't they being preserved across page calls, as you would expect? Config issue?
Many thanks
This issue may be related to my previous issue, regarding PHP creating a new session for a PHP script called via an IMG tag.
I have an admin script, called 'admin' (no PHP extension, Apache is configured to execute admin as a PHP script). When you first go to the page, the user is presented with a login page.
The login page has a 'session_start()' at the beginning. Looking at my cookies directory, when this page loads, PHP writes a cookie to my C:\Temp directory with a unique ID.
The script calls 'admin' again, which this time detects the $_POST variables sent by the login page, and accordingly writes a session variable indicating the user is logged on, with the intention of using this as a login test later on (the usual method, in other words).
However, expecting the session to be preserved between calls to the 'admin' script, PHP instead writes a new session. It correctly writes to this session, but as soon as you go off the page - for example by calling 'admin' directly again via the browser - it loses it, because it writes a whole new session again!
Why am I losing sessions between page loads - why aren't they being preserved across page calls, as you would expect? Config issue?
Many thanks
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
It's irrelevant for our purposes, but session.cookie_path determines for what paths is the default session cookie valid for. For example, if you were to set it to /foobar, the session cookie would only get set by the browser when you were in files in the http://localhost/foobar directory, and not say a http://localhost/baz directory.
Okay, next question: is your browser accepting cookies? Try var_dump($_COOKIE); on admin, you should get a value that looks like PHPSESSID => 39842y343ja9w8ryaw38 which should be staying constant.
Okay, next question: is your browser accepting cookies? Try var_dump($_COOKIE); on admin, you should get a value that looks like PHPSESSID => 39842y343ja9w8ryaw38 which should be staying constant.
please trywhen you press the link and reload the page session_name and session_id shouldn't change but a new line should be appended to session: each time. Does this work? If not, please post the output.
Code: Select all
<?php
error_reporting(E_ALL);
ini_set('display_errors', true);
session_start();
if ( !isset($_SESSION['xyz']) || !is_array($_SESSION['xyz']) ) {
$_SESSION['xyz'] = array();
}
$_SESSION['xyz'][] = date('H:i:s');
?>
<html>
<head><title>session test</title></head>
<body>
<a href="<?php echo $_SERVER['PHP_SELF']; ?>">re-load page</a><br />
self: <?php echo $_SERVER['PHP_SELF']; ?><br />
session_name: <?php echo session_name(); ?><br />
session_id: <?php echo session_id(); ?><br />
<pre>coookie_params: <?php var_export(session_get_cookie_params()); ?></pre>
<pre>cookies: <?php var_export($_COOKIE); ?></pre>
<pre>session: <?php var_export($_SESSION); ?></pre>
</body>
</html>Hi, many thanks for your test script. Here's the output:
And then
There's no preservation of the session id, and there's no appending of the array - it all gets reset. At least that's established, but I don't know if this is a configuration issue or not?
Code: Select all
re-load page
self: /cookies.php
session_name: PHPSESSID
session_id: q2nfro1b4851mp92h1m1kghj31
coookie_params: array (
'lifetime' => 0,
'path' => '\\',
'domain' => '',
'secure' => false,
)
cookies: array (
)
session: array (
'xyz' =>
array (
0 => '22:35:26',
),
)Code: Select all
re-load page
self: /cookies.php
session_name: PHPSESSID
session_id: d5jeq24ut5tbedclk62aqgp582
coookie_params: array (
'lifetime' => 0,
'path' => '\\',
'domain' => '',
'secure' => false,
)
cookies: array (
)
session: array (
'xyz' =>
array (
0 => '22:35:49',
),
)empty array _COOKIE means your browser did not accept the cookie.mjseaden wrote:coookie_params: array (
'lifetime' => 0,
'path' => '\\',
'domain' => '',
'secure' => false,
)
cookies: array (
)
'path' => '\\' ? Did you play with session.cookie_path? Please remove that line from your php.in then restart the webserver.
Hi. I've removed the cookie_path line from my PHP.ini, and restarted Apache, and now the output is:
And then:
So it now seems to be working. I honestly don't remember changing the cookie path in my PHP ini file - I doubt very much it's the default though.
Thanks for helping out, that probably solves my other issue too I hope.
Cheers
Code: Select all
re-load page
self: /cookies.php
session_name: PHPSESSID
session_id: vpbsjbfelrq9rev2r1q5vr0bd4
coookie_params: array (
'lifetime' => 0,
'path' => '/',
'domain' => '',
'secure' => false,
)
cookies: array (
'PHPSESSID' => 'vpbsjbfelrq9rev2r1q5vr0bd4',
)
session: array (
'admin_valid_user' => '3dfec677ecf45d4c3da3df311b98ea95',
'xyz' =>
array (
0 => '22:45:38',
),
)Code: Select all
re-load page
self: /cookies.php
session_name: PHPSESSID
session_id: vpbsjbfelrq9rev2r1q5vr0bd4
coookie_params: array (
'lifetime' => 0,
'path' => '/',
'domain' => '',
'secure' => false,
)
cookies: array (
'PHPSESSID' => 'vpbsjbfelrq9rev2r1q5vr0bd4',
)
session: array (
'admin_valid_user' => '3dfec677ecf45d4c3da3df311b98ea95',
'xyz' =>
array (
0 => '22:45:38',
1 => '22:47:31',
),
)Thanks for helping out, that probably solves my other issue too I hope.
Cheers