What is the correct way to set and retrieve session variables in PHP?
When setting and retrieving session variables in PHP, you need to start the session using session_start() before you can access or set any session variables. To set a session variable, you can simply assign a value to $_SESSION['variable_name']. To retrieve a session variable, you can access it using $_SESSION['variable_name'].
// Start the session
session_start();
// Set a session variable
$_SESSION['username'] = 'JohnDoe';
// Retrieve the session variable
$username = $_SESSION['username'];
echo $username; // Output: JohnDoe