session执行原理
客户端 登录 服务器, 在服务器上存储了session, 服务器会为该客户端创建一个session文件,
并把 session id 存储到 客户端的cookie上关于session的配置
session.save_handler = files session保存在文件中 session.save_path = "c:/wamp/tmp" 保存文件的路径 session.use_cookies = 1 是否使用cookie session.use_only_cookies = 1 是否只使用cookie session.name = PHPSESSID cookie中sessionid的键名 session.auto_start = 0 是否自动开启session session.cookie_lifetime = 0 sessionid在 cookie的生存周期 session.cookie_path = / session.cookie_domain = session.cookie_httponly = session.serialize_handler = php 文件中存储session数据的方法 session.gc_probability = 1 session.gc_divisor = 1000 session.gc_maxlifetime = 1440 session默认保存时间 秒 session.use_trans_sid = 0
自定义session
① 设置 session的处理 方式为 用户自定义session_module_name('user') 或 ini_set('')
② 设置回调函数 控制session的处理流程
session_set_save_handler(open, close, read, werite, destroy, gc) open(sess_path, sess_name) close() read(sess_id) write(sess_id, sess_data) destroy(sess_id) gc(sess_lifetime)
session存储在 自定义文件中
它的意义不大就不说了- session存储在 数据库中
定义一个类来执行操作1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87<?php
//session驱动 存储于 数据库
class DbSession{
private static $pdo;
//初始化
public static function start(PDO $pdo){
self::$pdo = $pdo; //给属性赋值
//设置session
session_module_name('user');
session_set_save_handler(
array(__CLASS__, 'open'),
array(__CLASS__, 'close'),
array(__CLASS__, 'read'),
array(__CLASS__, 'write'),
array(__CLASS__, 'destroy'),
array(__CLASS__, 'gc')
);
session_start();
}
//open
public static function open($sess_path, $sess_name){
return true;
}
//close
public static function close(){
return true;
}
//read
public static function read($sess_id){
$sql = "select sess_data from session where sess_id=?";
$stmt = self::$pdo->prepare($sql);
$stmt->execute(array($sess_id));
$stmt->bindColumn('sess_data', $sess_data);
$stmt->fetch();
return $sess_data;
}
//write
public static function write($sess_id, $sess_data) {
//判断 之前是否 存储过session
$sql = "select sess_data from session where sess_id=?";
$stmt = self::$pdo->prepare($sql);
$stmt->execute(array($sess_id));
if ($stmt->rowCount() > 0) {
//更新
$sql = "update session set sess_data=?, sess_time=? where sess_id=?";
$stmt = self::$pdo->prepare($sql);
$stmt->execute(array($sess_data, time(), $sess_id));
} else {
//插入
$sql = "insert into session(sess_id, sess_data, sess_time) values(?,?,?)";
$stmt = self::$pdo->prepare($sql);
$stmt->execute(array($sess_id, $sess_data, time()));
}
}
//destroy
public static function destroy($sess_id){
$sql = "delete from session where sess_id=?";
$stmt = self::$pdo->prepare($sql);
$stmt->execute(array($sess_id));
return true;
}
//gc
public static function gc($sess_lifetime){
$sql = "delete from session where sess_time<".(time()-$sess_lifetime);
$stmt = self::$pdo->prepare($sql);
$stmt->execute();
}
}
try {
$pdo = new PDO('mysql:host=localhost;dbname=s33', 'root', '123456');
$pdo->exec('set names utf8');
} catch (PDOException $e) {
echo $e->getMessage();exit;
}
//初始化
DbSession::start($pdo);
?>
数据库建表1
2
3
4
5create table session(
sess_id char(32) not null primary key,
sess_data text not null,
sess_time int not null
)engine=innodb default charset=utf8;
测试1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18<?php
header("Content-type:text/html;charset=utf-8");
require "./DbSession.class.php";
$_SESSION['username'] = "jiabrother";
$_SESSION['age'] = "45";
$_SESSION['grade'] = "s33";
//session_destroy();
echo "<hr>";
var_dump($_SESSION);
var_dump($_COOKIE);
?>
<a href="2.php">2.php</a>
- session存储在 memcache
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22<?php
header("Content-type:text/html;charset=utf-8");
ini_set('session.save_handler', 'memcache');
ini_set('session.save_path', '127.0.0.1:11211');
session_start();
$_SESSION['username'] = "jiabrother";
$_SESSION['age'] = "45";
$_SESSION['grade'] = "s33";
//session_destroy();
echo "<hr>";
var_dump($_SESSION);
var_dump($_COOKIE);
?>
<a href="2.php">2.php</a>
测试1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16<?php
header("Content-type:text/html;charset=utf-8");
ini_set('session.save_handler', 'memcache');
ini_set('session.save_path', '127.0.0.1:11211');
session_start();
var_dump($_SESSION);
var_dump($_COOKIE);
$mem = new Memcache();
$mem->addServer('127.0.0.1', 11211);
echo $mem->get('okgmpf6dije13a88u4ikad6h31');
?>
本文出自 “小风博客” BY:小风 如果你喜欢,转载请务必保留此出处^_^
小风博客 http://www.xfphp.cn 小风网络 http://www.hotxf.com