Check if PHP session has already started
Recommended way for versions of PHP >= 5.4.0 , PHP 7, PHP 8
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
Reference: http://www.php.net/manual/en/function.session-status.php
For versions of PHP < 5.4.0
if(session_id() == '') {
session_start();
}
Load session_start() only if session does not exist?
isset
is generally the proper way to check if predefined variables are currently defined:
If you are using a version of php prior to 5.4,
you can usually get away with and its tempting just doing the code below, but be aware that if for some reason sessions are disabled, session_start() will still be called which could end up leading to errors and needless debugging since the $_SESSION array isn't allowed to be created.
if(!isset($_SESSION)){session_start();}
if you want to account for checking to see if sessions are disabled for some reason, you can supress the error message, set a test session variable, and then verify it was set. If its not, you can code in a reaction for disabled sessions. You'll want to do all this at or near the start of your script. So as an example(works differently depending on error handling setting):
if(!isset($_SESSION)){session_start();}
$_SESSION['valid'] = 'valid';
if($_SESSION['valid'] != 'valid')
{
//handle disabled sessions
}
However, if you are using php version 5.4 or greater,
You can use the session_status() function, a better option as it accounts for sessions being disabled and checks if a session already exists.
if (session_status() === PHP_SESSION_NONE){session_start();}
NOTE that PHP_SESSION_NONE is a constant set by PHP and therefore does not need to be wrapped in quotes. It evaluates to integer 1, but as its a constant that was specifically set to eliminate the need for magic numbers, best to test against the constant.
PHP sessions that have already been started
Try
<?php
if(!isset($_SESSION))
{
session_start();
}
?>
PHP session has already been started
You should check the session status with session_status()
function to avoid repeated session start. For example:
if (session_status() == PHP_SESSION_NONE) {
session_start();
}
Check if session exists in php
Use isset
function
function ifsessionExists(){
// check if session exists?
if(isset($_SESSION)){
return true;
}else{
return false;
}
}
You can also use empty
function
function ifsessionExists(){
// check if session exists?
if(!empty($_SESSION)){
return true;
}else{
return false;
}
}
How can you check if a PHP session exists?
In PHP versions prior to 5.4, you can just the session_id()
function:
$has_session = session_id() !== '';
In PHP version 5.4+, you can use session_status()
:
$has_session = session_status() == PHP_SESSION_ACTIVE;
Detect if PHP session exists
If $_SESSION
(or $HTTP_SESSION_VARS
for PHP 4.0.6 or less) is used, use isset()
to check a variable is registered in $_SESSION
.
isset($_SESSION['varname'])
How do check if a PHP session is empty?
I would use isset
and empty
:
session_start();
if(isset($_SESSION['blah']) && !empty($_SESSION['blah'])) {
echo 'Set and not empty, and no undefined index error!';
}
array_key_exists
is a nice alternative to using isset
to check for keys:
session_start();
if(array_key_exists('blah',$_SESSION) && !empty($_SESSION['blah'])) {
echo 'Set and not empty, and no undefined index error!';
}
Make sure you're calling session_start
before reading from or writing to the session array.
Related Topics
Asynchronous Shell Exec in PHP
How to Maintain a "Version For the Server" - With Only Config Files Changed, in Git
How to Rename Uploaded File Before Saving It into a Directory
How to Use Store and Use Session Variables Across Pages
Curl Error 60: Ssl Certificate: Unable to Get Local Issuer Certificate
Giving My Function Access to Outside Variable
Calling Closure Assigned to Object Property Directly
PHP Error: "Cannot Pass Parameter 2 by Reference"
PHP Ini File_Get_Contents External Url
Multiple Inputs With Same Name Through Post in PHP
Filter/Remove Rows Where Column Value Is Found More Than Once in a Multidimensional Array
When Should I Use Prepared Statements
How to Get a File Name from a Full Path With PHP
How to Get Id of the Last Updated Row in MySQL