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
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With