彻底解决PHP Session不过期以及SessionId保持不变的问题
用过asp.net里面的session再用过php里面的session,你会觉得php 的session相比asp.net里面的session是如此的不爽。在用php的session,你可能会遇到session不失效,关掉浏览器session还存在,重新打开浏览器sessionid还和以前一样等问题。。。
下面我们就来看下php的session机制:
session 回收机制:
PHP采用Garbage Collection process对过期session进行回收,然而并不是每次session建立时,都能够唤起 'garbage collection' process ,gc是按照一定概率启动的。这主要是出于对服务器性能方面的考虑,每个session都触发gc,浏览量大的话,服务器吃不消,然而按照一定概率开启gc,当流览量大的时候,session过期机制能够正常运行,而且服务器效率得到节省。细节应该都是多年的经验积累得出的。
三个与PHP session过期相关的参数(php.ini中):
session.gc_probability = 1
session.gc_divisor = 1000
session.gc_maxlifetime = 1440
gc启动概率 = gc_probability / gc_divisor = 0.1%
session过期时间 gc_maxlifetime 单位:秒
当web服务正式提供时,session过期概率就需要根据web服务的浏览量和服务器的性能来综合考虑session过期概率。为每个session都开启gc,显然是不明智的,感觉有点“碰运气”的感觉,要是访问量小命中几率就小。我在本机测试过程中,几乎都没有被命中过,sessionid几天都不变,哪怕机器重启。测试过程中,这个过期概率值要设置大一点命中几率才高点。
通过修改php配置文件的过期概率值,可以“碰运气”式的设置session过期,那有没有更好的办法呢?
下面写的这个session类可以彻底解决session不过期以及sessionid不变的问题。
[php]
/**
* 扩展Session类(简单封装)
*
* @author slimboy
*
*/
class Session {
/**
* 初始化
*/
static function _init(){
ini_set('session.auto_start', 0);
//Session::start();
}
/**
* 启动Session
*/
static function start() {
session_start();
}
/**
* 设置Session
*
* @param $name Session名称
* @param $value 值
* @param $time 超时时间(秒)
*/
public static function set($name,$value,$time){
if(empty($time)){
$time = 1800; //默认值
}
$_SESSION[$name] = $value;
$_SESSION[$name.'_Expires'] = time() + $time;
}
/**
* 获取Session值
*
* @param $name Session名称
*/
public static function get($name){
//检查Session是否已过期
if(isset($_SESSION[$name.'_Expires']) && $_SESSION[$name.'_Expires']>time()){
return $_SESSION[$name];
}else{
Session::clear($name);
return null;
}
}
/**
* 设置Session Domain
*
* @param $sessionDomain 域
* @return string
*/
static function setDomain($sessionDomain = null) {
$return = ini_get('session.cookie_domain');
if(!empty($sessionDomain)) {
ini_set('session.cookie_domain', $sessionDomain);//跨域访问Session
}
return $return;
}
/**
* 清除某一Session值
*
* @param $name Session名称
*/
static function clear($name){
unset($_SESSION[$name]);
unset($_SESSION[$name.'_Expires']);
}
/**
* 重置销毁Session
*/
static function destroy(){
unset($_SESSION);
session_destroy();
}
/**
* 获取或设置Session id
*/
static function sessionid($id=null){
return session_id($id);
}
}
Session::_init();
/**
* 扩展Session类(简单封装)
*
* @author slimboy
*
*/
class Session {
/**
* 初始化
*/
static function _init(){
ini_set('session.auto_start', 0);
//Session::start();
}
/**
* 启动Session
*/
static function start() {
session_start();
}
/**
* 设置Session
*
* @param $name Session名称
* @param $value 值
* @param $time 超时时间(秒)
*/
public static function set($name,$value,$time){
if(empty($time)){
$time = 1800; //默认值
}
$_SESSION[$name] = $value;
$_SESSION[$name.'_Expires'] = time() + $time;
}
/**
* 获取Session值
*
* @param $name Session名称
*/
public static function get($name){
//检查Session是否已过期
if(isset($_SESSION[$name.'_Expires']) && $_SESSION[$name.'_Expires']>time()){
return $_SESSION[$name];
}else{
Session::clear($name);
return null;
}
}
/**
* 设置Session Domain
*
* @param $sessionDomain 域
* @return string
*/
static function setDomain($sessionDomain = null) {
$return = ini_get('session.cookie_domain');
if(!empty($sessionDomain)) {
ini_set('session.cookie_domain', $sessionDomain);//跨域访问Session
}
return $return;
}
/**
* 清除某一Session值
*
* @param $name Session名称
*/
static function clear($name){
unset($_SESSION[$name]);
unset($_SESSION[$name.'_Expires']);
}
/**
* 重置销毁Session
*/
static function destroy(){
unset($_SESSION);
session_destroy();
}
/**
* 获取或设置Session id
*/
static function sessionid($id=null){
return session_id($id);
}
}
Session::_init();
调用示例:
[php]
//设置session
Session::set('UserId', $userid, 3600);
//设置session
Session::set('UserId', $userid, 3600);
[php]
//读取session
$userId = Session::get('UserId');
//读取session
$userId = Session::get('UserId');
当然,应该还有其它办法,欢迎童鞋提成!

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

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

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.
