首页 >后端开发 >php教程 >php结合session操作数据库的方法

php结合session操作数据库的方法

墨辰丷
墨辰丷原创
2018-06-07 17:33:432007浏览

本篇文章主要介绍php结合session操作数据库的方法,感兴趣的朋友参考下,希望对大家有所帮助。

具体如下:

<?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   = &#39;sessions&#39;;
 private static $pre_key   = &#39;weige&#39;;//session 密钥
 //捆绑使用哈
 private static $gc_rate_de  = 100;//代表分母
 private static $gc_rate_co  = 20;//代表分子
 private static $path   = &#39;/&#39;;//保存路径
 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 = &#39;&#39;;
  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=&#39;$session_data&#39;, expiry=&#39;$session_expire&#39; where session_id=&#39;$session_id&#39;");
   self::close();
  }
  self::$is_del = TRUE;
 }
 /**
  * 销毁
  * 
  * */
 public static function destroy() 
 {
  $session_id   = self::get_session_id();
  $_COOKIE[&#39;WBSID&#39;] = &#39;&#39;;
  self::open();
  self::$dbo->query("delete from ".self::$table." where session_id=&#39;$session_id&#39;");
  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[&#39;HTTP_USER_AGENT&#39;]) ? md5($_SERVER[&#39;HTTP_USER_AGENT&#39;]) : &#39;&#39;;
  $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=&#39;$session_id&#39; and expiry>&#39;$session_expire&#39;");
  if (!$rs || $rs[&#39;agent&#39;] != $user_agent || $rs[&#39;ip&#39;] != $client_ip) 
  {
   return FALSE;
  }
  self::$session_id = $rs[&#39;session_id&#39;];
  return unserialize($rs[&#39;value&#39;]);
 }
 /**
  * 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[&#39;HTTP_USER_AGENT&#39;]) ? md5($_SERVER[&#39;HTTP_USER_AGENT&#39;]) : &#39;&#39;;
  $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=&#39;$session_data&#39;, expiry=&#39;$session_expire&#39;, agent=&#39;$user_agent&#39;, ip=&#39;$client_ip&#39; where session_id=&#39;$session_id&#39;");
  } 
  else 
  {
   self::$session_id = $session_id = self::create_session_id();
   self::$dbo->query("insert into ".self::$table."(session_id, value, expiry, agent, ip) 
    values(&#39;$session_id&#39;, &#39;$session_data&#39;, &#39;$session_expire&#39;, &#39;$user_agent&#39;, &#39;$client_ip&#39;)");
  }
  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=&#39;$session_expire&#39; where session_id=&#39;$session_id&#39;");
  }
  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<&#39;$session_expire&#39;");
 }
 private static function get_session_id() 
 {
  if (isset($_COOKIE[&#39;WBSID&#39;]) && strlen($_COOKIE[&#39;WBSID&#39;])==32) 
  {
   $sid = $_COOKIE[&#39;WBSID&#39;];
   setcookie(&#39;WBSID&#39;, $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(&#39;WBSID&#39;, 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实现适配器模式的方法详解

php实现页面纯静态的方法

ThinkPHP实现批量删除栏目的方法

以上是php结合session操作数据库的方法的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn