Home  >  Article  >  Backend Development  >  Set the session life cycle in php

Set the session life cycle in php

伊谢尔伦
伊谢尔伦Original
2016-11-26 14:27:201800browse

In PHP, the Session variable is saved on the server side (saved in file format by default), and the SessionID is saved on the client side in the form of a cookie.

There are two ways to destroy the session:

The first is through the program

session_destory()方法清除所有session
unset(session['x'])来清除指定的session['x']。

The second is by closing the browser

关闭后会直接清除所有session。

When cookies are not disabled, the session ID is saved in the cookie.

If you want to change the session life cycle, you can set the validity time of the session ID in the cookie. There are two ways to set the session life cycle:

The first one: 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.

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.


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