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";
?>
Keywords
Related Questions
- What are some best practices for handling interactivity with HTML elements in PHP applications?
- When facing persistent connection drop issues with fsockopen in PHP, what steps can be taken to debug and resolve the underlying causes effectively?
- What potential pitfalls should be considered when implementing a preview functionality for user input in PHP?