There are cases when session_start();
returns true even if it can't actually start the session. One can see that from the error log:
PHP Warning: session_start():
open(/var/lib/php5/sessions/sess_XXXXXXX, O_RDWR) failed:
Permission denied (13)
I've seen this happen if a programmer thinks he can use the same session ID on different websites, but they actually use different privileges and can't access each other files. And I can't say this programmer, like, hey, check what session_start()
returns and act accordingly because he'd tell me he checks and it reports all good.
One can simulate this problem by removing write privileges from a specific session file:
chmod a-w /var/lib/php5/sessions/sess_XXXXXXX
And then staring a session with the same ID:
session_start(); // returns true with a warning in the logs
// if it'd return false, there would be no question
// PHP thinks session is started: session_status() == PHP_SESSION_ACTIVE
What is the proper way to deal with such errors in software? E.g. how do I know that I have this kind of error but not by looking in the logs?
So far I've come to this:
set_error_handler(function ($errno, $errstr) {
// we got an error during session_start()
if (strpos($errstr, 'session_start') === 0) {
// forget previus session ID
session_regenerate_id();
// restore the handler for now
set_error_handler(null);
}
return false;
}, E_WARNING);
if (!session_start()) {
// et cetera
}
// session shall be running now
// restore the error handler
set_error_handler(null);
(My inspiration comes from phpMyAdmin, which also does something similar.)