zoukankan      html  css  js  c++  java
  • PHP封装 Session

    <?php
    namespace Lib;
    class Session{
        private $mypdo;
        public function __construct() {
            session_set_save_handler(
                [$this,'open'],
                [$this,'close'],
                [$this,'read'],
                [$this,'write'],
                [$this,'destroy'],
                [$this,'gc']
            );
            session_start();
        }
        public function open() {
            $this->mypdo= CoreMyPDO::getInstance($GLOBALS['config']['database']);
            return true;
        }
        //关闭会话
        public function close() {
            return true;
        }
        //读取会话
        public function read($sess_id) {
            $sql="select sess_value from sess where sess_id='$sess_id'";
            return (string)$this->mypdo->fetchColumn($sql);
        }
        //写入会话
        public function write($sess_id,$sess_value) {
            $sql="insert into sess values ('$sess_id','$sess_value',unix_timestamp()) on duplicate key update sess_value='$sess_value',sess_time=unix_timestamp()";
            return $this->mypdo->exec($sql)!==false;
        }
        //销毁会话
        public function destroy($sess_id) {
            $sql="delete from sess where sess_id='$sess_id'";
            return $this->mypdo->exec($sql)!==false;
        }
        //垃圾回收
        public function gc($lifetime) {
            $expires=time()-$lifetime;    //过期时间点
            $sql="delete from sess where sess_time<$expires";
            return $this->mypdo->exec($sql)!==false;
        }
    }
    ?>
  • 相关阅读:
    centos 6 安装
    DNS介绍
    Saltstack远程执行(四)
    Saltstack数据系统Grains和Pillar(三)
    array_multisort 二维数组排序
    jqgit...
    Redis 创建多个端口 链接redis端口
    百度商桥回话接口
    加ico
    redis 新开端口号
  • 原文地址:https://www.cnblogs.com/SharkJiao/p/14165514.html
Copyright © 2011-2022 走看看