// start the session session_start(); // set session data $_SESSION['username'] = 'alice'; $_SESSION['email'] = 'alice@example.com'; // retrieve session data $username = $_SESSION['username']; $email = $_SESSION['email'];
// start the session session_start(); // check if user is logged in if(isset($_SESSION['username'])){ // user is logged in $username = $_SESSION['username']; } else { // user is not logged in header("Location: login.php"); // redirect to login page }
use Symfony\Component\HttpFoundation\Session\Session; // create a new session $session = new Session(); // set the session lifetime to 30 minutes $session->setMaxIdleTime(1800); // start the session $session->start();Note: Symfony Session component is the package library used in the third example.