-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession.class.php
More file actions
122 lines (90 loc) · 3.66 KB
/
session.class.php
File metadata and controls
122 lines (90 loc) · 3.66 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
class Session {
private static $handler=null;
private static $ip=null;
private static $lifetime=null;
private static $time=null;
//初始化变量;
private static function init($handler){
self::$handler=$handler;
//$_SERVER["REMOTE_ADDR"]获取客户端路由地址;
self::$ip = !empty($_SERVER["REMOTE_ADDR"]) ? $_SERVER["REMOTE_ADDR"] : 'unknown';
//ini_get()获取配置文件变量;
self::$lifetime=ini_get('session.gc_maxlifetime');
self::$time=time();
}
static function start(PDO $pdo){
self::init($pdo);
//_CLASS_ 代表本类;
session_set_save_handler(
array(__CLASS__,"open"),
array(__CLASS__,"close"),
array(__CLASS__,"read"),
array(__CLASS__,"write"),
array(__CLASS__,"destroy"),
array(__CLASS__,"gc")
);
session_start();
}
public static function open($path, $name){
return true;
}
public static function close(){
return true;
}
public static function read($PHPSESSID){
$sql="select PHPSESSID, update_time, client_ip, data from session where PHPSESSID= ?";
$stmt=self::$handler->prepare($sql);
$stmt->execute(array($PHPSESSID));
if(!$result=$stmt->fetch(PDO::FETCH_ASSOC)){
return '';
}
if( self::$ip != $result["client_ip"]){
self::destroy($PHPSESSID);
return '';
}
if(($result["update_time"] + self::$lifetime) < self::$time ){
self::destroy($PHPSESSID);
return '';
}
return $result['data'];
}
public static function write($PHPSESSID, $data){
$sql="select PHPSESSID, update_time, client_ip, data from session where PHPSESSID= ?";
$stmt=self::$handler->prepare($sql);
$stmt->execute(array($PHPSESSID));
if($result=$stmt->fetch(PDO::FETCH_ASSOC)){
if($result['data'] != $data || self::$time > ($result['update_time']+30)){
$sql="update session set update_time = ?, data =? where PHPSESSID = ?";
$stm=self::$handler->prepare($sql);
$stm->execute(array(self::$time, $data, $PHPSESSID));
}
}else{
if(!empty($data)){
$sql="insert into session(PHPSESSID, update_time, client_ip, data) values(?,?,?,?)";
$sth=self::$handler->prepare($sql);
$sth->execute(array($PHPSESSID, self::$time, self::$ip, $data));
}
}
return true;
}
public static function destroy($PHPSESSID){
$sql="delete from session where PHPSESSID = ?";
$stmt=self::$handler->prepare($sql);
$stmt->execute(array($PHPSESSID));
return true;
}
private static function gc($lifetime){
$sql = "delete from session where update_time < ?";
$stmt=self::$handler->prepare($sql);
$stmt->execute(array(self::$time-$lifetime));
return true;
}
}
try{
$pdo=new PDO("mysql:host=localhost;dbname=myeducation", "root", "123456");
$pdo->query('set names utf8');
}catch(PDOException $e){
echo $e->getMessage();
}
Session::start($pdo);