Home  >  Article  >  Backend Development  >  How to set session life cycle in php?

How to set session life cycle in php?

青灯夜游
青灯夜游Original
2020-07-22 13:32:063243browse

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.

How to set session life cycle in php?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn