15

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.

TRiG
  • 10,148
  • 7
  • 57
  • 107
RatDon
  • 3,403
  • 8
  • 43
  • 85

7 Answers7

44

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.

TRiG
  • 10,148
  • 7
  • 57
  • 107
Jijo John
  • 1,375
  • 9
  • 21
  • 1
    though i'm including the class file in a page where the session already started... i've to use `session_start()` in every class the session is being used. else it's throwing an error. – RatDon Jan 31 '14 at 06:08
15

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

if (!isset($_SESSION))
  {
    session_start();
  }
James
  • 4,644
  • 5
  • 37
  • 48
5

https://www.php.net/manual/en/function.session-status.php

if (session_status() === PHP_SESSION_ACTIVE)
        session_destroy();
scr4bble
  • 192
  • 1
  • 15
2

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:

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

Roger Dueck
  • 615
  • 7
  • 16
1

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

if(!session_id()) {
    session_start();
}
AboElnouR
  • 281
  • 3
  • 14
0

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

?>
Padmanathan J
  • 4,614
  • 5
  • 37
  • 75
  • 1
    remove `@` from `@session_destroy();` it'll show the warning. – RatDon Aug 12 '13 at 13:58
  • 1
    it's not correct man. it's showing another notice `Notice: A session had already been started - ignoring session_start() in class.inc on line 2` as the file is included in each file after `session_start();`. so two `session_start();` will show a notice. – RatDon Aug 12 '13 at 14:07
0

You'll have to call session_start() before you call session_destroy();

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.

Alok Jha
  • 562
  • 7
  • 22