Error killing session in PHP

I had a problem handling session in PHP (version 5.2.10). I use the following functions to log in, log out, and verify 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 ();
 }

Each page first calls the validate_session () function call. If the session is invalid, it is redirected to the login page. The login () function is used to create a session for the user. When the user clicks the logout button, the logout () function is called to destroy the session.

Problem: randomly, the logout () function throws a warning:
Warning: session_destroy (): Object object failed

I receive this warning very rarely. For example, from 20-30 calls to the exit, I get it once. Any thoughts?

I am developing on a Windows XP machine.

Update: Sessions are stored in the file system.
Path: C: \ WINDOWS \ Temp

+7
source share
3 answers

Is logout () called in a different place than validate_session ()? If not, the problem could be calling session_destroy () before logging out ()

You can try the following:

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

How do you keep your sessions? If this is a file, could it be a timeout or permission error?

In addition, I am wondering if the restoreate_id function calls the destroy function to find a session that does not technically exist there. Have you tried setting this boolean argument to false in the regeneration function?

We had this problem in the CakePHP application, but we corrected it by shaking the Cake settings.

0
source

Found something that might be useful on this topic. Main problems:

  • The session is valid for starters - what is the return value from session_start() ?
  • Are there session files in PHP.ini session.save_path and can be deleted.

I suspect this is the first in your case. I don’t remember where, but I think I saw a case where a session is invalid by itself, and then for some reason tried to repeat this process.

0
source

All Articles