How can PHP be used to keep users logged in until they manually log out or leave the site?
To keep users logged in until they manually log out or leave the site, we can use session cookies in PHP. When a user logs in, we can set a session cookie with a long expiration time. This cookie will be sent with each request, allowing the server to identify the user and keep them logged in.
// Start the session
session_start();
// Set a session cookie with a long expiration time
$session_duration = 3600 * 24 * 30; // 30 days
setcookie(session_name(), session_id(), time() + $session_duration, '/');
// Other login logic here
Related Questions
- Can the closing PHP tag be omitted at the end of a PHP script, and what is the recommended practice for this?
- What is the significance of using LAST_INSERT_ID() function in PHP for retrieving the last inserted ID?
- How can absolute paths be utilized in PHP include statements to prevent issues with file loading on different servers?