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
Release: 2016-07-06 13:53:01
Original
1030 people have browsed it

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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template