PHP Check If Session Is Started

In this tutorial we will see How To Check If Session Is Started in PHP. PHP session_status() Function and PHP session_id() Function can be used to check if session is already started or not.

PHP session_status() Function

PHP session_status() Function returns the current status of session. It is used to check the session.

In this example session_status() Function is used to check if session is started or not.

Note: This is ideal for PHP 5.4.0 or higher.

<?php
if (session_status() === PHP_SESSION_NONE) {
 session_start();
}
?>

In above code PHP_SESSION_NONE is a value which is returned by session_status() function, if session is enabled but it doesn't exists or it hasn't been started yet.

PHP session_id() Function

PHP session_id() Function sets or returns the id of the current session.

In second example, session_id() Function is used to check the id of session, this can tell if the session has been started or not.

Note: This works with PHP 5.4.0 or lower.

<?php
if(session_id() == ''){
  session_start();
}
?>

Another possible way to check the session, is by using this code.

<?php
if (!isset($_SESSION))
{
session_start();
}
?>

In this third example isset() function is used which checks whether a variable is set or not.

Session is started with the session_start() function.

How To Append a String in PHP How To Prepend a String in PHP Replace a Word In String in PHP Convert Special Characters To HTML Entities PHP Find Number of Words in a String in PHP How To Find String Length in PHP How To Convert String To Uppercase in PHP How To Convert String To Lowercase in PHP How to Combine Two Strings in PHP Delete Array Elements By Value in PHP Get First Element Of Array In PHP Remove Numbers From String in PHP Convert Timestamp To Date in PHP Convert a Date to Timestamp in PHP Get Current Page Slug in PHP Check If Session Is Started in PHP Split Sentence Into Words in PHP Remove Last Character From String in PHP PHP Remove Spaces From String Replace Space With Dash in PHP Remove Everything from String Except Numbers Using PHP Get Full Url Address Using PHP Automatic Copyright Year Using PHP Generate 4 Digit Random Number in PHP How To Check If a Number is Prime using PHP Convert Integer to String in PHP