Home >Backend Development >PHP Tutorial >When using redis to store sessions, are the steps like this?
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>
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