All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

PHP Session Expire After 5 Minutes

Last Updated : Mar 11, 2024

PHP Session Expire After 5 Minutes

In this tutorial we will show you the solution of PHP session expire after 5 minutes, today we are going to understand how to expire session in php after 5 minutes.

With the help of this article, we are not able to understand only expiration of session after 5 minutes, but also understand how to expire session after specific time. So, let us understand this article today.

Step By Step Guide On PHP Session Expire After 5 Minutes :-

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title> php session expire after 5 minutes </title>
    <link rel="stylesheet" href="style.css">
// session starts here
$expireAfter = 5;
    $inactiveTime = time() - $_SESSION['last_action'];
    $expireAfterSeconds = $expireAfter * 60;
        if($inactiveTime >= $expireAfterSeconds){
$_SESSION['last_action'] = time();
  1. First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. As above now <head> tag is used to contain information about web page. In this tag a <title> tag is used which helps us to specify a webpage title. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  4. Here, then we create a body tag. All the content which we want to show on browser’s screen or display is always written inside this codes.
  5. In body, here as you see that inside here we create basic php tags. And inside these we start our session using session_start() and then we set time inside variable to expire our session.
  6. One thing to note here that to expire session after a particular time, we have to set that manually.
  7. Here as you can see that we use time () here, that is used to get the current table and we store the difference between that time inside a variable named as inactiveTime.
  8. Then we convert the time we get into seconds.
  9. If our current time exceeding then the specified time then with the use of if statement our session got unset and then destroyed.
  10. Unset is used to empty session whereas destroy is used to destroy the session.
  11. Here, one thing more that we are able to expire session after specific time with the help of this code.
  12. Here, in this article we use example of 5 minutes, according to user requirement we are also able to change time manually. We hope that you understand the code given above using these steps.
  13. At last, the <body> and <html> tags are closed with </body> and </html> respectively.

Conclusion :-

At last in conclusion, here we can say that with the help of this article we are able to understand how to expire session in php after 5 minutes.

I hope this tutorial on PHP session expire after 5 minutes helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪