Home >Backend Development >PHP Tutorial >When using redis to store sessions, are the steps like this?

When using redis to store sessions, are the steps like this?

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

When using redis to save the session, is it correct to only use the session_id and not actually store the value in the session?

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

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

When getting the value

<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>

Reply content:

When using redis to save the session, is it correct to only use the session_id and not actually store the value in the session?

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

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

When getting the value

<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>

You can do this, one set and one get to support cross-domain, you can get the best. If it is cross-domain, your redis must open the external port. For security, please set the login password of redis

Actually, you don’t need to ask so many questions at once. Make good use of search. When I searched for “ThinkPHP session redis” on Google, the first one was the CSDN blog article: Portal

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