What is the correct way to store a session variable in PHP and retrieve it in another page?
To store a session variable in PHP, you can use the $_SESSION superglobal array and assign a value to a specific key. To retrieve the session variable in another page, you simply need to start the session and access the value stored in the $_SESSION array using the same key.
// Storing a session variable
session_start();
$_SESSION['username'] = 'JohnDoe';
// Retrieving the session variable in another page
session_start();
$username = $_SESSION['username'];
echo $username; // Output: JohnDoe
Keywords
Related Questions
- What are best practices for handling timestamps and Unix Time in PHP applications to ensure consistency and accuracy when interacting with other languages?
- What best practices should be followed when handling session_start() and header() functions in PHP scripts?
- What are common methods for protecting a PHP website from XSS and CSRF attacks?