php 重定向后PHP会话丢失
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17242346/
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
PHP session lost after redirect
提问by dayuloli
How do I resolve the problem of losing a session after a redirect in PHP?
如何解决在 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 thiscame the closest).
最近遇到一个很常见的问题,redirect后session丢失。在通过这个网站搜索后,我仍然找不到解决方案(尽管这是最接近的)。
Update
更新
I have found the answer and I thought I'd post it here to help anyone experiencing the same problem.
我找到了答案,我想我会在这里发布它以帮助遇到相同问题的任何人。
回答by dayuloli
First, carry out these usual checks:
首先,执行这些常规检查:
- 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. - After the
header
redirect, end the current script usingexit();
(Others have also suggestedsession_write_close();
andsession_regenerate_id(true)
, you can try those as well, but I'd useexit();
) - Make sure cookies are enabled in the browser you are using to test it on.
- Ensure
register_globals
is off, you can check this on thephp.ini
file and also usingphpinfo()
. Refer to thisas to how to turn it off. - Make sure you didn't delete or empty the session
- Make sure the key in your
$_SESSION
superglobal array is not overwritten anywhere - Make sure you redirect to the same domain. So redirecting from a
www.yourdomain.com
toyourdomain.com
doesn't carry the session forward. - Make sure your file extension is
.php
(it happens!)
- 确保
session_start();
在调用任何会话之前调用。所以一个安全的赌注是把它放在你的页面的开头,紧跟在<?php
其他任何东西之前的开头声明之后。还要确保在开始<?php
声明之前没有空格/制表符。 - 在后
header
重定向,使用结束当前的脚本exit();
(其他人也建议session_write_close();
,并session_regenerate_id(true)
,你可以尝试那些为好,但我会用exit();
) - 确保在您用来测试的浏览器中启用了 cookie。
- 确保
register_globals
已关闭,您可以在php.ini
文件上进行检查,也可以使用phpinfo()
. 请参阅本关于如何将其关闭。 - 确保您没有删除或清空会话
- 确保您的
$_SESSION
超全局数组中的键没有在任何地方被覆盖 - 确保重定向到同一个域。因此从 a 重定向
www.yourdomain.com
到yourdomain.com
不会将会话向前推进。 - 确保您的文件扩展名是
.php
(它发生了!)
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:
现在,这些是最常见的错误,但如果他们没有解决问题,则问题很可能与您的托管公司有关。如果一切正常,localhost
但不在您的远程/测试服务器上,那么这很可能是罪魁祸首。因此,请检查您的托管服务提供商的知识库(也可以尝试他们的论坛等)。对于 FatCow 和 iPage 等公司,他们要求您指定session_save_path
. 所以像这样:
session_save_path('"your home directory path"/cgi-bin/tmp');
session_start();
(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:
(将“您的主目录路径”替换为您的实际主目录路径。这通常在您的控制面板(或等效的)中,但您也可以test.php
在您的根目录中创建一个文件并键入:
<?php echo $_SERVER['SCRIPT_FILENAME']; ?>
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)
'test.php' 之前的位是您的主目录路径。当然,请确保该文件夹确实存在于您的根目录中。(部分程序同步时不上传空文件夹)
回答by KraftART Berlin
you should use "exit" after header-call
您应该在标头调用后使用“退出”
header('Location: http://www.example.com/?blabla=blubb');
exit;
回答by ali al-juanidi
I tried all possible solutions, but none worked for me! Of course, I am using a shared hosting service.
我尝试了所有可能的解决方案,但没有一个对我有用!当然,我使用的是共享托管服务。
In the end, I got around the problem by using 'relative url' inside the redirecting header !
最后,我通过在重定向标头中使用“相对 url”解决了这个问题!
header("location: http://example.com/index.php")
nullified the session cookies
使会话 cookie 无效
header("location: index.php")
worked like a charm !
像魅力一样工作!
回答by Jeremie
I had the same problem. I worked on it for several hours and it drove me crazy.
我有同样的问题。我为此工作了几个小时,这让我发疯了。
In my case the problem was a 404 called due to a missing favicon.icoin Chrome and Firefox only. The other navigators worked fine.
在我的情况下,问题是 404 调用,因为仅在 Chrome 和 Firefox 中缺少 favicon.ico。其他导航员工作正常。
回答by user2999967
When i use relative path "dir/file.php" with in the header() function in works for me. I think that the session is not saved for some reason when you redirect using the full url...
当我在 header() 函数中使用相对路径“dir/file.php”时对我有用。我认为当您使用完整的 url 重定向时,由于某种原因没有保存会话......
//Does retain the session info for some reason
header("Location: dir");
//Does not retain the session for some reason
header("Location: https://mywebz.com/dir")
回答by NicB
I ran into this issue on one particular page. I was setting $_SESSION values in other pages right before redirecting and everything was working fine. But this particular page was not working.
我在一个特定的页面上遇到了这个问题。在重定向之前,我正在其他页面中设置 $_SESSION 值,一切正常。但是这个特定的页面不起作用。
Finally I realized that in this particular page, I was destroying the session at the beginning of the page but never starting it again. So my destroy function changed from:
最后我意识到在这个特定的页面中,我在页面开头破坏了会话,但再也没有启动它。所以我的销毁功能从:
function sessionKill(){
session_destroy();
}
to:
到:
function sessionKill(){
session_destroy();
session_start();
}
And everything worked!
一切都奏效了!
回答by Wynn
I was having the same problem. All of a sudden SOME of my session variables would not persist to the next page. Problem turned out to be ( in php7.1) you header location must not have WWW in it, ex https://mysite. is ok, https://www.mysite. will lose that pages session variables. Not all, just that page.
我遇到了同样的问题。突然之间,我的一些会话变量不会持续到下一页。问题原来是(在 php7.1 中)您的标题位置中不能包含 WWW,例如https://mysite。没问题,https://www.mysite。将丢失该页面会话变量。不是全部,只是那一页。
回答by Stephen K. Karanja
I had a similar problem, although my context was slightly different.
I had a local development setup on a machine whose hostname was windows
and IP address was 192.168.56.2
.
我遇到了类似的问题,尽管我的上下文略有不同。我在主机名windows
和 IP 地址为192.168.56.2
.
I could access the system using either of:
我可以使用以下任一方法访问系统:
After logging in, my PHP code would redirect using:
登录后,我的 PHP 代码将使用以下方法重定向:
header('http://windows/');
If the previous domain name used to access the system was not windows
, the session data would be lost. I solved this by changing the code to:
如果之前用于访问系统的域名不是windows
,则会话数据将丢失。我通过将代码更改为:
header('http://'.$_SERVER['HTTP_HOST'].'/');
It now works regardless of what local domain name or IP address the user puts in.
无论用户输入什么本地域名或 IP 地址,它现在都可以工作。
I hope this may be useful to someone.
我希望这可能对某人有用。
回答by sclarky
This stumped me for a long time (and this post was great to find!) but for anyone else who still can't get sessions between page redirects to work...I had to go into the php.ini file and turn cookies on:
这让我难住了很长时间(这篇文章很好找!)但是对于仍然无法在页面重定向之间进行会话的其他人来说......我不得不进入 php.ini 文件并打开 cookie :
session.use_cookies = 1
I thought sessions worked without cookies...in fact I know they SHOULD...but this fixed my problem at least until I can understand what may be going on in the bigger picture.
我认为会话在没有 cookie 的情况下也能工作……事实上我知道他们应该……但这至少解决了我的问题,直到我能理解大局中可能发生的事情。
回答by Gramrock
I had the same problem and found the easiest way. I simply redirected to a redirect .html with 1 line of JS
我遇到了同样的问题,找到了最简单的方法。我只是重定向到带有 1 行 JS 的重定向 .html
<!DOCTYPE html>
<html>
<script type="text/javascript">
<!--
window.location = "admin_index.php";
//–>
</script>
</html>
instead of PHP
而不是 PHP
header_remove();
header('Location: admin_login.php');
die;
I hope this helps.
我希望这有帮助。
Love Gram
爱克