php基于session实现数据库交互的类实例

时间:2022-09-22 09:49:55

本文实例讲述了php基于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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
/**
 * session 数据库存储类
 */
class Session {
 private static $session_id   = 0;
 private static $session_data  = array();
 private static $is_update   = FALSE;
 private static $is_del    = FALSE;
 private static $is_gc    = FALSE;
 private static $dbo    = NULL;  //数据库连接句柄
 private static $gc_max_time  = 1440;
 private static $table   = 'sessions';
 private static $pre_key   = 'weige';//session 密钥
 //捆绑使用哈
 private static $gc_rate_de  = 100;//代表分母
 private static $gc_rate_co  = 20;//代表分子
 private static $path   = '/';//保存路径
 private static $domain   = null; //域
 private static $secure   = false;//默认
 private static $httponly  = false;//默认
 /**
  * 获取数据库句柄 私有
  */
 private static function open()
 {
  if (!self::$dbo)
  {
   self::$dbo = Db::factory();
  }
  return TRUE;
 }
 /**
  * 设置
  * */
 public static function set($key, $val=NULL)
 {
  self::open();
  $data = self::read();
  if ($data === FALSE)
  {
   $data = array();
  }
  if (!$val && is_array($key))
  {
   $data = $key;
  }
  else if ($val && is_string($key))
  {
   $data[$key] = $val;
  }
  self::write($data);
  self::close();
 }
 /**
  *获取值
  *
  */
 public static function get($key=NULL) {
  self::open();
  self::$session_data = self::read();
  $ret = '';
  if (!$key) {
   $ret = self::$session_data;
  } else if(is_array(self::$session_data) && isset(self::$session_data[$key])) {
   $ret = self::$session_data[$key];
  }
  self::update();
  self::close();
  return $ret;
 }
 /**
  * 删除或者重置
  * */
 public static function del($key)
 {
  if (!self::$is_del)
  {
   self::open();
   $val = self::read();
   if (isset($val[$key]))
   {
    unset($val[$key]);
   }
   $session_id  = self::$session_id;
   $session_data  = serialize($val);
   $session_expire = TIME + self::get_gc_maxtime();
   self::$dbo->query("update ".self::$table." set value='$session_data', expiry='$session_expire' where session_id='$session_id'");
   self::close();
  }
  self::$is_del = TRUE;
 }
 /**
  * 销毁
  *
  * */
 public static function destroy()
 {
  $session_id   = self::get_session_id();
  $_COOKIE['WBSID'] = '';
  self::open();
  self::$dbo->query("delete from ".self::$table." where session_id='$session_id'");
  self::close();
 }
 /**
  * 读取 私有
  * */
 private static function read()
 {
  $session_id = self::$session_id;
  if (!$session_id) {
   $session_id = self::get_session_id();
  }
  if (!$session_id) return array();
  $user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? md5($_SERVER['HTTP_USER_AGENT']) : '';
  $client_ip = Fun::getIp();
  $session_expire = TIME - self::get_gc_maxtime();
  $rs = self::$dbo->fetchRow("select session_id, value, agent, ip from ".self::$table."
   where session_id='$session_id' and expiry>'$session_expire'");
  if (!$rs || $rs['agent'] != $user_agent || $rs['ip'] != $client_ip)
  {
   return FALSE;
  }
  self::$session_id = $rs['session_id'];
  return unserialize($rs['value']);
 }
 /**
  * session 写入 私有
  * */
 private static function write(array $session_data)
 {
  $session_id = self::$session_id;
  if (!$session_id)
  {
   $session_id = self::get_session_id();
  }
  $session_expire = TIME + self::get_gc_maxtime();
  $user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? md5($_SERVER['HTTP_USER_AGENT']) : '';
  $client_ip  = Fun::getIp();
  $session_data = serialize($session_data);
  if (self::$session_id && self::$session_id === $session_id)
  {
   self::$dbo->query("update ".self::$table." set value='$session_data', expiry='$session_expire', agent='$user_agent', ip='$client_ip' where session_id='$session_id'");
  }
  else
  {
   self::$session_id = $session_id = self::create_session_id();
   self::$dbo->query("insert into ".self::$table."(session_id, value, expiry, agent, ip)
    values('$session_id', '$session_data', '$session_expire', '$user_agent', '$client_ip')");
  }
  return true;
 }
 /**
  * session 更新 私有
  * */
 private static function update()
 {
  if (!self::$is_update)
  {
   $session_id = self::$session_id;
   $session_expire = TIME + self::get_gc_maxtime();
   self::$dbo->query("update ".self::$table." set expiry='$session_expire' where session_id='$session_id'");
  }
  self::$is_update = TRUE;
 }
 private static function close()
 {
  if (!self::$is_gc && mt_rand(1, self::$gc_rate_de)%self::$gc_rate_co == 0)
  {
   self::gc();
  }
  self::$is_gc = TRUE;
 }
 /**
  * 过期session 清除 随机触发
  * */
 private static function gc()
 {
  $session_expire = TIME - self::get_gc_maxtime();
   self::$dbo->query("delete from ".self::$table." where expiry<'$session_expire'");
 }
 private static function get_session_id()
 {
  if (isset($_COOKIE['WBSID']) && strlen($_COOKIE['WBSID'])==32)
  {
   $sid = $_COOKIE['WBSID'];
   setcookie('WBSID', $sid, TIME + self::get_gc_maxtime(), self::$path, self::$domain, self::$secure, self::$httponly);
   return $sid;
  }
  return null;
 }
 private static function create_session_id()
 {
  $sid = self::get_session_id();
  if (!$sid)
  {
   $sid = Fun::getIp() . TIME . microtime(TRUE) . mt_rand(mt_rand(0, 100), mt_rand(100000, 90000000));
   $sid = md5(self::$pre_key . $sid);
   setcookie('WBSID', substr($sid, 0, 32), TIME + self::get_gc_maxtime(), self::$path, self::$domain, self::$secure, self::$httponly);
  }
  return $sid;
 }
 public static function get_gc_maxtime()
 {
  return self::$gc_max_time;
 }
}

希望本文所述对大家的php程序设计有所帮助。