PHP session destroy Trying to destroy uninitialized session

后端 未结 3 1291
不知归路
不知归路 2021-01-29 00:10

I\'ve read several topics like:

Error — session_destroy() — Trying to destroy uninitialized session, Warning: session_destroy(): Trying to destroy uninitialized session

3条回答
  •  没有蜡笔的小新
    2021-01-29 00:52

    The problem is that you call session destroy twice. If $_COOKIE['data'] is not set, then $_COOKIE['data'] != sha1($_SESSION['email']) will return false as well and it will try to destroy the session again.

        if(!isset($_COOKIE['data'])){
            session_destroy();
            $this->error_404();
        }
    
        if($_COOKIE['data'] != sha1($_SESSION['email'])){
            session_destroy();
            unset($_COOKIE["data"]);
            setcookie("data", false, time() - 3600, '/');
            $this->error_404();
        }
    

    Make the checks on in another

         if($_COOKIE['data'] != sha1($_SESSION['email'])){
            if(!isset($_COOKIE['data'])){
            session_destroy();
            $this->error_404();
            }
            else
            {
            unset($_COOKIE["data"]);
            setcookie("data", false, time() - 3600, '/');
            session_destroy();
            $this->error_404();
            }
        }
    

    If the cookie data is not valid, it may be because there is no cookie. This way, if it's not valid, it checks if it exists. If it does exist and it's not valid, it does something. If it doesn't, it does something else.

提交回复
热议问题