PHP Tutorial

Define PHP

PHP Installation

PHP INI File

PHP Comment

PHP Case Sensitivity

PHP Variable, DataType

PHP Echo & Print

PHP Operators

PHP Receiving Input

Decision Making - if...else

PHP Switch Case

PHP Loops

PHP Jumping Statement

PHP Image Gallery

PHP File Upload

PHP Arrays

PHP Date Functions

PHP String Functions

PHP Math Functions

PHP Functions

PHP Variable Scope

PHP Constant Variable

PHP Superglobals

PHP Form Validation

PHP Include Statement

PHP Filter

PHP File Handling

PHP Cookies

PHP Session

PHP Send Emails

PHP Captcha

PHP MySQL Select

PHP MySQL Insert

PHP MySQL Delete

PHP MySQL Update

PHP MySQL Injection

PHP Assignment

Page Stats

Visitor: 382

PHP Session

PHP Session is use to store user's information which can be access across multiple pages. Unlike a cookie, the session information is not stored on the user's computer, A Session creates a temporary file where session variables and values are stored. The location of the temporary file is determined by a setting in the php.ini file called session.save_path. Normally, Session expires when the user logout from the account or user specified time. Default Session expires after 24 minutes.

A Session variable holds the information about one single user, and are available to all pages in entire application. To declare a session variable use the session_start() function. Session variables are set with the PHP global variable: $_SESSION['session-name'].

Example 1: Create a page using PHP session and set values in session variables.

<?php
// Start the session
session_start();
?>
<!DOCTYPE html>
<html>
<body>

    <?php
    // Set session variables
    $_SESSION["id"] = "yourmail@gmail.com";
    $_SESSION["age"] = "25";
    ?>

</body>
</html>

The session_start() function must be the very first line in your document. Before any HTML tags.

Retrive PHP Session Variable value

Now, we create another page "demo_session2.php". From this page, we will access session variable value that we have created on the "demo_session1.php" page.

Example 2: Retrive Session Variable Values

<?php session_start(); ?>
<!DOCTYPE html>
<html>
<body>

   <?php
   // echo session variables that were set on previous page
   echo "Email Id is " . $_SESSION["id"] . "<br>";
   echo "Age " . $_SESSION["age"];
   ?>

</body>
</html>

Example 3: Another way to show all the session variable values for a user session is:

<?php session_start(); ?>
<!DOCTYPE html>
<html>
<body>

   <?php
   print_r($_SESSION);
   ?>

</body>
</html>

Destroy a PHP Session

To remove all global session variables and destroy the session, use session_unset() and session_destroy()

Example 4: Destroy a PHP Session

<?php session_start(); ?>
<!DOCTYPE html>
<html>
<body>

   <?php
   // remove all session variables
   session_unset(); 
   
   // destroy the session 
   session_destroy(); 
   ?>

</body>
</html>

PHP Session - Timeout

Default Session timeout is 24 minutes, which is defined in PHP.ini file session.gc_maxlifetime.

Example 5: Change default session time.

<?php
// server should keep session data for AT LEAST 1 hour
ini_set('session.gc_maxlifetime', 3600);

// each client should remember their session id for EXACTLY 1 hour
session_set_cookie_params(3600); 
?<

Example 6: Change default session time.

<?php
    session_start();
    $_SESSION['auth'] = true;
    $_SESSION['start'] = time();
    $_SESSION['expire'] = $_SESSION['start'] + (60 * 60); //1 hour
Updated: 18-Oct-21