首页 > 代码库 > PHP实现session对象封装
PHP实现session对象封装
<?php class Session { private $db; // 设置数据库变量 private $expiry = 3600; // 设置Session失效时间 public function Session() { session_set_save_handler(array(& $this,‘_session_open‘), array(& $this,‘_session_close‘), array(& $this,‘_session_read‘), array(& $this,‘_session_write‘), array(& $this,‘_session_destroy‘), array(& $this,‘_session_gc‘) ); } public function _session_open($save_path,$session_name) { $this->db = mysql_connect(‘localhost‘,‘root‘,‘123456‘) or die ("数据库连接失败!"); mysql_select_db("db_database11",$this->db); return(true); } public function _session_close() { mysql_close($this->db); return(true); } public function _session_read($key) { $time = time(); // 设定当前时间 $sql = "select session_data from tb_session where session_key = ‘$key‘ and session_time > $time"; $result = mysql_query($sql,$this->db); $row = mysql_fetch_array($result); if ($row) { return($row[‘session_data‘]); // 返回Session名称及内容 }else { return(false); } } public function _session_write($key,$data) { $lapse_time = time() + $this->time; // 得到Unix时间戳 $sql = "select session_data from tb_session where session_key = ‘$key‘ and session_time > $lapse_time"; $result = mysql_query($sql,$this->db); if (mysql_num_rows($result) == 0 ) // 没有结果 { $sql = "insert into tb_session values(‘$key‘,‘$data‘,$lapse_time)"; // 插入数据库sql语句 $result = mysql_query($sql,$this->db); }else { $sql = "update tb_session set session_key = ‘$key‘,session_data = http://www.mamicode.com/‘$data‘,session_time = $lapse_time where session_key = ‘$key‘"; // 修改数据库sql语句 $result = mysql_query($sql,$this->db); } return($result); } public function _session_destroy($key) { $sql = "delete from tb_session where session_key = ‘$key‘"; // 删除数据库sql语句 $result = mysql_query($sql,$this->db); return($result); } public function _session_gc($expiry_time) { $expiry_time = time(); // 将参数$expiry_time赋值为当前时间戳 $sql = "delete from tb_session where expiry_time < $expiry_time"; // 删除数据库sql语句 $result = mysql_query($sql,$this->db); return($result); } } $_session_new = new Session(); session_start(); $_SESSION[‘SID‘] = true; ?>
PHP实现session对象封装
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。