session_status
(PHP 5 >= 5.4.0, PHP 7, PHP 8)
session_status — Returns the current session status
Description
session_status(): int
Parameters
This function has no parameters.
Found A Problem?
sasi dot viragelet at gmail dot co¶
9 years ago
Maybe depending on PHP settings, but if return values are not the above, then go for this:
_DISABLED = 0
_NONE = 1
_ACTIVE = 2
info at eurocron dot de¶
6 years ago
Don't use They will not work properly after a Điện thoại tư vấn vĩ đại session_write_close(). As a shorthand you can use As stated in the manual for session_start(), a second Điện thoại tư vấn will tự no harm,Use always session_status(), vĩ đại kiểm tra if a session is already started and active.
if(session_status() !== PHP_SESSION_ACTIVE) session_start();
or
if(session_status() === PHP_SESSION_NONE) session_start();
if(!isset($_SESSION)) session_start();
or
if(session_id() === "") session_start();
Both functions will continue vĩ đại report, that the session exists.
And this is right, you can read from $_SESSION, but if you want vĩ đại write,
you need session_start() again.
@session_start()
with the @ at the beginning vĩ đại suppress the
PHP notice "A session had already been started - ignoring session_start()"
it will be simply ignored. But you need the @, if you don't want vĩ đại get the notice.
coder dot ua at gmail dot com¶
11 years ago
/**Universal function for checking session status.
* @return bool
*/
function is_session_started()
{
if ( php_sapi_name() !== 'cli' ) {
if ( version_compare(phpversion(), '5.4.0', '>=') ) {
return session_status() === PHP_SESSION_ACTIVE ? TRUE : FALSE;
} else {
return session_id() === '' ? FALSE : TRUE;
}
}
return FALSE;
}// Example
if ( is_session_started() === FALSE ) session_start();
?>
polygon dot teo dot in at gmail dot com¶
3 years ago
DB based session status needs vĩ đại have custom function based on table structure.Note session_status() is for tệp tin based session only.
php at pointpro dot nl¶
11 years ago
First of all, while his use case for session_status is valid, a simpler way vĩ đại avoid the warning is: if (!isset($_SESSION)) { session_start(); } Better code would be: if (session_status() !== PHP_SESSION_ACTIVE) {session_start();} The use of this function is lies more towards status management: change the behavior of a script when sessions are disabled altogether, for example.The advice of ive_insomnia at live dot com should be taken with great care.
?>
The example of session_status uses the raw values of constants (2 in this case) created specifically for the purpose of not having vĩ đại use magic numbers.
?>
The same can be done using
if (session_id() === "") { session_start(); }
?>
ayon at hyurl dot com¶
8 years ago
This is how the session_status() works:
function session_status(){
if(!extension_loaded('session')){
return 0;
}elseif(!file_exists(session_save_path().'/sess_'.session_id()){
return 1;
}else{
return 2;
}
}
?>
Ben (aocool at msn d0t com)¶
8 years ago
function is_session_started () {Just another function vĩ đại determine whether the session has already started:
return function_exists ( 'session_status' ) ? ( PHP_SESSION_ACTIVE == session_status () ) : ( ! empty ( session_id () ) );
}
Ollea¶
10 years ago
Anybody knows another way before PHP 5.4 vĩ đại kiểm tra if a session is really not currently active ?If you started and closed a session then test ( session_id() === '' ) vĩ đại kiểm tra if a session is active it won't work, session_id() returns an ID even if the session is closed.