简体   繁体   English

为什么($ _SESSION = [];)使服务器无法处理请求?

[英]Why is ( $_SESSION = [];) making the server unable to handle the request?

I'm trying to clear the variables from a session and start a new one. 我正在尝试从会话中清除变量,然后开始一个新的会话。 From reading around about the variable issue, I think I discovered that this is the right answer: 通过阅读有关可变问题的信息,我认为我发现这是正确的答案:

( $_SESSION = [];)

But when I add that line to my code, on a line by itself within the PHP code, I get a message that "[the server] is currently unable to handle this request," indicating "HTTP ERROR 500." 但是,当我将该行添加到代码中时,在PHP代码本身的一行中,我收到一条消息,“ [服务器]当前无法处理此请求,”指示“ HTTP ERROR 500”。

Without that one command, the page works fine (except that the variables don't behave as I want them to). 如果没有该命令,该页面就可以正常工作(除了变量的行为不如我希望的那样)。 But every time I activate that line, I get the error. 但是每次我激活那条线,我都会得到错误。 How can I clear the variables without making the server angry? 如何清除变量而不使服务器生气?

Update: I have also tried session_destroy(), but it has the same effect. 更新:我也尝试了session_destroy(),但效果相同。 Here is a little more context: 这里是一些上下文:

<?php 
session_destroy();

session_set_cookie_params(3600,"/");
session_start();
?>

If I comment out the session_destroy() line, the page loads with no trouble. 如果我注释掉session_destroy()行,则页面加载不会有任何麻烦。 But with that line, I get the error. 但是有了这一行,我得到了错误。

Final update for others with this question: I've marked the correct answer. 有关此问题的其他人的最终更新:我已经标记了正确的答案。 I needed to start the new session before unsetting the variables from the last one. 在取消上一个会话的变量之前,我需要启动新会话。

You should not use session_destroy() before session_start(). 您不应该在session_start()之前使用session_destroy()。 Try this: 尝试这个:

session_start(); session_set_cookie_params(3600,"/"); session_destroy();

To remove all session variables use: 要删除所有会话变量,请使用:

session_unset(); 

if you are having issues after destroying your session, then it is likely that your code is trying to access session variables that have not been set. 如果销毁会话后遇到问题,则代码很可能试图访问尚未设置的会话变量。 I like to check with isset() eg 我喜欢用isset()检查,例如

$myVar = "";
if (isset($_SESSION('MyVar')) $MyVar = $_SESSION('MyVar');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM