How to tell if a session is active?
See edits to the original question; basically, PHP 5.4 and above now has a function called session_status()
to solve this problem!
"Expose session status via new function, session_status" (SVN Revision 315745)
If you need this functionality in a pre-PHP 5.4 version, see hakre's answer.
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();
}
Check if a PHP session is active;
session_status is available for PHP v5.4 and later, maybe that's why.
You can try with session_id :
session_id() returns the session id for the current session or the empty string ("") if there is no current session (no current session id exists).
Just what you need for PHP below 5.4 ! ;)
How can I check if a Express.js session is active?
I have managed to accomplish this using Redis. I have set a TTL(time-to-live) for each object so that if the user doesn't send a GET request to a certain route in some amount of time the object will be removed from Redis thus making the user "incative"
when others fetch their status.
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;
}
}
Related Topics
PHP Curl Curlopt_Ssl_Verifypeer Ignored
PHP Add Elements to Multidimensional Array with Array_Push
How to Add New Column to MySQL Table
Save Current Page as HTML to Server
How to Create a Database Using Pdo in PHP
Using PHP to Output an Mp4 Video
Passing an Array Using an HTML Form Hidden Element
Unique and Temporary File Names in PHP
How to Reduce the Image Size Without Losing Quality in PHP
PHP Get Start and End Date of a Week by Weeknumber
How to Replace Part of a String in PHP
How to Count Unique Visitors to My Site
Check If a Variable Is Undefined in PHP
PHP Technique to Query the Apns Feedback Server
Turn Off Warnings and Errors on PHP and MySQL