警告:session_destroy():试图破坏未初始化的会话 [英] Warning: session_destroy(): Trying to destroy uninitialized session

查看:89
本文介绍了警告:session_destroy():试图破坏未初始化的会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的class.inc文件:

my class.inc file:

<?php
class logout{
    public function logout(){
        $_SESSION = array();
        if (ini_get("session.use_cookies")) {
            $params = session_get_cookie_params();
            setcookie(session_name(), '', time() - 42000, $params['path'], $params['domain'], $params['secure'], $params["httponly"]);
        }
        session_destroy();
    }   
}

?>

我的注销使用的代码:

session_start();
require($path."include/class.inc");
if(!empty($_GET['logout'])){
    $object=new logout();
    $object->logout();
    $content='5;url='.$path.'index.php';
}

调用logout函数时,它将破坏会话,但显示警告:

When the logout function is called, it destroys the session, but shows the warning:

Warning: session_destroy(): Trying to destroy uninitialized session in class.inc on line 9

我无法进行故障排除,因为在class.incsession_destroy()之前会话没有被任何其他方式破坏.

I am unable to troubleshoot, as the session is not being destroyed by any other means before the session_destroy() of class.inc.

推荐答案

您必须在注销类中的注销函数顶部调用以下提到的函数.

You have to call the function mentioned below at the top your logout function in the logout class.

session_start();

添加以上功能,然后尝试一下.如果您没有在文件的顶部开始会话,它将引发诸如已发送标题",无法开始会话"之类的异常.

Add the above function and try it out. If you don’t start the session at the top of your file, it will throw exceptions like "headers already sent", "can’t start the session", etc.

这篇关于警告:session_destroy():试图破坏未初始化的会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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