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 namedusername
.- The value can be accessed throughout the session’s lifetime until it is destroyed or the browser is closed.