Set the session life cycle in php

伊谢尔伦
Release: 2016-11-26 14:27:20
Original
1834 people have browsed it

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']。
Copy after login

The second is by closing the browser

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

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,"/");
Copy after login

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);
Copy after login

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 labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!