Home > php教程 > php手册 > php之session高级

php之session高级

WBOY
Release: 2016-06-13 09:10:42
Original
1269 people have browsed it

php之session高级

此篇主要微微讲解一下session的高级用法

php.ini配置文件中有关session高级的配置:

session.save_path:session保存地址(原路径:C:/Users/ADMINI~1/AppData/Local/Temp)
session_name:session获取到的名称
session.use_trans_sid:启用SID支持

session.gc_maxlifetime: 垃圾回收器, 指定过了多少秒之后数据就会被视为垃圾,并被清除

session.gc_probability=1
session.gc_divisor=100
这两合起来就是启动gc进程管理概率的
初始化时(session_start())
概率=session.gc_probability/session.gc_divisor

session.use_cookie = 1:是否使用cookie
session.cookie_path = '/':cookie保存路径
session.cookie_domain = '': 一般不用改
session.cookie_lifetime = 0: 保存时间长

session.save_handler = files:用文件来保存会话信,改成user
session.save_handler=memcache: 如果是memcache,那么save_path=tcp://localhost:11211

 

一、首先在配置文件中将session.save_handler的值改为user (原本是files),将session数据保存路径改到D盘

 

二、在根目录下创建相关的文件

session.php 是公共的头文件,one.php 是session注册事件文件,two.php 是测试文件,three.php 是注销文件

one.php:

 

<!--?php
    include "session.php";
    
    $_SESSION["isLogin3"]=1;
    $_SESSION["username"]="admin";
    $_SESSION["uid"]=333;
    echo session_name().&#39;=&#39;.session_id()."<br-->";
Copy after login

two.php:
<!--?php
    include "session.php";
    print_r($_SESSION);
    echo &#39;<br-->&#39;;
    echo session_name().&#39;=&#39;.session_id()."
";
Copy after login

three.php:
<!--?php
    include "session.php";
    
    $_SESSION=array();
    if(isset($_COOKIE[session_name()])){
    	setCookie(session_name(),&#39;&#39;,time()-100,&#39;/&#39;);
    }
    session_destroy();
    echo session_name().&#39;=&#39;.session_id()."<br-->";
Copy after login

对于公共的头文件 session.php 相对重要,这之前必须先将配置文件中的必须将session.serialize_handler = file的值改为user

session.php 要使用到 session_set_save_handler() 函数,查看手册其用到的参数,再开启session

session_set_save_handler("open","close","read","write","destroy","gc");
	session_start();
Copy after login

再根据参数写出各个方法,重点是把握各个函数执行的时间

open():

Copy after login
//在运行session_start()是调用,读取session到$_SESSION中
    function open($save_path, $session_name){
    	global $sess_save_path;   //将保存路径设置为全局变量
    	$sess_save_path=$save_path;  //读取的路径给保存路径
    	return true;
    }
Copy after login

close():
//session_wirte_close()和session_destroy()
    function close(){
    	return true;
    }
Copy after login

read():
//session_start(),$_SESSION
    //自动把session_id传进来
    function read($id){
    	global $sess_save_path;
    	$sess_file=$sess_save_path."/kf_".$id;

    	return (string)@file_get_contents($sess_file);
    }
Copy after login

write():
//脚本结束时和使用session_write_close()强制提交SESSION数据时
    //直接调用$_SESSION[]="aaa";
    function write($id, $sess_data){
    	global $sess_save_path;

    	//定义保存文件及文件名
    	$sess_file=$sess_save_path."/kf_".$id;
    	if($fp=@fopen($sess_file, "w")){
    		$return=fwrite($fp, $sess_data);
    		fclose($fp);
    		return $return;
    	}else{
    		return false;
    	}
    }
Copy after login

的story()和gc():(删除和垃圾回收)

//session_destroy()时
    function destroy($id){
    	global $sess_save_path;
    	$sess_file=$sess_save_path."/kf_".$id;

    	//删除路径对象的文件
    	return @unlink($sess_file);
    }
    //session.gc_probability和session.gc_divisor决定的,在open()和read()读数据时,
    function gc($maxlifetime){
    	global $sess_save_path;


    	//所有过期的都要删除
    	//glob遍历出所有以"/kf"的文件作为每一个文件
    	foreach(glob($sess_save_path."/kf_*") as $filename){
    		if(filetime($filename)+$maxlifetime < time()){
    			@unlink($filename);
    			echo $filename;
    		}
    	}
    	return true;
    }
Copy after login




 

 

 

 

 

 

 

 

 

 

 

 


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template