简体   繁体   English

警告:session_destroy():试图销毁未初始化的会话

[英]Warning: session_destroy(): Trying to destroy uninitialized session

my class.inc file:我的 class.inc 文件:

<?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();
    }   
}

?>

used code for my logout:用于我的注销代码:

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

When the logout function is called, it destroys the session, but shows the warning:调用logout函数时,它会破坏会话,但会显示警告:

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

I am unable to troubleshoot, as the session is not being destroyed by any other means before the session_destroy() of class.inc .我无法进行故障排除,因为在class.incsession_destroy()之前没有通过任何其他方式破坏会话。

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.如果你没有在文件的顶部启动会话,它会抛出“headers already sent”、“can't start the session”等异常。

This error is common when you haven't started the session beforehand当您没有事先启动会话时,此错误很常见

if (!isset($_SESSION))
  {
    session_start();
  }

I encountered the session_destroy() error message when I started using session_write_close() .当我开始使用session_write_close()时,我遇到了session_destroy()错误消息。 To determine if session_destroy() should be called or not, I had to do the following:要确定是否应调用session_destroy() ,我必须执行以下操作:

class Session {
    public static function start() {
        self::$haveSession = true;
        session_start();
    }
    public static function finish() {
        session_write_close();
        self::$haveSession = false;
    }
    public static function clear() {
        if (self::$haveSession) {
            session_unset();
            session_destroy();
        }
    }
}

In PHP >= 5.4 it should work to replace if (self::$haveSession) with if(session_status() === PHP_SESSION_ACTIVE) .在 PHP >= 5.4 中,它应该可以用if(session_status() === PHP_SESSION_ACTIVE)替换if (self::$haveSession) if(session_status() === PHP_SESSION_ACTIVE)

You can add this code to start a session if it didn't start before如果之前未启动,您可以添加此代码以启动会话

if(!session_id()) {
    session_start();
}

Start your session session_start();开始你的会话session_start(); and you can destroy your session你可以破坏你的会话

    <?php
    session_start();
    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();
        }   
    }

?>

You'll have to call session_start() before you call session_destroy();你必须在调用 session_destroy() 之前调用 session_start();

Are you storing session in data in files or in a database.您是将会话存储在文件中还是数据库中的数据中。 If you are storing it in a database, I normally just delete the record from the session table that corresponds to the session id, that way you don't have to unregister each session var and it actually deletes the whole session.如果您将它存储在数据库中,我通常只是从会话表中删除与会话 ID 对应的记录,这样您就不必取消注册每个会话变量,它实际上会删除整个会话。

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

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