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

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

Warning: session_destroy(): Trying to destroy uninitialized session

phpsession

提问by RatDon

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 logoutfunction 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.

我无法排除故障,如会话没有被任何其他方式破坏之前session_destroy()class.inc

回答by Jijo John

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”等异常。

回答by James

This error is common when you haven't started the session beforehand

当您没有事先启动会话时,此错误很常见

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

回答by Roger Dueck

I encountered the session_destroy()error message when I started using session_write_close(). To determine if session_destroy()should be called or not, I had to do the following:

我遇到的session_destroy()错误消息,当我开始使用session_write_close()。要确定是否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 (self::$haveSession)if(session_status() === PHP_SESSION_ACTIVE).

回答by AboElnouR

You can add this code to start a session if it didn't start before

如果之前未启动,您可以添加此代码以启动会话

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

回答by Nathan Srivi

Start your session session_start();and you can destroy your session

开始你的会话 session_start();,你可以销毁你的会话

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

?>

回答by Alok Jha

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 对应的记录,这样您就不必取消注册每个会话变量,它实际上会删除整个会话。