Posted By

mafro on 07/11/07


Tagged

session


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

heinz1959
xuanyan


Session Class


 / Published in: PHP
 

PHP DB Session Class

  1. <?php
  2.  
  3. /**
  4. * @desc Session tracking class - overrides PHP session handling
  5. * @date 14/11/05
  6. * @ver 3.2
  7. *
  8. * @req DB tables: session {session_id[varchar32], timestamp[int11], data[text]}
  9. *
  10. * @sql CREATE TABLE `sessions` (
  11. * `sid` varchar(32) NOT NULL default '',
  12. * `timestamp` int(11) NOT NULL default '0',
  13. * `data` text NOT NULL,
  14. * PRIMARY KEY (`sid`)
  15. * ) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE latin1_general_cs;
  16. */
  17.  
  18. define('TIMEOUT_MINS', 20);
  19.  
  20.  
  21. class Session {
  22.  
  23. var $_db;
  24.  
  25. function Session() {
  26. session_set_save_handler(array(&$this,'_open'), array(&$this,'_close'), array(&$this,'_read'), array(&$this,'_write'), array(&$this, '_destroy'), array(&$this,'_gc'));
  27. }
  28.  
  29. function _open($path, $name) {
  30. $this->_db = &Registry::GetDBConnection();
  31. return true;
  32. }
  33.  
  34. function _close() {
  35. $this->_gc(0);
  36. return true;
  37. }
  38.  
  39. function _read($sid) {
  40. $sql = "SELECT data FROM sessions WHERE sid = '$sid' GROUP BY sid";
  41. $res = $this->_db->query($sql);
  42. if(Pear::IsError($res)) {
  43. return '';
  44. }
  45.  
  46. if($row = $res->fetchRow()) {
  47. return $row['data'];
  48. }else{
  49. return '';
  50. }
  51. }
  52.  
  53. /**
  54. * @desc create session or update it
  55. */
  56. function _write($sid, $data) {
  57. $timestamp = time();
  58. $data = addslashes($data);
  59.  
  60. $sql = "INSERT INTO sessions (sid, timestamp, data) VALUES ('$sid', $timestamp, '$data') ";
  61. $sql .= "ON DUPLICATE KEY UPDATE timestamp = $timestamp, data = '$data'";
  62. $res = $this->_db->query($sql);
  63.  
  64. if(Pear::IsError($res)) {
  65. return false;
  66. }else{
  67. return true;
  68. }
  69. }
  70.  
  71. function _destroy($sid) {
  72. $sql = "DELETE FROM sessions WHERE sid = '$sid'";
  73. $res = $this->_db->query($sql);
  74.  
  75. if(Pear::IsError($res)) {
  76. return false;
  77. }else{
  78. return true;
  79. }
  80. }
  81.  
  82. function _gc($life) {
  83. $timeout = strtotime("-" . TIMEOUT_MINS . " minutes");
  84. $sql = "DELETE FROM sessions WHERE timestamp < $timeout";
  85. $res = $this->_db->query($sql);
  86.  
  87. if(Pear::IsError($res)) {
  88. return false;
  89. }else{
  90. return true;
  91. }
  92. }
  93.  
  94.  
  95. /**
  96. * @desc checks whos online now
  97. * works for separate clients and pages
  98. *
  99. * @return array of results
  100. */
  101. function check($page_id=0) {
  102. $timeout = time() - (TIMEOUT_MINS * 60);
  103.  
  104. //load current client_id for WHERE
  105. $client_id = $_SESSION['client_id'];
  106.  
  107. $like_clause = '%client\_id|s:%:"' . $client_id . '"';
  108. $like_clause .= ($page_id==0) ? "" : '%page\_id|s:%:"' . $page_id . '"';
  109. $like_clause .= '%';
  110.  
  111. $sql = "SELECT sid, data FROM sessions ";
  112. $sql .= "WHERE data LIKE ";
  113. $sql .= " '" . $like_clause . "' ";
  114. $sql .= " GROUP BY sid";
  115.  
  116. $rows = $this->_db->query($sql);
  117.  
  118. if(Pear::IsError($res)) {
  119. return false;
  120. }else{
  121. //do session_decode to parse out values
  122. $sessions = array();
  123. while($row = $rows->fetchRow()) {
  124. $sessions[] = $this->_unserialize($row['data']);
  125. }
  126. return $sessions;
  127. }
  128. }
  129.  
  130. /*
  131. *
  132. * user_id|s:2:"19";client_id|s:1:"2";
  133. */
  134. function _unserialize($session) {
  135. $result = array();
  136. $items = explode(";", $session);
  137. for($i=0; $i<count($items)-1; $i++) {
  138. $array = explode("|", $items[$i]);
  139. $tmp = explode(":", $array[1]);
  140. $value = trim($tmp[2], "\"");
  141. $result[$array[0]] = $value;
  142. }
  143. return $result;
  144. }
  145.  
  146.  
  147. }
  148. ?>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: giachini on December 4, 2011

Como é que eu chamo essa classe no php.

You need to login to post a comment.