Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Warning: session_destroy(): Trying to destroy uninitialized session

Tags:

php

session

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

?>

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:

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.

like image 599
RatDon Avatar asked Aug 12 '13 13:08

RatDon


2 Answers

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.

like image 152
Jijo John Avatar answered Nov 04 '22 22:11

Jijo John


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

if (!isset($_SESSION))
  {
    session_start();
  }
like image 15
James Avatar answered Nov 04 '22 22:11

James