简体   繁体   中英

Session regenerate id and session_save_path( );

Hello I have this script below:

 <?php
   session_save_path('sessions/');
    if (!isset($_SESSION)) {
    session_start();
    }
   $loginUsername = A;

   if (PHP_VERSION >= 5.1) {session_regenerate_id(true); } else {session_regenerate_id();}
$_SESSION['wallet_email'] = $loginUsername;
   echo  $_SESSION['wallet_email']

?>

Now I have noticed that once a new session id is generated, I lose the path specified in "session_save_path('sessions/')". This means I am unable to create new sessions.

I actually get the following error.

Warning: Unknown: open(sessions//sess_7af5c5f2f2bd83afff0203cc45190260, 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 (sessions/) in Unknown on line 0

Please I need urgent help with this.

As has been said, use an absolute path for the parameter to session_save_path( ) . Here's why.

session_save_path( ) and session_regenerate_id( ) do not play nicely together when you use a relative path as a parameter to the former. On my setup (Windows 7, Apache 2.2.25, php 5.3.27), I have the following index.php in c:\\www\\test:

<?php
date_default_timezone_set( 'America/New_York' );
ini_set( 'session.save_path', './sessions' );
//ini_set( 'session.save_path', 'c:/www/test/sessions' );

session_start( );
if ( !isset( $_SESSION[ 'count' ] )) {
    $_SESSION[ 'count' ] = 0;
}
$s1 = session_id( );
$b1 = $_SESSION[ 'count' ]++;
//session_regenerate_id( true );
$s2 = session_id( );
$b2 = $_SESSION[ 'count' ]++;
?>
<html>
    <head>
        <title>Test</title>
    </head>
    <body>
        <h1>Test</h1>
        <p><?php echo "$s1: $b1"; ?></p>
        <p><?php echo "$s2: $b2"; ?></p>
    </body>
</html>

It works just as you would think. When I browse to the script:

  • the same session ID is printed twice;
  • an integer follows each session ID;
  • the second integer is 1 greater than the first integer;
  • refreshing the page causes the integers to grow by one with each successive refresh;
  • the session file is found in directory c:\\www\\test\\sessions (this directory MUST be created beforehand - PHP does not create it for you!);
  • and that session file is updated with each successive refresh.

When I uncomment the session_regenerate_id( ) command and browse to my script:

  • I get two different session IDs;
  • upon refresh, the 1st session ID is the same as the previous page's second session ID;
  • the integers are always 0 and 1 respectively;
  • there is no session file in c:\\www\\test\\sessions. (If I pass 'false' to regen( ), then a series of empty session files will accumulate.)

When I instead use the second ini_set command (with the full path), it works fine.

The problem seems to be that the regen function confuses PHP: after the regen, PHP writes the session files to c:\\Program Files\\Apache Software Foundation\\Apache2.2\\sessions - or, it tries to, but fails if that directory has not already been created. Once I created the second sessions directory, I saw sessions files show up there. But PHP isn't looking there when I call session_start( ), so all the accumulated session knowledge is lost between script invocations.

I only figured this out because I was NOT getting 'directory does not exist' error messages in my 'php_errors.log' - at least, not where I was expecting. I spotted another file by that name in the Apache2.2 directory. In php.ini, I had specified a simple filename for my error_log directive, without the directory path. Any error messages generated before the regen command went to c:\\www\\test\\php_errors.log, but after the regen then went to the file in the Apache directory. So the confusion regen causes appears to extend beyond the session functionality.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM