简体   繁体   English

如何解决PHP会话文件为空的问题?

[英]How to troubleshoot PHP session file empty issue?

I have a very simple test page for PHP session. 我有一个非常简单的PHP会话测试页。

<?php
session_start();

if (isset($_SESSIONS['views']))
{
    $_SESSIONS['views'] = $_SESSIONS['pv'] + 1;
}
else
{
    $_SESSIONS['views'] = 0;
}

var_dump($_SESSIONS);

?>

After refreshing the page, it always show 刷新页面后,它始终显示

array
  'views' => int 0

The environment is XAMPP 1.7.3. 环境是XAMPP 1.7.3。 I checked phpInfo(). 我检查了phpInfo()。 The session is enabled. 会话已启用。

Session Support     enabled
Registered save handlers    files user sqlite
Registered serializer handlers  php php_binary wddx

Directive   Local Value Master Value
session.auto_start  Off Off
session.bug_compat_42   On  On

When the page is accessed, there is session file sess_lnrk7ttpai8187v9q6iok74p20 created in my "D:\\xampp\\tmp" folder. 访问该页面时,在我的“ D:\\ xampp \\ tmp”文件夹中创建了会话文件sess_lnrk7ttpai8187v9q6iok74p20 But the content is empty. 但是内容为空。

With Firebug, I can see cookies about the session. 使用Firebug,我可以看到有关该会话的cookie。

Cookie  PHPSESSID=lnrk7ttpai8187v9q6iok74p20

It seems session data is not flushed to files. 似乎会话数据未刷新到文件。

Is there any way or direction to trouble shoot this issue? 有什么方法或方向可以解决这个问题?

Thanks. 谢谢。

BTW, it is $_SESSION not $_SESSIONS . 顺便说一句,它是$_SESSION而不是$_SESSIONS

Hence why it isn't saving the data. 因此,为什么它不保存数据。

您需要设置的变量称为$_SESSION而不是$_SESSIONS

use $_SESSION and check it by print_r($_SESSION); 使用$ _SESSION并通过print_r($ _ SESSION)进行检查;

              <?php
                 session_start();

             if (isset($_SESSION['views']))
              {
                 $_SESSION['views'] = $_SESSION['pv'] + 1;
              }
             else
             {
              $_SESSION['views'] = 0;
             }
              echo "<pre>";

              print_r($_SESSION);

              ?>

Heres what I did ... 这是我所做的...

  • Install sqlite for php if older than php5 (installed and enabled by default since php5). 如果PHP早于php5,则安装sqlite(自php5起默认安装并启用)。

  • In your php.ini (for the location see phpinfo() or run>php -i) change the line .. 在您的php.ini中(有关位置,请参阅phpinfo()或运行> php -i),更改行..

session.save_handler = files session.save_handler =文件

to

session.save_handler = sqlite session.save_handler = sqlite

  • Then in the same file (php.ini) make sure NOTICES are turned on 然后在同一文件(php.ini)中确保已打开NOTICES

error_reporting = E_ALL error_reporting = E_ALL

  • restart apache if you changed php.ini 如果您更改了php.ini,请重新启动apache

/etc/init.d/apache2 restart /etc/init.d/apache2重新启动

Now you will be getting a sensible error message most likely revealing you dont have correct permissions to your session.save_path so ... 现在,您将收到一条明智的错误消息,最有可能表明您没有对session.save_path的正确权限,因此...

  • Find out your web services user and group unless known. 找出您的Web服务用户和组,除非已知。 There are several ways to do so but I placed this line temporarily in my php code ... 有几种方法可以做到这一点,但我将这一行临时放在了我的PHP代码中...

print_r(posix_getpwuid(posix_getuid())); print_r(posix_getpwuid(posix_getuid()));

And I found that user www-data and group www-data were set for this web user by my ispconfig3. 我发现ispconfig3为该Web用户设置了用户www-data和组www-data。

  • Make a session directory on your web path (probably /var/www) for the web user. 在Web路径(可能是/ var / www)上为Web用户创建一个会话目录。

    sudo mkdir /var/www/whatever; 须藤mkdir / var / www / whatever; sudo chown <user_from_last_step>:<group_from_last_step> /var/www/whatever sudo chown <user_from_last_step>:<group_from_last_step> / var / www / whatever

    eg>sudo chown www-data:www-data /var/www/whatever 例如:> sudo chown www-data:www-data / var / www / whatever

  • Again in your php.ini file make sure session.save_path is commented out, something like ... 再次在您的php.ini文件中,确保session.save_path被注释掉,类似...

;session.save_path = /var/lib/php5 ; session.save_path = / var / lib / php5

but not 但不是

session.save_path = /var/lib/php5 session.save_path = / var / lib / php5

  • restart apache if you edited php.ini 如果您编辑了php.ini,请重新启动apache

  • Now go to your php code (Many have a SetEnv.php ish type script which does some global stuff and is imported by all files) and add the following line BEFORE you call session_start() ... 现在转到您的php代码(许多都有SetEnv.php ish类型的脚本,该脚本执行一些全局性操作并由所有文件导入),并在调用session_start()之前添加以下行:

ini_set('session.save_path', '0;0750;/var/www/whatever'); ini_set('session.save_path','0; 0750; / var / www / whatever');

Note that we set session.save_path in your code rather than in php.ini to help keep it secure, so to replace 'whatever' with something inventive would be useful. 请注意,我们在您的代码中而不是在php.ini中设置了session.save_path,以确保其安全,因此用有创造力的内容替换“任何内容”将很有用。

By now it should appear to be working, but not quite, it doesent work accross ajax requests for some reason that I dont care about related to sqlite ... so ... 到现在为止,它似乎应该可以正常工作,但是由于某些我不关心与sqlite相关的原因,它确实可以处理ajax请求。

  • Go back to your php.ini file and change the line ... 返回您的php.ini文件并更改行...

session.save_handler = sqlite session.save_handler = sqlite

back to 回到

session.save_handler = files. session.save_handler =文件。

  • restart apache 重新启动Apache

Fixed :) Hopefully :| 固定:)希望:|

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

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