php+redis實現(xiàn)注冊、刪除、編輯、分頁、登錄、關(guān)注等功能示例
本文實例講述了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,相當于主鍵
$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;
//當前頁碼
$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的最大值來當總個數(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>
當前<?php echo $page_num;?>頁
總共<?php echo $page_count;?>頁
總共<?php echo $count;?>個用戶
</td>
</tr>
</table>
<!--關(guān)注功能,建議用集合實現(xià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),因為集合元素唯一,并且可以容易求出兩個用戶粉絲之間交集與差集,進而進行好友推薦功能
$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");
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php+redis數(shù)據(jù)庫程序設(shè)計技巧總結(jié)》、《PHP擴展開發(fā)教程》、《php+mysql數(shù)據(jù)庫操作入門教程》、《php+mysqli數(shù)據(jù)庫程序設(shè)計技巧總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家PHP程序設(shè)計有所幫助。
相關(guān)文章
php實現(xiàn)阿拉伯數(shù)字和羅馬數(shù)字相互轉(zhuǎn)換的方法
這篇文章主要介紹了php實現(xiàn)阿拉伯數(shù)字和羅馬數(shù)字相互轉(zhuǎn)換的方法,涉及php字符串操作的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-04-04
PHP函數(shù)按引用傳遞參數(shù)及函數(shù)可選參數(shù)用法示例
這篇文章主要介紹了PHP函數(shù)按引用傳遞參數(shù)及函數(shù)可選參數(shù)用法,結(jié)合實例形式分析了php函數(shù)的引用傳參與可選參數(shù)具體使用技巧與注意事項,需要的朋友可以參考下2018-06-06
PHP使用PHPExcel讀取excel數(shù)據(jù)并批量上傳到數(shù)據(jù)庫
這篇文章主要為大家詳細介紹了PHP如何使用PHPExcel讀取excel數(shù)據(jù)并批量上傳到數(shù)據(jù)庫,文中的示例代碼講解詳細,感興趣的小伙伴可以參考一下2024-12-12
PHP使用array_merge重新排列數(shù)組下標的方法
這篇文章主要介紹了PHP使用array_merge重新排列數(shù)組下標的方法,以實例形式較為詳細的分析了array_merge的功能,及針對數(shù)組下標進行重新排列操作的實現(xiàn)技巧,需要的朋友可以參考下2015-07-07

