如果客户端没有禁用 Cookie,则 Cookie 在启动 Session 会话的时候扮演的是存储 Session ID 和 Session 生存期的角色。我们来手动设置 Session 的生存期:
例子,利用cooikes
<?php session_start(); // 保存一天 $lifeTime = 24 * 3600; setcookie(session_name() , session_id() , time() + $lifeTime, "/"); ?>
PHP5 Session还提供了一个函数 session_set_cookie_params(); 来设置PHP5 Session的生存期的,该函数必须在 session_start() 函数调用之前调用:
<?php // 保存一天 $lifeTime = 24 * 3600; session_set_cookie_params($lifeTime); session_start(); ?>
php中还有一个ini_set可以设置session.gc_maxlifetime来设定Session的生存周期。例如:
<?php ini_set('session.gc_maxlifetime', 3600); //设置时间 ini_get('session.gc_maxlifetime'); //得到ini中设定值 ?>
下面提供一个别人封装好的函数,但是我没有测试过,仅供参考:
<?php function start_session($expire = 0) { if ($expire == 0) { $expire = ini_get('session.gc_maxlifetime'); } else { ini_set('session.gc_maxlifetime', $expire); } if (empty($_COOKIE['PHPSESSID'])) { session_set_cookie_params($expire); session_start(); } else { session_start(); setcookie('PHPSESSID', session_id() , time() + $expire); } } ?>
使用方法:
加入start_session(600);//600秒以后过期。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1
Easy-to-use and free code editor

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
