重定向后 PHP 会话丢失 [英] PHP session lost after redirect

查看:45
本文介绍了重定向后 PHP 会话丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何解决PHP重定向后会话丢失的问题?

How do I resolve the problem of losing a session after a redirect in PHP?

最近,我遇到了一个很常见的问题,重定向后丢失会话.在通过这个网站搜索后,我仍然找不到解决方案(尽管 this 来了最近的).

Recently, I encountered a very common problem of losing session after redirect. And after searching through this website I can still find no solution (although this came the closest).

更新

我找到了答案,我想我会把它贴在这里以帮助遇到同样问题的人.

I have found the answer and I thought I'd post it here to help anyone experiencing the same problem.

推荐答案

首先,执行以下常规检查:

First, carry out these usual checks:

  1. 确保在调用任何会话之前调用 session_start();.因此,一个安全的选择是将它放在页面的开头,紧跟在开头的 <?php 声明之后.还要确保在开始的 <?php 声明之前没有空格/制表符.
  2. header 重定向之后,使用 exit(); 结束当前脚本(其他人也建议使用 session_write_close();>session_regenerate_id(true),你也可以尝试这些,但我会使用 exit();)
  3. 确保在您用来测试的浏览器中启用了 cookie.
  4. 确保 register_globals 已关闭,您可以在 php.ini 文件中检查这一点,也可以使用 phpinfo().请参阅this 了解如何转关闭.
  5. 确保您没有删除或清空会话
  6. 确保 $_SESSION 超全局数组中的键没有在任何地方被覆盖
  7. 确保重定向到同一个域.因此,从 www.yourdomain.com 重定向到 yourdomain.com 不会将会话向前推进.
  8. 确保您的文件扩展名是 .php(它会发生!)
  1. Make sure session_start(); is called before any sessions are being called. So a safe bet would be to put it at the beginning of your page, immediately after the opening <?php declaration before anything else. Also ensure there are no whitespaces/tabs before the opening <?php declaration.
  2. After the header redirect, end the current script using exit(); (Others have also suggested session_write_close(); and session_regenerate_id(true), you can try those as well, but I'd use exit();)
  3. Make sure cookies are enabled in the browser you are using to test it on.
  4. Ensure register_globals is off, you can check this on the php.ini file and also using phpinfo(). Refer to this as to how to turn it off.
  5. Make sure you didn't delete or empty the session
  6. Make sure the key in your $_SESSION superglobal array is not overwritten anywhere
  7. Make sure you redirect to the same domain. So redirecting from a www.yourdomain.com to yourdomain.com doesn't carry the session forward.
  8. Make sure your file extension is .php (it happens!)

现在,这些是最常见的错误,但如果他们没有解决问题,则问题很可能与您的托管公司有关.如果在 localhost 上一切正常,但在你的远程/测试服务器上却没有,那么这很可能是罪魁祸首.因此,请检查您的托管服务提供商的知识库(也可以尝试他们的论坛等).对于 FatCow 和 iPage 等公司,它们要求您指定 session_save_path.就像这样:

Now, these are the most common mistakes, but if they didn't do the trick, the problem is most likely to do with your hosting company. If everything works on localhost but not on your remote/testing server, then this is most likely the culprit. So check the knowledge base of your hosting provider (also try their forums etc). For companies like FatCow and iPage, they require you to specify session_save_path. So like this:

session_save_path('"your home directory path"/cgi-bin/tmp');
session_start();

(将您的主目录路径"替换为您的实际主目录路径.这通常在您的控制面板(或等效的)中,但您也可以在您的根目录中创建一个 test.php 文件目录和类型:

(replace "your home directory path" with your actual home directory path. This is usually within your control panel (or equivalent), but you can also create a test.php file on your root directory and type:

<?php echo $_SERVER['SCRIPT_FILENAME']; ?>

'test.php' 之前的位是您的主目录路径.当然,请确保该文件夹确实存在于您的根目录中.(部分程序同步时不上传空文件夹)

The bit before 'test.php' is your home directory path. And of course, make sure that the folder actually exists within your root directory. (Some programs do not upload empty folders when synchronizing)

这篇关于重定向后 PHP 会话丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆