Home>Article>Backend Development> Detailed explanation of how PHP sessions are destroyed after 30 minutes (with code examples)
This article will introduce you to the issue of how to specify the time to destroy the PHP session. Here is a detailed introduction to how to destroy the session through the session_destroy() function. I hope it will be helpful to friends in need~
PHP has a core function session_destroy() to clear all session values. It is a simple function with no parameters that returns a boolean value of true or false.
PHP's session ID is stored in a cookie by default. Generally, the name of the session cookie file is PHPSESSID. The session_destroy function will not cancel the sessionid in the cookie.
In order to "completely" destroy the session, the session ID must also be unset.
This quick example uses session_destroy() to destroy the session. It uses the set_cookie() method to kill the entire session with an expired PHP session ID.
destroy-session.php
Note: Use session_start() to restart a PHP session after it is destroyed. Use PHP$_SESSION to unset specific session variables. For older PHP versions, use session_unset(). PHP session destruction output [Recommended learning:PHP video tutorial]
About this login session_destory() example
Let us create a login sample code to use PHP session, session_destroy etc. It allows users to log in and out from the current session. If you are looking for complete user registration and login in PHP script then use this code. This sample provides automatic login session expiration functionality.
Login page with login form
This form posts the username and password entered by the user. It verifies login credentials in PHP. After a successful login, it stores the login status into the PHP session. It sets the expiration time to 30 minutes from the last login time. It stores the last login time and expiration time into the PHP session. These two session variables are used to automatically expire the session.
login.php
PHP Session Destroy after 30 Minutes Login
Dashboard authenticates PHP login session and displays login and logout links
This is the target page for redirection after login. If a login session exists, it will display a logout link. Once it times out it will call destroy session. php code to destroy all sessions. If the 30 minute expiration time is reached or the session is empty, it will ask the user to log in.
home.php
PHP Session Destroy after 30 Minutes This PHP code is for users who wish to log out before their session expires.
It destroys the session by asking it to be destroyed. php code. It then redirects the user to the login page. logout.php
I hope this example helps understand how to destroy a PHP session. And, this is a perfect scenario to explain the need to destroy sessions.
This article is reprinted, original address: https://juejin.cn/post/7164391542164520990
The above is the detailed content of Detailed explanation of how PHP sessions are destroyed after 30 minutes (with code examples). For more information, please follow other related articles on the PHP Chinese website!