How to Tell If a Session Is Active

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



Leave a reply



Submit