PHP session destroy Trying to destroy uninitialized session

后端 未结 3 1243
不知归路
不知归路 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 01:07

    You're calling session_destroy() twice.

    If your cookie isn't set, then it won't equal $_SESSION['email'] will it?

    Change your code to:

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

提交回复
热议问题