Online.class.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. class Online extends Think {
  3. protected $lifeTime='1800' ,$sessionid='' ,$dao;
  4. public function __construct(&$params='') {
  5. $this->lifeTime = C('EXPIRE_TIME') ? C('EXPIRE_TIME') : 1800;
  6. if(cookie('onlineid')){
  7. $this->sessionid = cookie('onlineid');
  8. }else{
  9. $this->sessionid = substr(MD5(session_id()), 0, 32);
  10. cookie('onlineid',$this->sessionid,0);
  11. }
  12. $this->dao = M('Online');
  13. //session_set_save_handler(array(&$this,'open'), array(&$this,'close'), array(&$this,'read'), array(&$this,'write'), array(&$this,'destroy'), array(&$this,'gc'));
  14. $this->write($this->sessionid);
  15. $this->gc($this->lifeTime);
  16. }
  17. public function open($savePath, $sessName) {
  18. return true;
  19. }
  20. public function close() {
  21. return $this->gc($this->lifetime);
  22. }
  23. public function read($sessID) {
  24. $r = $this->dao->find($sessID);
  25. return $r ? $r['data'] : '';
  26. }
  27. public function write($sessID,$sessData) {
  28. $ip = get_client_ip();
  29. $username = cookie('username') ? cookie('username') : '';
  30. $groupid = cookie('groupid') ? intval(cookie('groupid')) : 4;
  31. $sessiondata = array(
  32. 'sessionid'=>$sessID,
  33. 'userid'=>intval(cookie('userid')),
  34. 'username'=>$username,
  35. 'ip'=>$ip,
  36. 'lastvisit'=>time(),
  37. 'groupid'=> $groupid,
  38. 'data'=> '',
  39. );
  40. return $this->dao->add($sessiondata,'',true);
  41. }
  42. public function destroy($sessID) {
  43. return $this->dao->delete($this->sessionid);
  44. }
  45. public function gc($sessMaxLifeTime) {
  46. $expiretime = time() -$sessMaxLifeTime;
  47. $r = $this->dao->where(" lastvisit < $expiretime")->delete();
  48. return $r;
  49. }
  50. }