PHP sessions that have already been started [duplicate]
In my PHP code, if a session has already started, and I try to start a new one, I get the following Notice:
Notice: A session had already been started - ignoring session_start()
How can I avoid this?
Try
<?php
if(!isset($_SESSION))
{
session_start();
}
?>
If you want a new one, then do session_destroy()
before starting it.
To check if its set before starting it, call session_status()
:
$status = session_status();
if($status == PHP_SESSION_NONE){
//There is no active session
session_start();
}else
if($status == PHP_SESSION_DISABLED){
//Sessions are not available
}else
if($status == PHP_SESSION_ACTIVE){
//Destroy current and start new one
session_destroy();
session_start();
}
I would avoid checking the global $_SESSION
instead of I am calling the session_status()
method since PHP implemented this function explicitly to:
Expose session status via new function, session_status This is for (PHP >=5.4.0)