The Note You're Voting On
marco dot agnoli at me dot com ¶ 9 years ago
I recently made an interesting observation:
It seems that `session_start()` can return `true` even if the session was not properly created. In my case, the disk storage was full and so the session data could not be written to disk. I had some logic that resulted in an infinite loop when the session was not written to disk.
To check if the session really was saved to disk I used:
```
<?php
function safe_session_start() {
# Attempt to start a session
if (!@\session_start()) return false;
#
# Check if we need to perform
# the write test.
#
if (!isset($_SESSION['__validated'])) {
$_SESSION['__validated'] = 1;
# Attempt to write session to disk
@\session_write_close();
# Unset the variable from memory.
# This step may be unnecessary
unset($_SESSION['__validated']);
# Re-start session
@\session_start();
# Check if variable value is retained
if (!isset($_SESSION['__validated'])) {
# Session was not written to disk
return false;
}
}
return true;
}
if (!safe_session_start()) {
# Sessions are probably not written to disk...
# Handle error accordingly.
}
?>
```
Took me quite a while to figure this out.
Maybe it helps someone!