PHP Session Expire After 5 Minutes
Last Updated : Mar 11, 2024
IN - PHP | Written & Updated By - Pragati

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">
<head>
<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">
</head>
<body>
<?php
// session starts here
session_start();
$expireAfter = 5;
if(isset($_SESSION['last_action'])){
$inactiveTime = time() - $_SESSION['last_action'];
$expireAfterSeconds = $expireAfter * 60;
if($inactiveTime >= $expireAfterSeconds){
session_unset();
session_destroy();
}
}
$_SESSION['last_action'] = time();
</body>
</html>
- First, we write <! DOCTYPE html> which we used as an instruction to the web browser about what version of HTML file is written in.
- Secondly, the <html> tag is used to indicate the beginning of an HTML document.
- 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.
- 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.
- 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.
- One thing to note here that to expire session after a particular time, we have to set that manually.
- 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.
- Then we convert the time we get into seconds.
- If our current time exceeding then the specified time then with the use of if statement our session got unset and then destroyed.
- Unset is used to empty session whereas destroy is used to destroy the session.
- Here, one thing more that we are able to expire session after specific time with the help of this code.
- 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.
- 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.













