RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
redis替代php文件存储session的实例-创新互联

redis 替代php文件存储session的实例

广安ssl适用于网站、小程序/APP、API接口等需要进行数据传输应用场景,ssl证书未来市场广阔!成为创新互联的ssl证书销售渠道,可以享受市场价格4-6折优惠!如果有意向欢迎电话联系或者加微信:18980820575(备注:SSL证书合作)期待与您的合作!

查看实例之前请先了解   PHP session_set_save_handler函数的用法

定义个SessionManager  类

class SessionManager {
  private $redis;
  public function __construct(){
    $this->redis = new Redis();
    $this->redis->connect('192.168.0.102', 6379);
    $retval =session_set_save_handler(
      array($this,"open"),
      array($this,"close"),
      array($this,"read"),
      array($this,"write"),
      array($this,"destroy"),
      array($this,"gc")
    );
    session_start();
  }
  public function open($path,$name){
    return true;
  }
  public function close(){
    return true;
  }
  public function read($id){
    $session_value = $this->redis->get($id);
    if($session_value){
      return $session_value;
    }else{
      return "";
    }
  }
  public function write($id,$data){
    if($this->redis->set($id,$data)){
      return true;
    }else{
      return false;
    }
  }
  public function destroy($id){
    if($this->redis->delete($id)){
      return true;
    }else{
      return false;
    }
  }
  public function gc($maxlifetime){
    return true;
  }
  public function __destruct(){
    session_write_close();
  }
}

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


本文标题:redis替代php文件存储session的实例-创新互联
URL地址:http://scyingshan.cn/article/ddodjs.html