PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/ThinkPHP/Extend/Driver/Session/SessionDb.class.php

https://github.com/huangshan/yuqing
PHP | 189 lines | 107 code | 11 blank | 71 comment | 15 complexity | 9522e8c4cdfda5735b5153c7bc16f725 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2012 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. defined('THINK_PATH') or exit();
  12. /**
  13. * 数据库方式Session驱动
  14. * CREATE TABLE think_session (
  15. * session_id varchar(255) NOT NULL,
  16. * session_expire int(11) NOT NULL,
  17. * session_data blob,
  18. * UNIQUE KEY `session_id` (`session_id`)
  19. * );
  20. * @category Extend
  21. * @package Extend
  22. * @subpackage Driver.Session
  23. * @author liu21st <liu21st@gmail.com>
  24. */
  25. class SessionDb {
  26. /**
  27. * Session有效时间
  28. */
  29. protected $lifeTime = '';
  30. /**
  31. * session保存的数据库名
  32. */
  33. protected $sessionTable = '';
  34. /**
  35. * 数据库句柄
  36. */
  37. protected $hander = array();
  38. /**
  39. * 打开Session
  40. * @access public
  41. * @param string $savePath
  42. * @param mixed $sessName
  43. */
  44. public function open($savePath, $sessName) {
  45. $this->lifeTime = C('SESSION_EXPIRE')?C('SESSION_EXPIRE'):ini_get('session.gc_maxlifetime');
  46. $this->sessionTable = C('SESSION_TABLE')?C('SESSION_TABLE'):C("DB_PREFIX")."session";
  47. //分布式数据库
  48. $host = explode(',',C('DB_HOST'));
  49. $port = explode(',',C('DB_PORT'));
  50. $name = explode(',',C('DB_NAME'));
  51. $user = explode(',',C('DB_USER'));
  52. $pwd = explode(',',C('DB_PWD'));
  53. if(1 == C('DB_DEPLOY_TYPE')){
  54. //读写分离
  55. if(C('DB_RW_SEPARATE')){
  56. $w = floor(mt_rand(0,C('DB_MASTER_NUM')-1));
  57. if(is_numeric(C('DB_SLAVE_NO'))){//指定服务器读
  58. $r = C('DB_SLAVE_NO');
  59. }else{
  60. $r = floor(mt_rand(C('DB_MASTER_NUM'),count($host)-1));
  61. }
  62. //主数据库链接
  63. $hander = mysql_connect(
  64. $host[$w].(isset($port[$w])?':'.$port[$w]:':'.$port[0]),
  65. isset($user[$w])?$user[$w]:$user[0],
  66. isset($pwd[$w])?$pwd[$w]:$pwd[0]
  67. );
  68. $dbSel = mysql_select_db(
  69. isset($name[$w])?$name[$w]:$name[0]
  70. ,$hander);
  71. if(!$hander || !$dbSel)
  72. return false;
  73. $this->hander[0] = $hander;
  74. //从数据库链接
  75. $hander = mysql_connect(
  76. $host[$r].(isset($port[$r])?':'.$port[$r]:':'.$port[0]),
  77. isset($user[$r])?$user[$r]:$user[0],
  78. isset($pwd[$r])?$pwd[$r]:$pwd[0]
  79. );
  80. $dbSel = mysql_select_db(
  81. isset($name[$r])?$name[$r]:$name[0]
  82. ,$hander);
  83. if(!$hander || !$dbSel)
  84. return false;
  85. $this->hander[1] = $hander;
  86. return true;
  87. }
  88. }
  89. //从数据库链接
  90. $r = floor(mt_rand(0,count($host)-1));
  91. $hander = mysql_connect(
  92. $host[$r].(isset($port[$r])?':'.$port[$r]:':'.$port[0]),
  93. isset($user[$r])?$user[$r]:$user[0],
  94. isset($pwd[$r])?$pwd[$r]:$pwd[0]
  95. );
  96. $dbSel = mysql_select_db(
  97. isset($name[$r])?$name[$r]:$name[0]
  98. ,$hander);
  99. if(!$hander || !$dbSel)
  100. return false;
  101. $this->hander = $hander;
  102. return true;
  103. }
  104. /**
  105. * 关闭Session
  106. * @access public
  107. */
  108. public function close() {
  109. if(is_array($this->hander)){
  110. $this->gc($this->lifeTime);
  111. return (mysql_close($this->hander[0]) && mysql_close($this->hander[1]));
  112. }
  113. $this->gc($this->lifeTime);
  114. return mysql_close($this->hander);
  115. }
  116. /**
  117. * 读取Session
  118. * @access public
  119. * @param string $sessID
  120. */
  121. public function read($sessID) {
  122. $hander = is_array($this->hander)?$this->hander[1]:$this->hander;
  123. $res = mysql_query("SELECT session_data AS data FROM ".$this->sessionTable." WHERE session_id = '$sessID' AND session_expire >".time(),$hander);
  124. if($res) {
  125. $row = mysql_fetch_assoc($res);
  126. return $row['data'];
  127. }
  128. return "";
  129. }
  130. /**
  131. * 写入Session
  132. * @access public
  133. * @param string $sessID
  134. * @param String $sessData
  135. */
  136. public function write($sessID,$sessData) {
  137. $hander = is_array($this->hander)?$this->hander[0]:$this->hander;
  138. $expire = time() + $this->lifeTime;
  139. mysql_query("REPLACE INTO ".$this->sessionTable." ( session_id, session_expire, session_data) VALUES( '$sessID', '$expire', '$sessData')",$hander);
  140. if(mysql_affected_rows($hander))
  141. return true;
  142. return false;
  143. }
  144. /**
  145. * 删除Session
  146. * @access public
  147. * @param string $sessID
  148. */
  149. public function destroy($sessID) {
  150. $hander = is_array($this->hander)?$this->hander[0]:$this->hander;
  151. mysql_query("DELETE FROM ".$this->sessionTable." WHERE session_id = '$sessID'",$hander);
  152. if(mysql_affected_rows($hander))
  153. return true;
  154. return false;
  155. }
  156. /**
  157. * Session 垃圾回收
  158. * @access public
  159. * @param string $sessMaxLifeTime
  160. */
  161. public function gc($sessMaxLifeTime) {
  162. $hander = is_array($this->hander)?$this->hander[0]:$this->hander;
  163. mysql_query("DELETE FROM ".$this->sessionTable." WHERE session_expire < ".time(),$hander);
  164. return mysql_affected_rows($hander);
  165. }
  166. /**
  167. * 打开Session
  168. * @access public
  169. */
  170. public function execute() {
  171. session_set_save_handler(array(&$this,"open"),
  172. array(&$this,"close"),
  173. array(&$this,"read"),
  174. array(&$this,"write"),
  175. array(&$this,"destroy"),
  176. array(&$this,"gc"));
  177. }
  178. }