redis+thinkphp5的注册、登陆、关注基础例子
最近初步接触redis,结合thinkphp5与redis,写了一个用户注册的基础例子,用于学习。
这个例子是结合了兄弟连的redis视频,最后两节的内容写的:
https://study.163.com/course/courseMain.htm?courseId=265010
这个例子实现了以下功能:
(1)用户注册
(2)用户登陆
(3)redis+thinkphp5的数据的增加,修改,删除
(4)redis翻页功能
(5)用户的关注列表+粉丝列表
案例需要搭建的环境需求:
(1)thinkphp5
(2)redis
(3)phpredis(windows环境下也可以)
(4)redis desktop manager (用于观察redis server 的数据存储情况,当然这个不安装也可以)
说明:
(1)这个例子,主要是使用了phpredis的原生函数命令进行的redis的数据的增加,删除,修改等操作,并没有丰富thinkphp中的redis类库函数。
(2)涉及到了string,hash,set ,list数据类型,用于加深对redis数据类型的学习。
thinkphp5 控制器的文件代码:
namespace app\home\controller; use think\Controller; class Test extends Base{ public function index() { $redis = new \Redis(); $redis->connect('127.0.0.1', 6379); if(IS_POST){ $username = I('post.username'); $password = md5(I('post.password')); $age = I('post.age'); $uid = $redis->incr("userid");//$uid自增操作 $redis->hMset("user:$uid",array("uid"=>$uid,"username"=>$username,"password"=>$password,"age"=>$age)); //把用户结构存在链表里,每次注册一个用户,就向链表中插入一个用户 $redis->rPush("uid",$uid); $redis->set("username:".$username,$uid); $this->success('注册成功', 'Test/list_content'); } return $this->fetch(); } public function list_content() { $redis = new \Redis(); $redis->connect('127.0.0.1', 6379); //var_dump($data); //用户总数 $count = $redis->lSize("uid"); //echo $count = $redis->lSize("uid"); //页容量 $page_size = 3; //当前页码: $page_num = (!empty(I('get.page')))?I('get.page'):1; $page_num_pre = (($page_num-1)<=1)?1:($page_num-1);//上一页 //页总数 $page_count = ceil($count/$page_size); $page_num_next = (($page_num+1)>=$page_count)?$page_count:($page_num+1);//下一页 $ids = $redis->lrange("uid",($page_num-1)*$page_size,($page_num-1)*$page_size+$page_size-1); //http://www.c41.c4/home/Test/list_content/page/2 // var_dump($ids); // for($i=1;$i<=($redis->get("userid"));$i++){ // $data[] = $redis->hgetall("user:".$i); // }; foreach ($ids as $v){ $data[] = $redis->hgetall("user:".$v); } if(!empty($_COOKIE['auth'])){ $welcome = "您已登陆"; $id = $redis->get("auth:".$_COOKIE['auth']); $name = $redis->hget("user:".$id,"username"); $logout = '<a href="/home/Test/logout.html">退出登陆</a>'; }else{ $welcome = '<a href="/home/Test/login.html">登陆页面</a>'; } if(!empty($_COOKIE['auth'])){ $c=0; } // $data = array_filter($data); $follow_data = $redis->sMembers("user:".$id.":following"); foreach ($follow_data as $v){ $row[] = $redis->hGetAll("user:".$v); } $fans_data = $redis->sMembers("user:".$id.":followers"); foreach ($fans_data as $v){ $row2[] = $redis->hGetAll("user:".$v); } $this->assign('page_num',$page_num); $this->assign('page_num_pre',$page_num_pre); $this->assign('page_num_next',$page_num_next); $this->assign('page_count',$page_count); $this->assign('count',$count); $this->assign('data',$data); $this->assign('row',$row); $this->assign('row2',$row2); $this->assign('welcome',$welcome); $this->assign('name',$name); $this->assign('logout',$logout); $this->assign('c',$c); $this->assign('id',$id); return $this->fetch(); } public function logout() { setcookie("auth","",time()-1); $this->success('退出成功', 'Test/index'); } public function addfan() { $redis = new \Redis(); $redis->connect('127.0.0.1', 6379); $id = I('get.id');//目标用户id $uid = I('get.uid');//当前已登陆用户,自己的id $redis->sAdd("user:".$uid.":following",$id);//关注用户集合,例:user:2:following =$id $redis->sAdd("user:".$id.":followers",$uid);//目标用户的粉丝,就是已登陆用户自己的id,即uid $this->redirect('Test/list_content'); } public function del() { $redis = new \Redis(); $redis->connect('127.0.0.1', 6379); $uid = I('get.id'); $redis->del("user:".$uid); $redis->lrem("uid",$uid); $this->success('删除成功', 'Test/list_content'); return $this->fetch(); } public function edit() { $redis = new \Redis(); $redis->connect('127.0.0.1', 6379); $uid = I('get.id'); $data = $redis->hgetall("user:".$uid); //var_dump($data); $this->assign('data',$data); return $this->fetch(); } public function doedit() { $redis = new \Redis(); $redis->connect('127.0.0.1', 6379); $uid = I('get.id'); $username = I('post.username'); $password = md5(I('post.password')); $age = I('post.age'); $a = $redis->hmset("user:".$uid,array("username"=>$username,"password"=>$password,"age"=>$age)); if($a){ $this->success('编辑成功', 'Test/list_content'); }else{$this->error('编辑失败', 'Test/edit');} return $this->fetch(); } public function login() { $redis = new \Redis(); $redis->connect('127.0.0.1', 6379); $username = I('post.username'); $password = md5(I('post.password')); $id = $redis->get("username:".$username); if(!empty($id)){ $pass = $redis->hget("user:".$id,password); if($password == $pass){ $auth = md5(time().$username.rand()); $redis->set("auth:".$auth,$id); setcookie("auth",$auth,time()+86400); $this->success('登陆成功','Test/list_content'); } } return $this->fetch(); } }
模板文件:
注册页面,index.html
<html> <head> <meta charset="UTF-8"> <title>测试模块</title> <script src="__STATIC__/js/jquery-3.2.1.js" type="text/javascript" charset="utf-8"></script> </head> <body> <form action="" method="post"> 用户名:<input type="text" name="username"/><br/> 密码:<input type="password" name="password"/><br/> 年龄:<input type="text" name="age"/><br/> <input type="submit" value="注册"/> <input type="reset" value="重新填写"/> <a href="/home/Test/login.html">登陆</a><br/> </form> </body> </html>
登陆页面,login.html
<html> <head> <meta charset="UTF-8"> <title>登陆-测试模块</title> <script src="__STATIC__/js/jquery-3.2.1.js" type="text/javascript" charset="utf-8"></script> </head> <body> <form action="" method="post"> 用户名:<input type="text" name="username"/><br/> 密码:<input type="password" name="password"/><br/> <input type="submit" value="登陆"/> <input type="reset" value="重新填写"/><br/> </form> </body> </html>
list列表页,list_content.html
<html> <head> <meta charset="UTF-8"> <title>测试模块-list</title> <script src="__STATIC__/js/jquery-3.2.1.js" type="text/javascript" charset="utf-8"></script> </head> <body> <a href="/home/Test.html">注册页面</a> {$welcome},{$name} {$logout}<br><br> <table border="1"> <tr> <th>uid</th> <th>username</th> <th>age</th> <th>操作</th> </tr> <volist name="data" id="vo"> <tr> <td>{$vo.uid}</td> <td>{$vo.username}</td> <td>{$vo.age}</td> <td><a href="/home/test/del/id/{$vo.uid}.html">删除</a> <a href="/home/test/edit/id/{$vo.uid}.html">编辑</a> <if condition="($c eq 0)AND($id eq $vo.uid)"> <else /> <a href="/home/test/addfan/id/{$vo.uid}/uid/{$id}.html">关注</a> </if> </td> </tr> </volist> <tr> <td colspan="4"> <a href="/home/test/list_content/page/{$page_num_pre}.html">上一页</a> <a href="/home/test/list_content/page/{$page_num_next}.html">下一页</a> <a href="/home/test/list_content/page/1.html">首页</a> <a href="/home/test/list_content/page/{$page_count}.html">尾页</a> 当前第{$page_num}页 总共{$page_count}页 总共{$count}个用户 </td> </tr> </table> <br> <br> <table border="1"> <caption>关注列表</caption> <tr> <th>uid</th> <th>username</th> <th>age</th> <th>操作</th> </tr> <volist name="row" id="vo"> <tr> <td>{$vo.uid}</td> <td>{$vo.username}</td> <td>{$vo.age}</td> <td>取消关注</td> </tr> </volist> </table> <br> <br> <table border="1"> <caption>我的粉丝</caption> <tr> <th>uid</th> <th>username</th> <th>age</th> <th>操作</th> </tr> <volist name="row2" id="vo"> <tr> <td>{$vo.uid}</td> <td>{$vo.username}</td> <td>{$vo.age}</td> <td>取消关注</td> </tr> </volist> </table> </body> </html>
数据编辑页面,edit.html
<html> <head> <meta charset="UTF-8"> <title>编辑-测试模块</title> <script src="__STATIC__/js/jquery-3.2.1.js" type="text/javascript" charset="utf-8"></script> </head> <body> <form action="/home/test/doedit/id/{$data['uid']}" method="post"> 用户名:<input type="text" name="username" value="{$data['username']}"/><br/> 密码:<input type="password" name="password" value="{$data['password']}"/><br/> 年龄:<input type="text" name="age" value="{$data['age']}"/><br/> <input type="submit" value="提交"/> <input type="reset" value="重新填写"/><br/> </form> </body> </html>
纯PHP版源码:https://gitee.com/omotec_bv/codes/rcp5s8u7do1x6lki0vhmw83
本示例代码:https://gitee.com/omotec_bv/codes/lyrn32ao0pw45efuihj9q61
---------------------
作者:sinat_34469308
来源:CSDN
原文:https://blog.csdn.net/sinat_34469308/article/details/83153764
版权声明:本文为博主原创文章,转载请附上博文链接!