Check if session is started or not in php

Check if session is started or not in php

To avoid Cannot send session cookie - headers already sent warning which is caused by calling the session_start()function without checking if the session is already started or not.

You can find following warning in error log file :

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /xxx/xxx/abc.php:8) in /xxx/xxx/test.php on line 11

Recommended way for versions of PHP >= 5.4.0

 Instant Images if ( session_status() == PHP_SESSION_NONE ) {
    session_start();
}

Source: http://www.php.net/manual/en/function.session-status.php

For versions of PHP < 5.4.0

if ( session_id() == '' ) {
    session_start();
}


Leave a Reply

Your email address will not be published. Required fields are marked *