Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error while destroying session in PHP

Tags:

php

session

I am having problem in session handling in PHP(version 5.2.10). I am using the below mentioned functions for login, logout and validating sessions.

login()
{
    session_set_cookie_params(0);
    session_start();
    session_regenerate_id(true);
    $_SESSION['user_id']
}

validate_session()
{
    session_set_cookie_params(0);
    session_start();
    if (isset($_SESSION['user_id']) === FALSE) {
        session_destroy();
        logout();
        header("Location: login_page");
    }
}

logout()
{
    session_set_cookie_params(0);
    session_start();
    $_SESSION = array();
    setcookie(session_name(), '', time() - 3600, '/');
    session_destroy();
}

Every page first makes a call to validate_session() function. If session invalid it redirects to the login page. login() function is used for creating the session for the user. When user clicks logout, the logout() function is called to destroy the session.

The problem is: randomly the logout() function throws the warning:
Warning: session_destroy(): Session object destruction failed

I am getting this warning very infrequently. Like out of 20-30 calls to logout, I get it once. Any thoughts?

I am developing on a windows xp machine.

Update: The sessions are stored in file-system.
Path: C:\WINDOWS\Temp

like image 906
Varun Avatar asked Jul 01 '11 13:07

Varun


1 Answers

Is logout() called elsewhere than in validate_session() ? If not, the problem might be the call to session_destroy() before logout()

You could try this:

validate_session()
{
    session_set_cookie_params(0);
    session_start();
    if ( !isset( $_SESSION['user_id'] ) ) {
        logout();
        header("Location: login_page");
    }
}

logout()
{
    $_SESSION = array();
    setcookie(session_name(), '', time() - 3600, '/');
    session_destroy();
}
like image 163
Mudar Avatar answered Sep 30 '22 00:09

Mudar