I'm wondering why session_start behaves in a peculiar manner dependent on cookies being accepted or not.
If cookies are accepted the php scripts below work as expected by retaining the session id.
After turning off accept cookies in the browser, session_start grabs a new id each time a page is loaded and I no longer have access to $_SESSION[] and of course a new session file is created.
After turning accept cookies back on the original id is restored and I again have access to $_SESSION[].
Why does this happen and how do I keep the first session id set regardless of
whether or not cookies are accepted? I did try turning on and off several of the
environment variables in php.ini file without satisfactory results each time restarting Apache server.
PHP ver. 5.2.0 is running on a Win XP SP 2, Apache ver. 2.2.4, MySql 5.0.27.
php.ini session is set:
session.save_path="C:\phptemp"
session.use_cookies = 1
session.name = PHPSESSID
session.auto_start = 0
session.use_trans_sid = 0
the file index.php contains the following:
<?php
// index.php
session_start();
$_SESSION['favcolor'] = 'green';
$_SESSION['animal'] = 'dog';
$_SESSION['time'] = time();
echo '<html><head><title>Page 1 SID</title></head><body>';
echo '<br />Welcome to page #1<br /><br />';
echo 'Your referer is: ' . $_SERVER['HTTP_REFERER'] . '<br />';
echo 'You came from ip: ' . $_SERVER['REMOTE_ADDR'] . '<br />';
echo 'Your host name is: ' . $_SERVER['HTTP_HOST'] . '<br /><br />';
echo 'Session ID is: ' . session_id();;
echo '<br />Session set as a Cookie: ' .$_COOKIE['PHPSESSID'];
echo '<br /><br /><a href="page2.php">Goto page 2</a>';
//phpinfo();
echo '</body></html>';
?>
the file page2.php contains the following:
<?php
// page2.php
session_start();
echo '<html><head><title>Page 2 SID</title></head><body>';
echo 'Welcome to page #2';
echo '<br /><br />Session ID is: '.session_id();
echo '<br />Session set as a Cookie: ' .$_COOKIE['PHPSESSID'];
echo '<br /><br />favcolor set to: '.$_SESSION['favcolor'];
echo '<br />animal set to: '.$_SESSION['animal'];
echo '<br />time set to: '.$_SESSION['time'];
echo '<br /><br /><a href="index.php">Goto page 1</a>';
echo '</body></html>';
?>