How to use sessions in PHP?

Introduction:
Sessions in PHP store user data across multiple pages. A session is initiated using session_start() and stores data in a global $_SESSION superglobal array.

Example:

<?php
session_start(); // Start the session

$_SESSION['username'] = "JohnDoe"; // Set session variable
echo "Session is set for username: " . $_SESSION['username'];
?>

Explanation:

  • session_start() initializes a session or resumes an existing one.
  • $_SESSION['username'] assigns a value "JohnDoe" to a session variable named username.
  • The value can be accessed throughout the session’s lifetime until it is destroyed or the browser is closed.

Leave a Reply

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