PHP 会话变量未在页面之间保存

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16450174/
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-25 11:09:29  来源:igfitidea点击:

PHP session variables not saving between pages

phpsession-variables

提问by Sean McCully

I know this might be a duplicate question, but I can't seem to find an answer to my specific problem.

我知道这可能是一个重复的问题,但我似乎无法找到我的具体问题的答案。

I have 3 PHP files. signin.php gets users data and passes it to signin_auth.php.

我有 3 个 PHP 文件。signin.php 获取用户数据并将其传递给 signin_auth.php。

signin.php then redirects to mytoolkit.php after successful sign in.

signin.php 然后在成功登录后重定向到 mytoolkit.php。

signin_auth.php uses the following code at the very end of the PHP script:

signin_auth.php 在 PHP 脚本的最后使用以下代码:

<?
session_start();

$_SESSION['sessionID'] = $id;
$_SESSION['time'] = time();

header ("Location: mytoolkit.php");
exit;

?>

Then, the users is redirected to mytoolkit.php, which uses this code:

然后,用户被重定向到 mytoolkit.php,它使用以下代码:

<?

session_start();

if (!isset($_SESSION['sessionID'])) {

    header('Location: signin.php?message=4');
        exit;

}

$inactive = 5400;
$session_life = time() - $_SESSION['time'];

if ($session_life > $inactive) { 

   session_destroy(); 
   header("Location: signin.php?message=5"); 
   exit;

}

?>

The session variables aren't storing and mytoolkit.php keeps redirecting me to signin.php?message=4

会话变量未存储并且 mytoolkit.php 不断将我重定向到 signin.php?message=4

Probably a stupid mistake. I've just been looking at it too long.

可能是一个愚蠢的错误。我只是看着它太久了。

UPDATE --

更新 -

So I have a couple of servers. Both on iPage. I moved the entire folder over to a differnent server (abc.com/toolkit is now copied to xyz.com/toolkit) The app runs perfectly on xyz.com... it the first server that's giving me probelms.

所以我有几台服务器。两者都在 iPage 上。我将整个文件夹移动到不同的服务器(abc.com/toolkit 现在被复制到 xyz.com/toolkit) 该应用程序在 xyz.com 上完美运行......它是第一个给我问题的服务器。

There both run on the same hosting company. I'm not sure what to do.

两者都在同一托管公司上运行。我不知道该怎么做。

回答by Sean McCully

I finally figured it out... It was an issues with my php.inifile on iPage's server. For some reason, they had it set to

我终于想通了...这是我php.ini在 iPage 服务器上的文件的问题。出于某种原因,他们将其设置为

session.save_path = "/var/php_sessions"

Where it should be my document root. That's not the first time they've messed stuff up.

它应该是我的文档根目录。这不是他们第一次搞砸了。

回答by Amir

if you did not destroy session with session_destroy();before if (!isset($_SESSION['sessionID'])) {statement, then a reason could be $idhas null value.

如果您没有使用session_destroy();beforeif (!isset($_SESSION['sessionID'])) {语句销毁会话,则原因可能为$id空值。

回答by blokeish

I was running httpd as another user (not apache). On checking /etc/php.ini I found that "session directory must be owned by process owner". So in /etc/httpd/conf.d/php.conf I changed the following:

我以另一个用户(不是 apache)的身份运行 httpd。在检查 /etc/php.ini 时,我发现“会话目录必须由进程所有者拥有”。所以在 /etc/httpd/conf.d/php.conf 我改变了以下内容:

php_value session.save_path    "/var/lib/php/session"
php_value soap.wsdl_cache_dir  "/var/lib/php/wsdlcache"

to

php_value session.save_path    "/home/[httpduser]/php/session"
php_value soap.wsdl_cache_dir  "/home/[httpduser]/php/wsdlcache"

and reloaded httpd.

并重新加载httpd。

Not sure if it was a better idea to change the owner of /var/lib/php to [httpduser]

不确定将 /var/lib/php 的所有者更改为 [httpduser] 是否更好

回答by CodeBird

I think this is your issue

我认为这是你的问题

  $inactive = 5400;
  $session_life = time() - $_SESSION['time'];
  if ($session_life > $inactive) { 
       session_destroy(); 
       header("Location: signin.php?message=5"); 
       exit;
  }

回答by Hugo Delsing

If you use a header('location: example.com')in your script it is sending a 302 redirect to your browser. Now this is none permanent, but some browser still cache this result. Just google it

如果您header('location: example.com')在脚本中使用 a ,它会向您的浏览器发送 302 重定向。现在这不是永久的,但一些浏览器仍然缓存这个结果。只需谷歌一下

So it could very well be that your browser is redirecting your request before it even accesses your code. Try changing signin_auth.phpto header ("Location: mytoolkit.php?t=".time());and then call the page with some random parameter attached to prevent the first page from caching. EG: /signin_auth.php?t=random

因此,很可能您的浏览器在访问您的代码之前就重定向了您的请求。尝试更改signin_auth.phpheader ("Location: mytoolkit.php?t=".time());然后调用带有一些随机参数的页面,以防止第一页缓存。例如:/signin_auth.php?t=random