Db.class.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2014 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. namespace Think\Session\Driver;
  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. */
  21. class Db {
  22. /**
  23. * Session有效时间
  24. */
  25. protected $lifeTime = '';
  26. /**
  27. * session保存的数据库名
  28. */
  29. protected $sessionTable = '';
  30. /**
  31. * 数据库句柄
  32. */
  33. protected $hander = array();
  34. /**
  35. * 打开Session
  36. * @access public
  37. * @param string $savePath
  38. * @param mixed $sessName
  39. */
  40. public function open($savePath, $sessName) {
  41. $this->lifeTime = C('SESSION_EXPIRE')?C('SESSION_EXPIRE'):ini_get('session.gc_maxlifetime');
  42. $this->sessionTable = C('SESSION_TABLE')?C('SESSION_TABLE'):C("DB_PREFIX")."session";
  43. //分布式数据库
  44. $host = explode(',',C('DB_HOST'));
  45. $port = explode(',',C('DB_PORT'));
  46. $name = explode(',',C('DB_NAME'));
  47. $user = explode(',',C('DB_USER'));
  48. $pwd = explode(',',C('DB_PWD'));
  49. if(1 == C('DB_DEPLOY_TYPE')){
  50. //读写分离
  51. if(C('DB_RW_SEPARATE')){
  52. $w = floor(mt_rand(0,C('DB_MASTER_NUM')-1));
  53. if(is_numeric(C('DB_SLAVE_NO'))){//指定服务器读
  54. $r = C('DB_SLAVE_NO');
  55. }else{
  56. $r = floor(mt_rand(C('DB_MASTER_NUM'),count($host)-1));
  57. }
  58. //主数据库链接
  59. $hander = mysql_connect(
  60. $host[$w].(isset($port[$w])?':'.$port[$w]:':'.$port[0]),
  61. isset($user[$w])?$user[$w]:$user[0],
  62. isset($pwd[$w])?$pwd[$w]:$pwd[0]
  63. );
  64. $dbSel = mysql_select_db(
  65. isset($name[$w])?$name[$w]:$name[0]
  66. ,$hander);
  67. if(!$hander || !$dbSel)
  68. return false;
  69. $this->hander[0] = $hander;
  70. //从数据库链接
  71. $hander = mysql_connect(
  72. $host[$r].(isset($port[$r])?':'.$port[$r]:':'.$port[0]),
  73. isset($user[$r])?$user[$r]:$user[0],
  74. isset($pwd[$r])?$pwd[$r]:$pwd[0]
  75. );
  76. $dbSel = mysql_select_db(
  77. isset($name[$r])?$name[$r]:$name[0]
  78. ,$hander);
  79. if(!$hander || !$dbSel)
  80. return false;
  81. $this->hander[1] = $hander;
  82. return true;
  83. }
  84. }
  85. //从数据库链接
  86. $r = floor(mt_rand(0,count($host)-1));
  87. $hander = mysql_connect(
  88. $host[$r].(isset($port[$r])?':'.$port[$r]:':'.$port[0]),
  89. isset($user[$r])?$user[$r]:$user[0],
  90. isset($pwd[$r])?$pwd[$r]:$pwd[0]
  91. );
  92. $dbSel = mysql_select_db(
  93. isset($name[$r])?$name[$r]:$name[0]
  94. ,$hander);
  95. if(!$hander || !$dbSel)
  96. return false;
  97. $this->hander = $hander;
  98. return true;
  99. }
  100. /**
  101. * 关闭Session
  102. * @access public
  103. */
  104. public function close() {
  105. if(is_array($this->hander)){
  106. $this->gc($this->lifeTime);
  107. return (mysql_close($this->hander[0]) && mysql_close($this->hander[1]));
  108. }
  109. $this->gc($this->lifeTime);
  110. return mysql_close($this->hander);
  111. }
  112. /**
  113. * 读取Session
  114. * @access public
  115. * @param string $sessID
  116. */
  117. public function read($sessID) {
  118. $hander = is_array($this->hander)?$this->hander[1]:$this->hander;
  119. $res = mysql_query("SELECT session_data AS data FROM ".$this->sessionTable." WHERE session_id = '$sessID' AND session_expire >".time(),$hander);
  120. if($res) {
  121. $row = mysql_fetch_assoc($res);
  122. return $row['data'];
  123. }
  124. return "";
  125. }
  126. /**
  127. * 写入Session
  128. * @access public
  129. * @param string $sessID
  130. * @param String $sessData
  131. */
  132. public function write($sessID,$sessData) {
  133. $hander = is_array($this->hander)?$this->hander[0]:$this->hander;
  134. $expire = time() + $this->lifeTime;
  135. mysql_query("REPLACE INTO ".$this->sessionTable." ( session_id, session_expire, session_data) VALUES( '$sessID', '$expire', '$sessData')",$hander);
  136. if(mysql_affected_rows($hander))
  137. return true;
  138. return false;
  139. }
  140. /**
  141. * 删除Session
  142. * @access public
  143. * @param string $sessID
  144. */
  145. public function destroy($sessID) {
  146. $hander = is_array($this->hander)?$this->hander[0]:$this->hander;
  147. mysql_query("DELETE FROM ".$this->sessionTable." WHERE session_id = '$sessID'",$hander);
  148. if(mysql_affected_rows($hander))
  149. return true;
  150. return false;
  151. }
  152. /**
  153. * Session 垃圾回收
  154. * @access public
  155. * @param string $sessMaxLifeTime
  156. */
  157. public function gc($sessMaxLifeTime) {
  158. $hander = is_array($this->hander)?$this->hander[0]:$this->hander;
  159. mysql_query("DELETE FROM ".$this->sessionTable." WHERE session_expire < ".time(),$hander);
  160. return mysql_affected_rows($hander);
  161. }
  162. }