php 为什么Session对象销毁失败

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

Why Session object destruction failed

phpsession

提问by Mahks

I get "Session object destruction failed" when I use session_destroy().

使用 session_destroy() 时出现“会话对象销毁失败”。

session_start();
if(isset($_SESSION['user_id'])){    
    $_SESSION=array();
    if(isset($_COOKIE[session_name()])){
        setcookie(session_name(),'',0,"/");
    }
    session_destroy();
}

What causes this error?

是什么导致了这个错误?

回答by hakre

Error:

错误:

Warning: session_destroy(): Session object destruction failed

警告:session_destroy():会话对象销毁失败

It's rather trivial, no session has been startedobject has been comitted, so you can't destroy it.

这是相当微不足道的,没有会话已启动对象已提交,因此您无法销毁它。

The @operator is not always active, e.g. with error reporting functions.

@运营商并不总是有效的,例如用错误报告功能。

Edit:

编辑:

1) What causes this error?

1)是什么导致了这个错误?

This error is normally caused when PHP tries to delete the session file, but it can't find it.

此错误通常是由于 PHP 尝试删除会话文件但找不到它时导致的。

In your case with session_destroythere is only one place in PHPwhich causes this. That's when the session.save_handler(see as well session_set_save_handler) returns FALSEfor the destroyaction. This can depends which type of save-handler you use, the default one is files. With that one, when the session.save_pathsetting is wrong (e.g. not an accessible directory), this would cause such an error.

在您的情况下,PHP 中session_destroy只有一个地方会导致这种情况。那时session.save_handler(也请参阅 session_set_save_handlerFALSE销毁操作返回。这取决于您使用哪种类型的保存处理程序,默认为files。有了那个,当session.save_path设置错误时(例如不是可访问的目录),这将导致这样的错误。

2) Why would the "@" not be suppressing the error?

2)为什么“@”不会抑制错误?

That depends how the output is created and on PHP configuration. @does not always work. For example callbacks registered with set_error_handlerwill still receive these messages.

这取决于输出的创建方式和 PHP 配置。@并不总是有效。例如注册的回调set_error_handler仍然会收到这些消息。

回答by Adam Bubela

In my case I was trying to destroy session before cookie was created. In other words I did something like:

就我而言,我试图在创建 cookie 之前销毁会话。换句话说,我做了类似的事情:

session_start();
...
session_destroy();

So the server didn't have a chance to 'contact' with the browser before destroying the session. Simple solution that worked for me was

因此,服务器在销毁会话之前没有机会与浏览器“联系”。对我有用的简单解决方案是

session_start();
...
$_SESSION=array();

回答by dbagnara

If you are using an autoloader, it may be failing to load a class that is saved in the session.

如果您使用的是自动加载器,则可能无法加载会话中保存的类。