Wednesday 2 June 2021

Session (Native Php)

Maybe depending on PHP settings, but if return values are not the above, then go for this:
_DISABLED = 0
_NONE = 1
_ACTIVE = 2



Universal function for checking session status.

start session

<?php
/**
* @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();
?>


Unset

<?php
// remove all session variables
session_unset();


Destroy

// destroy the session
session_destroy();
?>


Notes

As a shorthand you can use
@session_start()
with the @ at the beginning to suppress the
PHP notice "A session had already been started - ignoring session_start()"

As stated in the manual for session_start(), a second call will do no harm,
it will be simply ignored. But you need the @, if you don't want to get the notice.

No comments:

Post a Comment