In this tutorial we will show you the solution of PHP session timeout default, by default, the session timeout is set to 24 minutes and it depend on configuration set in php.ini file.
We can change this as we want. In this article we learn about php session timeout custom and default. Let us see how to do this using codes.
Step By Step Guide On PHP Session Timeout Default :-
Session is used in php for login system. The for which user is inactive is checked by session timeout.
When a user login to a website session automatically created and is destroyed on time of logout.
But what if user forgot to logout and busy with some other work then it make sure that his data must be secured. So, we used timed session at that time.
Let us see default timeout code used in php and then we will understand custom also.
//Ending a php session after 24 minutes of inactivity $sessionExpireTime=24; if (isset($_SESSION['LASTTIMEACTIVITY']) && (time() - $_SESSION['LASTTIMEACTIVITY'] > ($sessionExpireTime*60))) { session_unset(); // unset session session_destroy(); // destroy session data } $_SESSION['lastTimeActivity'] = time(); // another example <?php //Start a new session here session_start(); //Check the session start time is set or not if(!isset($_SESSION['session_start'])) { //Set the session start time $_SESSION['session_start'] = time(); } //Check the session is expired or not if (isset($_SESSION['session_start']) && (time() - $_SESSION['session_start'] >600)) { //Unset the session here session_unset(); //Destroy the session here session_destroy(); echo "Sorry, Session is expired now.<br/>"; } else echo "Current session still exists here.<br/>"; ?>
- Here, as you see in first example we just show you that code with help of which you can set a timed session. The value or say time set here to 24 minutes.
- We can also set custom time there by changing the value from 24 to 40, 15 or as much we want.
- Another way is to change the check the configuration file that is php.ini file. Here, we have to change the value of 24 minutes that is given in seconds.
- Now, let us see our last example in which we use $_Session and time() function inside php to change the default session timeout.
- Here, as we see that we first start our session using session_start() and then check weather time is set to session or not, if not then it will set current time.
- At last, we check the session is expired or not. If the session is expired which is check using if statement then it will unset the session variable and then destroy the session using session_destroy() function.
- At last, it will print a line in which it shows the session is destroyed.
- If session is not destroyed then another line is executed in which it shows that the current session still exists. We hope that you understand the codes easily.
Conclusion :-
At last in conclusion, here we can say that with the help of this article we are able to understand how about php session timeout.
I hope this tutorial on PHP session timeout default helps you and the steps and method mentioned above are easy to follow and implement.