I am having an issue with session_start()
. It is creating a new session at every refresh/load of the page.
Here is the code of the complete script executed:
// Stuff added to see why a new session id is created.
ini_set(display_errors, 1);
error_reporting(E_ALL);
$session_dir = session_save_path();
$last_session_id = file_get_contents("$session_dir/last_session_id.txt");
$message = "Last session id : $last_session_id. -- ";
if(!is_writable($session_dir)) $message .= "Directory $session_dir is not writable. --";
if(headers_sent($file, $line)) $message .= "Headers already sent at $line in $file. --";
// The code just start a session and output a message to see what is going on.
if (!session_start()) {
$message .= "Failed to start the session. --";
}
else
{
$message .= "New session id : " . session_id() . "<br>";
file_put_contents("$session_dir/last_session_id.txt", session_id());
}
echo $message; // Last session id : hh3isj5cc1b964itlgek89vm13. -- New session id : 6hss2s8v340n5qad0smbmmlfm3
There is no warning or notice, nothing, just the message showing a different new session id each time, on a local server with only me executing this script. This is a duplicate of session_start() creates new session every refresh , session_id() creates new session every reload and PHP creating new session with each reload, but the answers do not apply. Here are the relevant php.ini settings:
session.save_handler = files
session.save_path = "/var/www/html/php_session"
session.use_strict_mode = Off
session.use_cookies = 1
session.cookie_secure = On
session.use_only_cookies = 1
session.name = PHPSESSID
session.auto_start = 0
session.cookie_lifetime = 0
session.cookie_path = /
session.cookie_domain =
session.cookie_httponly = On
session.serialize_handler = php