How can the $_SESSION superglobal array be used to store session data in PHP?
To store session data in PHP using the $_SESSION superglobal array, you can simply assign values to keys within the $_SESSION array. This data will persist across multiple pages as long as the session is active. To start a session, you need to call session_start() at the beginning of your script.
<?php
// Start the session
session_start();
// Store data in the session
$_SESSION['username'] = 'john_doe';
$_SESSION['email'] = 'john@example.com';
// Retrieve data from the session
$username = $_SESSION['username'];
$email = $_SESSION['email'];
// Output the stored data
echo "Username: $username <br>";
echo "Email: $email";
?>
Related questions
-
@ operator file_get_contents error suppression
-
static declarations compile-time resolution
-
In what situations should PHP developers consider using error handling functions like mysql_error()?PHP error handling mysql_error()