主要介面
連線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");