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

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

问题描述

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

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

最近,我在重定向后遇到了一个非常普遍的丢失会话的问题.在搜索了该网站之后,我仍然找不到任何解决方案(尽管来了最近的).

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()进行检查.请参阅,以了解如何打开它关闭.
  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天全站免登陆