Home >Backend Development >PHP Problem >How to set session life cycle in php?
If you want to change the session life cycle, you can set the validity time of the session ID in the cookie. Method: 1. Use the setcookie() function to set the life cycle; 2. Use the session_set_cookie_params() function to set the life cycle.
In PHP, the Session variable is saved on the server side (saved in file format by default), and the Session ID is saved on the client side in the form of a cookie.
There are two ways to destroy a session
The first is to clear all sessions through the program
session_destory() method
unset(session['x']) to clear the specified session['x'].
The second is to close the browser
After closing, all sessions will be cleared directly.
When cookies are not disabled, the session ID is saved in the cookie.
If you want to change the session life cycle, you can do it by setting the validity time of the session ID in the cookie.
There are also 2 settings for the session life cycle. method.
The first one is setcookie()
$lifetime=60;//保存1分钟 session_start(); setcookie(session_name(),session_id(),time()+$lifetime,"/");
Directly use setcookie to set the life cycle of the session id.
The second session_set_cookie_params()
$lifetime=60;//保存1分钟 session_set_cookie_params($lifetime); session_start(); session_regenerate_id(true);
session_regenerate_id(); method is used to change the value of the current session_id and retain the value of the array in the session. The parameter defaults to false. If set to true, the value of session_id will be changed and the current session array will be cleared.
Related tutorial recommendations: "PHP Tutorial"
The above is the detailed content of How to set session life cycle in php?. For more information, please follow other related articles on the PHP Chinese website!