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 Remove Empty Array Elements with PHP How To Remove Common Values from two Arrays with PHP How To Find sum of array values with PHP How To Shuffle array Values in PHP How to Remove Duplicate Values from an array with PHP PHP Sort Associative array by Key PHP Sort Associative array by Value Sort array in numerical order with PHP Sort array in alphabetical order with PHP Sort array in descending order PHP Sort array in ascending order in PHP How to merge two or more arrays in PHP Add elements to the end of an array in PHP Add elements to the start of an array in PHP PHP Remove First Element from an Array PHP Remove Last Element from an Array Check if Key Exists in array PHP Check if Value exists in array PHP How to reverse an array in PHP PHP Print array line by line PHP Echo array values using foreach loop Count array values in PHP PHP Count How Many Times Word appears in String How to Reverse a String with PHP Check if Variable is Null in PHP