How can one implement a session with a time limit in PHP?

To implement a session with a time limit in PHP, you can set a timestamp when the session is started and then check if the current time exceeds the time limit. If it does, you can destroy the session and redirect the user to a login page.

<?php
session_start();

$session_time = 60; // 1 minute time limit for the session
if (isset($_SESSION['start_time']) && time() - $_SESSION['start_time'] > $session_time) {
    session_unset();
    session_destroy();
    header("Location: login.php");
    exit;
}

$_SESSION['start_time'] = time();
?>