Skip to content
Advertisement

session_start() giving error for session_destroy() in PHP 8

All of a sudden, my custom session handlers’ session’s session_start() is not working. I had to include destroy after upgrading to PHP 8. It isn’t an issue in PHP 7.4.

private static function load()
{
    # session_module_name("user");
    session_set_save_handler(['CBSession', 'open'],
                             ['CBSession', 'close'],
                             ['CBSession', 'read'],
                             ['CBSession', 'write'],
                             ['CBSession', 'remove'],
                             ['CBSession', 'gc'],
                             ['CBSession', 'destroy']
                             );        
    
    session_start(); // Error here
}

public static function destroy($id)
{
    return TRUE;
}

Fatal error: Uncaught ArgumentCountError: Too few arguments to function CBSession::destroy(), 0 passed and exactly 1 expected in Session.php: Stack trace: #0 [internal function]: CBSession::destroy() #1 /path/CB/Session.php(35): session_start() #2 /path/CB/Session.php(17): CBSession::load()

Why is it talking about session_destroy for session_start (login) ? My logout is working.

EDIT : For some reason CBSession::destroy() is getting called before session_start()

Advertisement

Answer

According to the session_set_save_handler manual page the arguments are:

  1. $open
  2. $close
  3. $read
  4. $write
  5. $destroy
  6. $gc
  7. (optional) $create_sid
  8. (optional) $validate_sid
  9. (optional) $update_timestamp

The method names you have provided are:

  1. ‘open’
  2. ‘close’
  3. ‘read’
  4. ‘write’
  5. ‘remove’
  6. ‘gc’
  7. ‘destroy’

So the method called for the session “destroy” event is CBSession::remove, and the CBSession::destroy method is being called for the “create_sid” event. Since the create_sid callback is called without any arguments, this is giving you the error you’re seeing.

At the beginning of your question you said:

I had to include destroy after upgrading to PHP 8.

Since what you’ve actually included is a broken create_sid callback, whatever problem you thought you were solving may still need solving, but that would be a different question. The solution to your current error is to remove the erroneous ['CBSession', 'destroy'] line from your code.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement