php PHP会话数据没有被保存

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/155920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:42:24  来源:igfitidea点击:

PHP Session data not being saved

phpsession

提问by CrackerHyman

I have one of those "I swear I didn't touch the server" situations. I honestly didn't touch any of the php scripts. The problem I am having is that php data is not being saved across different pages or page refreshes. I know a new session is being created correctly because I can set a session variable (e.g. $_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?

我有一种“我发誓我没有碰过服务器”的情况。老实说,我没有接触任何 php 脚本。我遇到的问题是 php 数据没有跨不同页面或页面刷新保存。我知道正在正确创建一个新会话,因为我可以设置一个会话变量(例如 $_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. I'm thinking this is a problem on their side, but I wanted to make sure I'm not a complete idiot first.

刷新页面后,'views' 变量永远不会增加。我认为这是他们这边的问题,但我想首先确保我不是一个彻头彻尾的白痴。

Here is the phpinfo() for my hosts' server (PHP Version 4.4.7): alt text

这是我的主机服务器(PHP 版本 4.4.7)的 phpinfo(): 替代文字

采纳答案by CrackerHyman

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. A solution would have been to declare 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.

感谢所有有用的信息。事实证明,我的主机更改了服务器并开始使用除 /var/php_sessions 之外的不同会话保存路径,该路径不再存在。一个解决方案是ini_set(' session.save_path','SOME WRITABLE PATH');在我所有的脚本文件中声明,但这会很痛苦。我与主持人交谈,他们明确地将会话路径设置为确实存在的真实路径。希望这可以帮助任何有会话路径问题的人。

回答by paul

Check to make sure you are not mixing https:// with http://. Session variables do not flow between secure and insecure sessions.

检查以确保您没有将 https:// 与 http:// 混合使用。会话变量不会在安全会话和不安全会话之间流动。

回答by Shane N

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. Since the cookie was not being found, php was creating a new session every time, thus session variables were not being seen.

有同样的问题——发生在我身上的是我们的服务器管理员将 session.cookie_secure 布尔值更改为 On,这意味着 cookie 将仅通过安全连接发送。由于没有找到 cookie,php 每次都创建一个新会话,因此没有看到会话变量。

回答by vIceBerg

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);to have a dump of this variable and see the content....

你也可以吗?print_r($_SESSION);转储此变量并查看内容....

Regarding your phpinfo(), is the session.save_patha valid one? Does your web server have write access to this directory?

关于你的phpinfo(),是session.save_path有效的吗?您的 Web 服务器是否对该目录具有写访问权限?

Hope this helps.

希望这可以帮助。

回答by harry

I had following problem

我有以下问题

index.php

索引.php

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

index2.php

索引2.php

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

The variable $_SESSION['a']was not set correctly. Then I have changed the index.phpacordingly

变量$_SESSION['a']设置不正确。然后,我已经改变了index.phpacordingly

<?
    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 :)

我不知道这在内部意味着什么,我只是向自己解释会话变量更改不够快:)

回答by DGM

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)

确保你打开了cookies..(我忘了什么时候关掉它们来测试一些东西)

Use firefox with the firebug extension to see if the cookie is being set and transmitted back.

使用带有 firebug 扩展的 firefox 来查看 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 系列的最后一个。

回答by buzzy blogger

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.

检查脚本运行所在文件夹的组和所有者是谁。如果group id或者user id错误,比如设置为root,会导致session不能正确保存。

回答by Harry

If you set a session in php5, then try to read it on a php4 page, it might not look in the correct place! Make the pages the same php version or set the session_path.

如果您在 php5 中设置会话,然后尝试在 php4 页面上阅读它,它可能看起来不正确!使页面具有相同的 php 版本或设置 session_path。

回答by Harry

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. 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.

我知道当我遇到类似问题时,我发现的一个解决方案(OSX 与 Apache 1 并刚刚切换到 PHP5)是取消设置 1 个特定键(即 unset($_SESSION['key']);) 导致它无法保存。一旦我不再取消设置该键,它就会保存。我再也没有见过这个,除了在另一个站点的那个服务器上,但它是一个不同的变量。两者都没有什么特别的。

Thanks for this one 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(这对我的应用程序来说很好),并且它可以工作。

回答by Nathan Strong

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? 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 (i.e. you turned off cookies and forgot to turn them on, or are blocking cookies by mistake).

  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.

  1. 你在任何地方调用 session_unset() 或 session_destroy() 吗?这些函数将立即删除会话数据。如果我把这些放在我的脚本的末尾,它就会开始像你描述的那样表现。

  2. 它在所有浏览器中的行为都一样吗?如果它在一个浏览器上运行而不在另一个浏览器上运行,则您可能在无法运行的浏览器上遇到了配置问题(即您关闭了 cookie 而忘记打开它们,或者错误地阻止了 cookie)。

  3. 会话文件夹是否可写?你不能用 is_writable() 测试这个,所以你需要转到文件夹(从 phpinfo() 它看起来像 /var/php_sessions)并确保会话实际上被创建。