Home  >  Article  >  Backend Development  >  使用redis储存session的时候,步骤是这样的吗

使用redis储存session的时候,步骤是这样的吗

WBOY
WBOYOriginal
2016-07-06 13:53:01971browse

使用redis来保存session的时候,也就是只使用session_id对吗,不在session里面真实存值?

<code>                //获取session_id
                $sid=session_id();

                \Predis\Autoloader::register();
                $a = new \Predis\Client();
                $a->set('sid',$sid);
                $a->set('uid','100');</code>

获取值的时候

<code>     $sidfrompost=I('post.sid');

        \Predis\Autoloader::register();
        $a = new \Predis\Client();
        $sidfromredis=$a->get('sid');

        if($sidfrompost!=$sidfromredis){
            $this->ajaxReturn(array("result"=>"非法访问"));
           exit;
        }else{
            $this->ajaxReturn(array("uid"=>$a->get('uid'),"sid"=>$sidfromredis,"session"=>session()));
        }
        </code>

回复内容:

使用redis来保存session的时候,也就是只使用session_id对吗,不在session里面真实存值?

<code>                //获取session_id
                $sid=session_id();

                \Predis\Autoloader::register();
                $a = new \Predis\Client();
                $a->set('sid',$sid);
                $a->set('uid','100');</code>

获取值的时候

<code>     $sidfrompost=I('post.sid');

        \Predis\Autoloader::register();
        $a = new \Predis\Client();
        $sidfromredis=$a->get('sid');

        if($sidfrompost!=$sidfromredis){
            $this->ajaxReturn(array("result"=>"非法访问"));
           exit;
        }else{
            $this->ajaxReturn(array("uid"=>$a->get('uid'),"sid"=>$sidfromredis,"session"=>session()));
        }
        </code>

可以这样做的一个set一个get支持跨域,你能拿到最好,跨域的话你的redis的就要开启外部端口,为了安全请你设置redis的登录密码

其实你没必要一下子问这么多问题,善用搜索,我Google搜索“ThinkPHP session redis"第一个就是CSDN博客的文章:传送门

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn