What are the basic principles of using sessions in PHP and how can variables be accessed across different pages?
To use sessions in PHP, you need to start the session using session_start() at the beginning of each page where you want to access session variables. Session variables can be set and accessed using the $_SESSION superglobal array. This allows you to store and retrieve data across different pages for a specific user session.
<?php
// Start the session
session_start();
// Set a session variable
$_SESSION['username'] = 'JohnDoe';
// Access the session variable on another page
session_start();
echo $_SESSION['username'];
?>
Related Questions
- What is the significance of using the PHP code snippet provided to handle HTTPS form submission in the forum thread?
- What is the correct way to pass the value of a checkbox in PHP when submitting a form?
- What could be causing the "Uncaught Error: Call to undefined function mysqli_connect()" error in PHP?