php session 写入数据库


Posted in PHP onFebruary 13, 2016

本文实例介绍了php session 写入数据库的方法,分享给大家供大家参考,具体内容如下

<?php
 
#
#  codeMaker Alpha 0.1.1 ( haowei.me )
#  This framework comply with the GPL license agreement
#
 
class session_handler {
   
  protected $maxlifetime = null;
  protected $dbHandle = null;
  public $config = null;
   
  public static function init($args) {
    return new self($args);
  }
   
  public function __construct($args) {
     
    $this->config = $args;
    $this->maxlifetime = get_cfg_var("session.gc_maxlifetime");
    session_set_save_handler(
    array($this, "open"),
    array($this, "close"),
    array($this, "read"),
    array($this, "write"),
    array($this, "destroy"),
    array($this, "gc"));
     
  }
   
  public function open() {
    $this->link = mysqli_connect(
    $this->config['host'], 
    $this->config['user'], 
    $this->config['password'], 
    $this->config['database']);
    mysqli_set_charset($this->link, "utf8");
    $sql = 'CREATE TABLE IF NOT EXISTS `%s` (
    `session_id` varchar(255) NOT NULL,
    `session_data` text,
    `session_expires` char(10) NOT NULL,
    PRIMARY KEY (`session_id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;';
    $sql = sprintf($sql, $this->config['table']);
    mysqli_query($this->link, $sql);
    return true;
  }
   
  public function close() {
    return true;
  }
   
   
  public function read($session_id) {
    if(empty($session_id)) return null;
     
    $sql = 'SELECT `session_data` AS `data` FROM `%s` WHERE `session_id` = "%s" AND `session_expires` > "%u"';
    $sql = sprintf($sql, 
    mysqli_real_escape_string($this->link, $this->config['table']), 
    mysqli_real_escape_string($this->link, $session_id),
    time());
     
    $result = mysqli_query($this->link, $sql);
    $row = mysqli_fetch_assoc($result);
    return $row['data'];
     
  }
   
  public function write($session_id, $session_data) {
    if(empty($session_id)) return null;
     
    $newExpires = time() + $this->maxlifetime;
    $sql = 'REPLACE INTO `%s` SET `session_id` = "%s", `session_data` = "%s", `session_expires` = "%u"';
    $sql = sprintf($sql, 
    mysqli_real_escape_string($this->link, $this->config['table']), 
    mysqli_real_escape_string($this->link, $session_id),
    mysqli_real_escape_string($this->link, $session_data),
    $newExpires);
     
    $result = mysqli_query($this->link, $sql);
    return mysqli_affected_rows($this->link);
     
  }
   
  public function destroy($session_id) {
    $sql = 'DELETE FROM `%s` WHERE `session_id` = "%s"';
    $sql = sprintf($sql, 
    mysqli_real_escape_string($this->link, $this->config['table']), 
    mysqli_real_escape_string($this->link, $session_id));
 
    $result = mysqli_query($this->link, $sql);
    return mysqli_affected_rows($this->link);
  }
   
  public function gc() {
    $sql = 'DELETE FROM `%s` WHERE `session_expires` < "%u"';
    $sql = sprintf($sql, 
    mysqli_real_escape_string($this->link, $this->config['table']), 
    time());
     
    $result = mysqli_query($this->link, $sql);
    return mysqli_affected_rows($this->link);
  }
   
}
 
class session {
 
  public static $collection = null;
   
  public static function open($clean = false, $token = false) {
    if($clean) ob_end_clean();
    if($token) session_id($token);
    session_start();
    self::$collection = $_SESSION;
  }
   
  public static function id() {
    $num_args = func_num_args();
    if($num_args) {
      $args = func_get_arg(0);
      return session_id($args);
    }else{
      return session_id();
    }
  }
   
  public static function get($name) {
    return isset($_SESSION[$name]) ? $_SESSION[$name] : null;
  }
   
  public static function set($name, $value) {
    $_SESSION[$name] = $value;
    return true;
  }
   
  public static function delete($name) {
    if(!isset($_SESSION[$name])) return null;
    unset($_SESSION[$name]);
    return true;
  }
   
  public static function destroy() {
    session_destroy();
  }
   
}
 
 
 
$config = array(
"host"=> "127.0.0.1",
"user"=> "root",
"password"=> "123456",
"database"=> "test",
"charset"=> "utf8",
"table"=> "user_session");
 
session_handler::init($config);
session::open();
session::set("profile", array("id"=> 1, "user"=> "haowei", "vip-level"=> 6));

以上就是本文的全部内容,希望对大家的学习有所帮助。

PHP 相关文章推荐
理解PHP5中static和const关键字的区别
Mar 19 PHP
PHP二维数组的去重问题解析
Jul 17 PHP
php中Smarty模板初体验
Aug 08 PHP
PHP fgetcsv 定义和用法(附windows与linux下兼容问题)
May 29 PHP
PHP对接微信公众平台消息接口开发流程教程
Mar 25 PHP
PHPer 需要了解的 5 个 Composer 小技巧
Aug 18 PHP
php使用substr()和strpos()联合查找字符串中某一特定字符的方法
May 12 PHP
PHP微信开发之查询城市天气
Jun 23 PHP
Yii净化器CHtmlPurifier用法示例(过滤不良代码)
Jul 15 PHP
laravel接管Dingo-api和默认的错误处理方式
Oct 25 PHP
PHP实现随机发扑克牌
Apr 22 PHP
深入理解PHP+Mysql分布式事务与解决方案
Dec 03 PHP
php等比例缩放图片及剪切图片代码分享
Feb 13 #PHP
PHP信号量基本用法实例详解
Feb 12 #PHP
PHP消息队列用法实例分析
Feb 12 #PHP
PHP共享内存用法实例分析
Feb 12 #PHP
PHP连接MSSQL方法汇总
Feb 05 #PHP
Symfony2开发之控制器用法实例分析
Feb 05 #PHP
Symfony2实现在doctrine中内置数据的方法
Feb 05 #PHP
You might like
PHP 加密与解密的斗争
2009/04/17 PHP
php下把数组保存为文件格式的实例应用
2010/02/08 PHP
jquery 事件执行检测代码
2009/12/09 Javascript
javascript 获取所有id中包含某关键字的控件的实现代码
2010/11/25 Javascript
javascript获得网页窗口实际大小的示例代码
2013/09/21 Javascript
javascript实现简单的页面右下角提示信息框
2015/07/31 Javascript
js实现iPhone界面风格的单选框和复选框按钮实例
2015/08/18 Javascript
javascript定义类和类的实现实例详解
2015/12/01 Javascript
JS中的数组方法笔记整理
2016/07/26 Javascript
一个超简单的jQuery回调函数例子(分享)
2016/08/08 Javascript
同步异步动态引入js文件的几种方法总结
2016/09/23 Javascript
Vue.js在使用中的一些注意知识点
2017/04/29 Javascript
vue2.0在table中实现全选和反选的示例代码
2017/11/04 Javascript
vue如何判断dom的class
2018/04/26 Javascript
JavaScript显式数据类型转换详解
2019/03/18 Javascript
微信小程序实现简单文字跑马灯
2020/05/26 Javascript
Electron+vue从零开始打造一个本地播放器的方法示例
2020/10/27 Javascript
[47:22]Mineski vs Winstrike 2018国际邀请赛小组赛BO2 第二场 8.16
2018/08/17 DOTA
python机器学习实战之树回归详解
2017/12/20 Python
Django实现celery定时任务过程解析
2020/04/21 Python
Django admin管理工具TabularInline类用法详解
2020/05/14 Python
基于HTML5的WebSocket的实例代码
2018/08/15 HTML / CSS
HTML5标签小集
2011/08/02 HTML / CSS
美国围栏公司:Walpole Outdoors
2019/11/19 全球购物
食堂个人先进事迹
2014/01/22 职场文书
求职信需要的五点内容
2014/02/01 职场文书
《穷人》教学反思
2014/04/08 职场文书
捐款倡议书
2014/04/14 职场文书
协议书与合同的区别
2014/04/18 职场文书
2014年母亲节寄语
2014/05/07 职场文书
校本培训个人总结
2015/02/28 职场文书
党员“一帮一”活动总结
2015/05/07 职场文书
同学聚会感言一句话
2015/07/30 职场文书
JDBC连接的六步实例代码(与mysql连接)
2021/05/12 MySQL
再谈python_tkinter弹出对话框创建
2022/03/20 Python
nginx实现多geoserver服务的负载均衡
2022/05/15 Servers