您好,登錄后才能下訂單哦!
這篇文章主要介紹了php+redis怎么實現注冊、刪除、編輯、分頁、登錄、關注等功能,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
具體如下:
主要界面
連接redis
redis.php
<?php //實例化 $redis = new Redis(); //連接服務器 $a=$redis->connect("localhost",6379); //var_dump($a); //授權 $redis->auth("107lab");
注冊界面
add.php
<form action="reg.php" 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="重填"> </form>
注冊實現
reg.php
<?php require("redis.php"); $username = $_POST['username']; $password = md5($_POST['password']); $age = $_POST['age']; //echo $username.$password.$age; $uid = $redis->incr("userid");//設置自增id,相當于主鍵 $redis->hMset("user:".$uid,array("uid"=>$uid,"username"=>$username,"password"=>$password,"age"=>$age));//用hash類型存儲用戶比較方便 //將用戶id存入一個鏈表中,便于統計數據 $redis->rpush("uid",$uid); //將用id存入以用戶名為鍵的字符類型中,便于查看用戶是否存在。 $redis->set("username:".$username,$uid); header('location:list.php');
列表頁面
list.php
<a href="add.php" rel="external nofollow" >注冊</a> <?php require("redis.php"); if(!empty($_COOKIE['auth'])){ $id = $redis->get("auth:".$_COOKIE['auth']); $name = $redis->hget("user:".$id,"username"); ?> 歡迎您:<?php echo $name;?> <a href="logout.php" rel="external nofollow" >退出</a> <?php } else { ?> <a href="login.php" rel="external nofollow" >登錄</a> <?php } ?> <?php require("redis.php"); //用戶總數 $count = $redis->lsize("uid");//獲取鏈表的長度 //echo $count; //頁大小 $page_size = 3; //當前頁碼 $page_num=(!empty($_GET['page']))?$_GET['page']:1; //頁總數 $page_count = ceil($count/$page_size); $ids = $redis->lrange("uid",($page_num-1)*$page_size,(($page_num-1)*$page_size+$page_size-1)); //var_dump($ids); foreach($ids as $v){ $data[]=$redis->hgetall("user:".$v); } /* //以下為最初想到的分頁處理,放入一個數組中,根據uid的最大值來當總個數,但是刪除個別用戶以后,uid不會變小,所以建議用鏈表,因為他有個lsize函數可以求出鏈表長度 //根據userid獲取所有用戶 for($i=1;$i<=($redis->get("userid"));$i++){ $data[]=$redis->hgetall("user:".$i); } //過濾空值 $data = array_filter($data); //var_dump($data); */ ?> <table border=1> <tr> <th>uid</th> <th>username</th> <th>age</th> <th>操作</th> </tr> <?php foreach($data as $v){ ?> <tr> <td><?php echo $v['uid']?></td> <td><?php echo $v['username']?></td> <td><?php echo $v['age']?></td> <td> <a href="del.php?id=<?php echo $v['uid'];?>" rel="external nofollow" >刪除</a> <a href="mod.php?id=<?php echo $v['uid'];?>" rel="external nofollow" >編輯</a> <?php if(!empty($_COOKIE['auth']) && $id != $v['uid']){ ?> <a href="addfans.php?id=<?php echo $v['uid'];?>&uid=<?php echo $id;?>" rel="external nofollow" >加關注</a> <?php } ?> </td> </tr> <?php } ?> <tr> <td colspan="4"> <?php if(($page_num-1)>=1){ ?> <a href="?page=<?php echo ($page_num-1);?>" rel="external nofollow" >上一頁</a> <?php } ?> <?php if(($page_num+1)<=$page_count){ ?> <a href="?page=<?php echo ($page_num+1);?>" rel="external nofollow" >下一頁</a> <?php } ?> <a href="?page=1" rel="external nofollow" >首頁</a> <a href="?page=<?php echo ($page_count);?>" rel="external nofollow" >尾頁</a> 當前<?php echo $page_num;?>頁 總共<?php echo $page_count;?>頁 總共<?php echo $count;?>個用戶 </td> </tr> </table> <!--關注功能,建議用集合實現,因為集合元素唯一,并且可以容易求出兩個用戶粉絲之間交集與差集,進而進行好友推薦功能--> <table border=1> <caption>我關注了誰</caption> <?php $data = $redis->smembers("user:".$id.":following"); foreach($data as $v){ $row = $redis->hgetall("user:".$v); ?> <tr> <td><?php echo $row['uid'];?></td> <td><?php echo $row['username'];?></td> <td><?php echo $row['age'];?></td> </tr> <?php } ?> <table> <table border=1> <caption>我的粉絲</caption> <?php $data = $redis->smembers("user:".$id.":followers"); foreach($data as $v){ $row = $redis->hgetall("user:".$v); ?> <tr> <td><?php echo $row['uid'];?></td> <td><?php echo $row['username'];?></td> <td><?php echo $row['age'];?></td> </tr> <?php } ?> <table>
退出
logout.php
<?php setcookie("auth","",time()-1); header("location:list.php");
登錄
login.php
<?php require("redis.php"); $username = $_POST['username']; $pass = $_POST['password']; //根據注冊時存儲的以用戶名為鍵的字符類型中查找用戶id $id = $redis->get("username:".$username); if(!empty($id)){ $password = $redis->hget("user:".$id,"password"); if(md5($pass) == $password){ $auth = md5(time().$username.rand()); $redis->set("auth:".$auth,$id); setcookie("auth",$auth,time()+86400); header("location:list.php"); } } ?> <form action="" method="post"> 用戶名:<input type="text" name="username"/><br> 密碼:<input type="password" name="password"><br> <input type="submit" value="登錄"/> </form>
刪除
del.php
<?php require("redis.php"); $uid = $_GET['id']; //echo $uid; $username = $redis->hget("user:".$id,"username"); $a=$redis->del("user:".$uid); $redis->del("username:".$username); $redis->lrem("uid",$uid); //var_dump($a); header("location:list.php");
編輯界面
mod.php
<?php require("redis.php"); $uid = $_GET['id']; $data=$redis->hgetall("user:".$uid); ?> <form action="doedit.php" method="post"> <input type="hidden" value="<?php echo $data['uid'];?>" name="uid"> 用戶名:<input type="text" name="username" value="<?php echo $data['username'];?>"><br> 年齡:<input type="text" name="age" value="<?php echo $data['age'];?>"><br> <input type="submit" value="提交"> <input type="reset" value="重填"> </form>
編輯功能
doedit.php
<?php require('redis.php'); $uid = $_POST['uid']; $username = $_POST['username']; $age = $_POST['age']; $a=$redis->hmset("user:".$uid,array("username"=>$username,"age"=>$age)); if($a){ header("location:list.php"); }else{ header("location:mod.php?id=".$uid); }
加關注
addfans.php
<?php //關注功能,建議用集合實現,因為集合元素唯一,并且可以容易求出兩個用戶粉絲之間交集與差集,進而進行好友推薦功能 $id = $_GET['id']; $uid = $_GET['uid']; require("redis.php"); $redis->sadd("user:".$uid.":following",$id); $redis->sadd("user:".$id.":followers",$uid); header("location:list.php");
感謝你能夠認真閱讀完這篇文章,希望小編分享的“php+redis怎么實現注冊、刪除、編輯、分頁、登錄、關注等功能”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。