您好,登錄后才能下訂單哦!
這篇文章主要介紹了php+redis怎么實現(xiàn)注冊、刪除、編輯、分頁、登錄、關(guān)注等功能,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
具體如下:
主要界面
連接redis
redis.php
<?php //實例化 $redis = new Redis(); //連接服務(wù)器 $a=$redis->connect("localhost",6379); //var_dump($a); //授權(quán) $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>
注冊實現(xiàn)
reg.php
<?php require("redis.php"); $username = $_POST['username']; $password = md5($_POST['password']); $age = $_POST['age']; //echo $username.$password.$age; $uid = $redis->incr("userid");//設(shè)置自增id,相當(dāng)于主鍵 $redis->hMset("user:".$uid,array("uid"=>$uid,"username"=>$username,"password"=>$password,"age"=>$age));//用hash類型存儲用戶比較方便 //將用戶id存入一個鏈表中,便于統(tǒng)計數(shù)據(jù) $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"); //用戶總數(shù) $count = $redis->lsize("uid");//獲取鏈表的長度 //echo $count; //頁大小 $page_size = 3; //當(dāng)前頁碼 $page_num=(!empty($_GET['page']))?$_GET['page']:1; //頁總數(shù) $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); } /* //以下為最初想到的分頁處理,放入一個數(shù)組中,根據(jù)uid的最大值來當(dāng)總個數(shù),但是刪除個別用戶以后,uid不會變小,所以建議用鏈表,因為他有個lsize函數(shù)可以求出鏈表長度 //根據(jù)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" >加關(guān)注</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> 當(dāng)前<?php echo $page_num;?>頁 總共<?php echo $page_count;?>頁 總共<?php echo $count;?>個用戶 </td> </tr> </table> <!--關(guān)注功能,建議用集合實現(xiàn),因為集合元素唯一,并且可以容易求出兩個用戶粉絲之間交集與差集,進(jìn)而進(jìn)行好友推薦功能--> <table border=1> <caption>我關(guān)注了誰</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']; //根據(jù)注冊時存儲的以用戶名為鍵的字符類型中查找用戶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); }
加關(guān)注
addfans.php
<?php //關(guān)注功能,建議用集合實現(xiàn),因為集合元素唯一,并且可以容易求出兩個用戶粉絲之間交集與差集,進(jìn)而進(jìn)行好友推薦功能 $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");
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“php+redis怎么實現(xiàn)注冊、刪除、編輯、分頁、登錄、關(guān)注等功能”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。