简体   繁体   English

会话变量在PHP5 / Apache2 / Ubuntu12中不持久

[英]Session variables not persistent in PHP5/Apache2/Ubuntu12

I couldn't find this specific problem around. 我找不到周围的这个具体问题。 I have a website that uses session variables (in PHP) to know if the visitor is entering the website or not. 我有一个使用会话变量(在PHP中)的网站来了解访问者是否正在进入该网站。 If he's new, then I redirect him to the detected language. 如果他是新来的,那么我会将他重定向到检测到的语言。 But if he's not new, you let him go wherever he wants to (even the pages in other languages). 但是,如果他不是新手,则可以让他去他想去的任何地方(甚至是其他语言的页面)。

The problem is that the variable used to achieve this ($_SESSION["knownvisitor"]) is not persistent. 问题是用于实现此目的的变量($_SESSION["knownvisitor"])不是持久性的。 I've tested the same website in other server and it works just fine, so I'm pretty sure it's a sessions config problem. 我已经在其他服务器上测试了相同的网站,并且效果很好,所以我很确定这是一个会话配置问题。 The problematic server is an Ubuntu Server 12.04.4 with Apache 2.2.22 and PHP 5.3.10. 问题服务器是带有Apache 2.2.22和PHP 5.3.10的Ubuntu Server 12.04.4。

The sessions are stored in /var/lib/php5 , and I can see the files there but they are empty. 会话存储在/var/lib/php5 ,我可以在其中看到文件,但它们为空。 The session files belong to www-data and have the following rights -rw------- . 会话文件属于www-data并且具有以下权限-rw------- The directory, in the other hand, belongs to "root" and has these rights drwx-wx-wt . 另一方面,该目录属于“ root”,并且具有这些权限drwx-wx-wt

I tried using session_write_close() before redirecting, as recommended in some forums, with no luck. 如某些论坛所建议,在重定向之前,我尝试使用session_write_close() ,但是没有运气。 The redirection is like header('Location: [URL]') . 重定向就像header('Location: [URL]')

Does anyone have a clue? 有人有线索吗? Thank you! 谢谢!

EDIT: I'll add the code of the script so you can understand it's basic behaviour. 编辑:我将添加脚本的代码,以便您可以了解它的基本行为。 This is what I put in the beginning of all the files in my web: 这是我在网络中所有文件的开头添加的内容:

function detectedlang($availlangs)
{
 // FUNCTION STUFF HERE TO FIND OUT WHICH IS THE LANGUAGE OF THE VISITOR
 return $detectedlang;
}

session_start();

if (!isset($_SESSION["knownvisitor"]))
{
 $detectedlang = detectedlang(array("en", "es"));
 $_SESSION["knownvisitor"] = true;
 header("Location: http://www.mysite.com/".$detectedlang);
}

A session only lives for as long as defined in session.gc_maxlifetime I think 20 minutes is the default 会话的生存时间只能达到session.gc_maxlifetime定义的时间,我认为默认为20分钟

If you want to check for a returning visitor you'll need to set a cookie 如果要检查回访者,则需要设置一个Cookie

检查session.save_handler : files也使用error_reporting(E_ALL);

I got it solved thanks to the good people of serverfault.com. 多亏了serverfault.com的好人,我才得以解决。 The problem is that the folder /var/lib/php5 is owned by root and can't be read by anyone else ( drwx-wx-wt ). 问题在于文件夹/var/lib/php5是root拥有的,其他任何人都无法读取( drwx-wx-wt )。 This is why PHP (user www-data ) can't access and retrieve the session data from the file there. 这就是为什么PHP(用户www-data )无法从那里的文件访问和检索会话数据的原因。

A simple change in groups and permissions got it running: 对组和权限进行简单的更改即可使其运行:

chgrp www-data /var/lib/php5
chmod g+rwx /var/lib/php5

In other words, change the group of the directory to www-data and allow its members to read the contents. 换句话说,将目录的组更改为www-data并允许其成员读取内容。

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

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