简体   繁体   English

会话重新生成ID和session_save_path();

[英]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/')". 现在我注意到,一旦生成了新的会话ID,我就会丢失在“ 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( ) . 如前所述,为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. 当您使用相对路径作为前者的参数时, session_save_path( )session_regenerate_id( )不能很好地配合使用。 On my setup (Windows 7, Apache 2.2.25, php 5.3.27), I have the following index.php in c:\\www\\test: 在我的设置(Windows 7,Apache 2.2.25,php 5.3.27)上,我在c:\\ www \\ test中具有以下index.php:

<?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; 相同的会话ID被打印两次;
  • an integer follows each session ID; 每个会话ID后面都有一个整数;
  • the second integer is 1 greater than the first integer; 第二个整数比第一个整数大1;
  • refreshing the page causes the integers to grow by one with each successive refresh; 刷新页面会使整数在每次连续刷新时增长1;
  • the session file is found in directory c:\\www\\test\\sessions (this directory MUST be created beforehand - PHP does not create it for you!); 会话文件位于目录c:\\ www \\ test \\ sessions中(此目录必须事先创建-PHP不会为您创建!);
  • and that session file is updated with each successive refresh. 并且该会话文件在每次连续刷新时都会更新。

When I uncomment the session_regenerate_id( ) command and browse to my script: 当我取消注释session_regenerate_id()命令并浏览到我的脚本时:

  • I get two different session IDs; 我得到两个不同的会话ID。
  • upon refresh, the 1st session ID is the same as the previous page's second session ID; 刷新后,第一个会话ID与上一页的第二个会话ID相同;
  • the integers are always 0 and 1 respectively; 整数始终分别为0和1;
  • there is no session file in c:\\www\\test\\sessions. c:\\ www \\ test \\ sessions中没有会话文件。 (If I pass 'false' to regen( ), then a series of empty session files will accumulate.) (如果我将'false'传递给regen(),则将累积一系列空的会话文件。)

When I instead use the second ini_set command (with the full path), it works fine. 当我改为使用第二个ini_set命令(具有完整路径)时,它可以正常工作。

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. 问题似乎是regen函数混淆了PHP:在重新生成后,PHP将会话文件写入c:\\ Program Files \\ Apache Software Foundation \\ Apache2.2 \\ sessions-或尝试这样做,但是如果该目录包含以下内容,则失败尚未创建。 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. 但是,当我调用session_start()时,PHP不在那儿,因此在脚本调用之间会丢失所有累积的会话知识。

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. 我只是想出这一点,因为我没有在“ php_errors.log”中收到“目录不存在”错误消息-至少不是我期望的那样。 I spotted another file by that name in the Apache2.2 directory. 我在Apache2.2目录中发现了该名称的另一个文件。 In php.ini, I had specified a simple filename for my error_log directive, without the directory path. 在php.ini中,我为我的error_log指令指定了一个简单的文件名,没有目录路径。 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. 在regen命令之前生成的所有错误消息都将到达c:\\ www \\ test \\ php_errors.log,但在重新生成之后,然后将其转到Apache目录中的文件。 So the confusion regen causes appears to extend beyond the session functionality. 因此,引起混乱的原因似乎超出了会话功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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