简体   繁体   English

PHP会话数据未保存

[英]PHP Session data not being saved

I have one of those "I swear I didn't touch the server" situations. 我有一个“我发誓我没有触摸服务器”的情况。 I honestly didn't touch any of the php scripts. 老实说,我没有触及任何PHP脚本。 The problem I am having is that php data is not being saved across different pages or page refreshes. 我遇到的问题是PHP数据没有保存在不同的页面或页面刷新。 I know a new session is being created correctly because I can set a session variable (eg $_SESSION['foo'] = "foo" and print it back out on the same page just fine. But when I try to use that same variable on another page it is not set! Is there any php functions or information I can use on my hosts server to see what is going on? 我知道正在创建一个新会话,因为我可以设置一个会话变量(例如$ _SESSION ['foo'] =“foo”并将其打印在同一页面上就好了。但是当我尝试使用同一个变量时在另一个页面上它没有设置!我可以在我的主机服务器上使用任何PHP函数或信息来查看发生了什么?

Here is an example script that does not work on my hosts' server as of right now: 这是一个示例脚本,目前在我的主机服务器上不起作用:

<?php
session_start();
if(isset($_SESSION['views']))
    $_SESSION['views'] = $_SESSION['views']+ 1;
else
    $_SESSION['views'] = 1;

echo "views = ". $_SESSION['views'];
echo '<p><a href="page1.php">Refresh</a></p>';
?>

The 'views' variable never gets incremented after doing a page refresh. 执行页面刷新后,'views'变量永远不会增加。 I'm thinking this is a problem on their side, but I wanted to make sure I'm not a complete idiot first. 我认为这是他们身边的问题,但我想确保我不是一个完全白痴。

Here is the phpinfo() for my hosts' server (PHP Version 4.4.7): 这是我的主机服务器的phpinfo()(PHP版本4.4.7): 替代文字

Thanks for all the helpful info. 感谢所有有用的信息。 It turns out that my host changed servers and started using a different session save path other than /var/php_sessions which didn't exist anymore. 事实证明,我的主机更改了服务器并开始使用除/ var / php_sessions之外的其他会话保存路径,这些路径不再存在。 A solution would have been to declare ini_set(' session.save_path','SOME WRITABLE PATH'); 一个解决方案就是声明ini_set(' session.save_path','SOME WRITABLE PATH'); in all my script files but that would have been a pain. 在我的所有脚本文件中,但这将是一个痛苦。 I talked with the host and they explicitly set the session path to a real path that did exist. 我与主机进行了交谈,他们明确地将会话路径设置为确实存在的真实路径。 Hope this helps anyone having session path troubles. 希望这可以帮助任何有会话路径麻烦的人。

Check to make sure you are not mixing https:// with http://. 检查以确保您没有将https://与http://混合使用。 Session variables do not flow between secure and insecure sessions. 会话变量不会在安全会话和不安全会话之间流动。

Had same problem - what happened to me is our server admin changed the session.cookie_secure boolean to On, which means that cookies will only be sent over a secure connection. 有同样的问题 - 发生在我身上的是我们的服务器管理员将session.cookie_secure布尔值更改为On,这意味着cookie只会通过安全连接发送。 Since the cookie was not being found, php was creating a new session every time, thus session variables were not being seen. 由于没有找到cookie,php每次都会创建一个新会话,因此没有看到会话变量。

Use phpinfo() and check the session.* settings. 使用phpinfo()并检查session.*设置。

Maybe the information is stored in cookies and your browser does not accept cookies, something like that. 也许这些信息存储在cookie中,而您的浏览器也不接受cookie,就像那样。

Check that first and come back with the results. 首先检查并返回结果。

You can also do a print_r($_SESSION); 你也可以做一个print_r($_SESSION); to have a dump of this variable and see the content.... 转储此变量并查看内容....

Regarding your phpinfo() , is the session.save_path a valid one? 关于你的phpinfo()session.save_path是有效的吗? Does your web server have write access to this directory? 您的Web服务器是否具有此目录的写入权限?

Hope this helps. 希望这可以帮助。

I had following problem 我有以下问题

index.php 的index.php

<?
    session_start();
    $_SESSION['a'] = 123;
    header('location:index2.php');
?>

index2.php index2.php

<?
  session_start();
  echo $_SESSION['a'];
?>

The variable $_SESSION['a'] was not set correctly. 变量$_SESSION['a']未正确设置。 Then I have changed the index.php acordingly 然后我依次更改了index.php

<?
    session_start();
    $_SESSION['a'] = 123;
    session_write_close();
    header('location:index2.php');
?>

I dont know what this internally means, I just explain it to myself that the session variable change was not quick enough :) 我不知道这内部意味着什么,我只是向自己解释会话变量变化不够快:)

Check to see if the session save path is writable by the web server. 检查Web服务器是否可以写入会话保存路径。

Make sure you have cookies turned on.. (I forget when I turn them off to test something) 确保你打开了饼干..(当我把它们关掉来测试时我会忘记)

Use firefox with the firebug extension to see if the cookie is being set and transmitted back. 使用firefox和firebug扩展来查看cookie是否被设置并传回。

And on a unrelated note, start looking at php5, because php 4.4.9 is the last of the php4 series. 并且在一个不相关的说明中,开始查看php5,因为php 4.4.9是php4系列的最后一个。

Check who the group and owner are of the folder where the script runs. 检查组和所有者是脚本运行的文件夹的对象。 If the group id or user id are wrong, for example, set to root, it will cause sessions to not be saved properly. 如果组ID或用户标识错误(例如,设置为root),则会导致会话无法正确保存。

I know one solution I found (OSX with Apache 1 and just switched to PHP5) when I had a similar problem was that unsetting 1 specific key (ie unset($_SESSION['key']);) was causing it not to save. 我知道我找到的一个解决方案(OSX与Apache 1并且刚刚切换到PHP5)当我遇到类似问题时,未设置1个特定键(即未设置($ _ SESSION ['key']);)导致它不能保存。 As soon as I didn't unset that key any more it saved. 一旦我没有取消该密钥,它就会保存。 I have never seen this again, except on that server on another site, but then it was a different variable. 我再也没见过这个,除了在另一个网站上的那个服务器上,但那时它是一个不同的变量。 Neither were anything special. 两者都不特别。

Thanks for this one Darryl. 谢谢你这个Darryl。 This helped me out. 这帮助了我。 I was deleting a session variable, and for some reason it was keeping the session from committing. 我正在删除一个会话变量,由于某种原因,它使会话保持不变。 now i'm just setting it to null instead (which is fine for my app), and it works. 现在我只是将它设置为null(这对我的应用程序来说很好),并且它有效。

Check the value of "views" when before you increment it. 在增加之前检查“视图”的值。 If, for some bizarre reason, it's getting set to a string, then when you add 1 to it, it'll always return 1. 如果由于某些奇怪的原因,它被设置为字符串,那么当你向它添加1时,它将始终返回1。

if (isset($_SESSION['views'])) {
    if (!is_numeric($_SESSION['views'])) {
        echo "CRAP!";
    }
    ++$_SESSION['views'];
} else {
    $_SESSION['views'] = 1;
}

Well, we can eliminate code error because I tested the code on my own server (PHP 5). 好吧,我们可以消除代码错误,因为我在自己的服务器上测试了代码(PHP 5)。

Here's what to check for: 这是检查的内容:

  1. Are you calling session_unset() or session_destroy() anywhere? 你在任何地方调用session_unset()或session_destroy()吗? These functions will delete the session data immediately. 这些函数将立即删除会话数据。 If I put these at the end of my script, it begins behaving exactly like you describe. 如果我将这些放在脚本的末尾,它就会像你描述的那样开始表现。

  2. Does it act the same in all browsers? 它在所有浏览器中的行为是否相同? If it works on one browser and not another, you may have a configuration problem on the nonfunctioning browser (ie you turned off cookies and forgot to turn them on, or are blocking cookies by mistake). 如果它在一个浏览器而不是另一个浏览器上工作,则可能在非功能浏览器上存在配置问题(即您关闭了cookie并忘记打开它们,或者错误地阻止了cookie)。

  3. Is the session folder writable? 会话文件夹是否可写? You can't test this with is_writable(), so you'll need to go to the folder (from phpinfo() it looks like /var/php_sessions) and make sure sessions are actually getting created. 您无法使用is_writable()进行测试,因此您需要转到文件夹(从phpinfo()看起来像/ var / php_sessions)并确保会话实际上已创建。

If you set a session in php5, then try to read it on a php4 page, it might not look in the correct place! 如果您在php5中设置会话,然后尝试在php4页面上阅读它,它可能看起来不正确! Make the pages the same php version or set the session_path. 使页面与php版本相同或设置session_path。

I spent ages looking for the answer for a similar problem. 我花了很多年的时间寻找类似问题的答案。 It wasn't an issue with the code or the setup, as a very similar code worked perfectly in another .php on the same server. 这不是代码或设置的问题,因为非常相似的代码在同一服务器上的另一个.php中完美地工作。 Turned out the problem was caused by a very large amount of data being saved into the session in this page. 原来这个问题是由于在此页面中将大量数据保存到会话中引起的。 In one place we had a line like this: $_SESSION['full_list'] = $full_list where $full_list was an array of data loaded from the database; 在一个地方我们有这样一行: $_SESSION['full_list'] = $full_list其中$full_list是从数据库加载的数据数组; each row was an array of about 150 elements. 每行是一个大约150个元素的数组。 When the code was initially written a couple of years ago, the DB only contained about 1000 rows, so the $full_list contained about 100 elements, each being an array of about 20 elements. 当代码最初在几年前编写时,DB只包含大约1000行,因此$full_list包含大约100个元素,每个元素是一个包含大约20个元素的数组。 With time, the 20 elements turned into 150 and 1000 rows turned into 17000, so the code was storing close to 64 meg of data into the session. 随着时间的推移,20个元素变成150和1000行变成17000,因此代码将近64兆的数据存储到会话中。 Apparently, with this amount of data being stored, it refused to store anything else. 显然,在存储了大量数据的情况下,它拒绝存储任何其他内容。 Once we changed the code to deal with data locally without saving it into the session, everything worked perfectly. 一旦我们更改代码以在本地处理数据而不将其保存到会话中,一切都运行良好。

A common issue often overlooked is also that there must be NO other code or extra spacing before the session_start() command. 经常被忽视的一个常见问题是在session_start()命令之前必须没有其他代码或额外的间距。

I've had this issue before where I had a blank line before session_start() which caused it not to work properly. 我之前遇到过这个问题,我在session_start()之前有一个空行,导致它无法正常工作。

Edit your php.ini. 编辑你的php.ini。
I think the value of session.gc_probability is 1, so set it to 0. 我认为session.gc_probability的值是1,所以将其设置为0。

session.gc_probability=0

I know one solution I found (OSX with Apache 1 and just switched to PHP5) when I had a similar problem was that unsetting 1 specific key (ie unset($_SESSION['key']);) was causing it not to save. 我知道我找到的一个解决方案(OSX与Apache 1并且刚刚切换到PHP5)当我遇到类似问题时,未设置1个特定键(即未设置($ _ SESSION ['key']);)导致它不能保存。 As soon as I didn't unset that key any more it saved. 一旦我没有取消该密钥,它就会保存。 I have never seen this again, except on that server on another site, but then it was a different variable. 我再也没见过这个,除了在另一个网站上的那个服务器上,但那时它是一个不同的变量。 Neither were anything special. 两者都不特别。

Here is one common problem I haven't seen addressed in the other comments: is your host running a cache of some sort? 这是我在其他评论中没有看到的一个常见问题:您的主机是否正在运行某种缓存? If they are automatically caching results in some fashion you would get this sort of behavior. 如果他们以某种方式自动缓存结果,你会得到这种行为。

Adding my solution: 添加我的解决方案

Check if you access the correct domain . 检查您是否访问了正确的域 I was using www.mysite.com to start the session, and tried to receive it from mysite.com (without the www ). 我正在使用www.mysite.com开始会话,并试图从mysite.com (没有www )接收它。

I have solved this by adding a htaccess rewrite of all domains to www to be on the safe side/site. 我已经解决了这个问题,将所有域的htaccess重写添加到www以保证安全/站点。

Also check if you use http or https. 另请检查您是使用http还是https。

I had session cookie path set to "//" instead of "/". 我将会话cookie路径设置为“//”而不是“/”。 Firebug is awesome. 萤火虫太棒了。 Hope it helps somebody. 希望它对某人有帮助。

I had this problem when using secure pages where I was coming from www.domain.com/auth.php that redirected to domain.com/destpage.php. 我使用安全页面时出现此问题,我来自www.domain.com/auth.php,重定向到domain.com/destpage.php。 I removed the www from the auth.php link and it worked. 我从auth.php链接中删除了www并且它有效。 This threw me because everything worked otherwise; 这让我失望,因为一切都行不通; the session was not set when I arrived at the destination though. 虽然我到达目的地时没有设置会话。

只是想添加一个小注释,如果您不小心错过了页面上的session_start()语句,也会发生这种情况。

Check if you are using session_write_close(); 检查您是否使用session_write_close(); anywhere, I was using this right after another session and then trying to write to the session again and it wasn't working.. so just comment that sh*t out 在任何地方,我在另一个会话之后正在使用这个,然后再次尝试写入会话并且它无法工作..所以只需评论sh * t out

Another few things I had to do (I had same problem: no sesson retention after PHP upgrade to 5.4). 我必须做的另外几件事(我有同样的问题:PHP升级到5.4后没有sesson保留)。 You many not need these, depending on what your server's php.ini contains (check phpinfio()); 您可能不需要这些,具体取决于您的服务器的php.ini包含的内容(请参阅phpinfio());

session.use_trans_sid=0 ; Do not add session id to URI (osc does this)
session.use_cookies=0;  ; ensure cookies are not used
session.use_only_cookies=0 ; ensure sessions are OK to use IMPORTANT
session.save_path=~/tmp/osc; ; Set to same as admin setting
session.auto_start = off; Tell PHP not to start sessions, osc code will do this

Basically, your php.ini should be set to no cookies, and session parameters must be consistent with what osc wants. 基本上,你的php.ini应该设置为没有cookie,会话参数必须与osc想要的一致。

You may also need to change a few session code snippets in application_top.php - creating objects where none exist in the tep_session_is_registered(...) calls (e eg. navigation object), set $HTTP_ variables to the newer $_SERVER ones and a few other isset tests for empty objects (google for info). 您可能还需要在application_top.php中更改一些会话代码片段 - 创建tep_session_is_registered(...)调用中不存在的对象(例如导航对象),将$ HTTP_变量设置为较新的$ _SERVER变量和a空对象的其他一些isset测试(google for info)。 I ended up being able to use the original sessions.php files (includes/classes and includes/functions) with a slightly modified application_top.php to get things going again. 我最终能够使用原始的sessions.php文件(包括/ classes和includes / functions)以及稍微修改过的application_top.php来重新开始。 The php.ini settings were the main problem, but this of course depends on what your server company has installed as the defaults. php.ini设置是主要问题,但这当然取决于您的服务器公司安装的默认设置。

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

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