Home > Backend Development > PHP Tutorial > How Can I Implement PHP Session Timeout Based on User Inactivity?

How Can I Implement PHP Session Timeout Based on User Inactivity?

Patricia Arquette
Release: 2024-12-02 12:19:10
Original
304 people have browsed it

How Can I Implement PHP Session Timeout Based on User Inactivity?

PHP Session Timeout

As you're creating a session during user login, you can set a timeout duration for inactivity and perform a specified action upon its expiration. Here's how to achieve this:

Last Request Timestamp

Store the timestamp of the user's last request in the session variable on each request:

$_SESSION['timeout'] = time();
Copy after login

Session Expiry Check

On subsequent requests, you can check how long ago the previous request was made. For instance, if you want to timeout the session after 10 minutes of inactivity:

if ($_SESSION['timeout'] + 10 * 60 < time()) {
    // Session timed out
    // Perform desired action (e.g., function execution or page redirect)
} else {
    // Session is active
}
Copy after login

Remember that the session will time out after 10 minutes of inactivity, and you can customize this duration as per your requirements.

The above is the detailed content of How Can I Implement PHP Session Timeout Based on User Inactivity?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template