連接redis代碼redis.php
<?php //實例化 $redis = new Redis(); //連接服務器 $redis->connect("localhost"); //授權 $redis->auth("lamplijie");
說明:這是入口文件,使用redis數據庫必須先引入它。這樣才能用php操作redis數據庫。
列表頁list.php
<a href="add.php">注冊</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">退出</a> <?php }else{ ?> <a href="login.php">登陸</a> <?php } //用戶總數 $count = $redis->lsize("uid"); //頁大小 $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); /*for($i=1;$i<=($redis->get("userid"));$i++){ $data[] = $redis->hgetall("user:".$i); }*/ foreach($ids as $v){ $data[] = $redis->hgetall("user:".$v); } //var_dump($data); //$data = array_filter($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']?>">刪除</a> <a href="mod.php?id=<?php echo $v['uid']?>">編輯</a> <?php if(!empty($_COOKIE['auth'])&&$id!=$v['uid']){?> <a href="addfans.php?id=<?php echo $v['uid']?>&uid=<?php echo $id?>">加關注</a></td> <?php }?> </tr> <?php }?> <tr> <td colspan="4"> <a href="?page=<?php echo (($page_num-1)<=1)?1:($page_num-1) ?>">上一頁</a> <a href="?page=<?php echo (($page_num+1)>=$page_count)?$page_count:($page_num+1) ?>">下一頁</a> <a href="?page=1">首頁</a> <a href="?page=<?php echo $page_count?>">尾頁</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>
說明:相關功能和連接都在這里體現出來
添加靜態頁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頁面中保存處理。
處理添加頁reg.php
<?php require("redis.php"); $username = $_POST['username']; $password = md5($_POST['password']); $age = $_POST['age']; $uid = $redis->incr("userid"); $redis->hmset("user:".$uid,array("uid"=>$uid,"username"=>$username,"password"=>$password,"age"=>$age)); $redis->rpush("uid",$uid); $redis->set("username:".$username,$uid); header("location:list.php");
說明:注冊成功后,也就是添加成功后跳轉到list頁面,這是一般的流程,在這里有所體現。這里是選擇用hash來保存用戶信息,類似於sql中的表。同時將id保存到隊列中,方便分頁的時候統計個數。
修改靜態頁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>
說明:修改頁面,首先獲取id,用了hgetall方法,獲取用戶信息並展示出來。
處理添加信息頁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); }
說明:通過post獲取信息,並且修改之hmset,修改成功進行跳轉。
刪除頁del.php
<?php require("redis.php"); $uid = $_GET['id']; $redis->del("user:".$uid); $redis->lrem("uid",$uid); header("location:list.php");
說明:刪除hash表中的數據,和隊列中的數據。
登錄頁login.php
<?php require("redis.php"); $username = $_POST['username']; $pass = $_POST['password']; $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>
說明:獲取數據並判斷用戶名是否存在,以及密碼是否正確,如果正確,保存到cookie中。
登出頁logout.php
<?php setcookie("auth","",time()-1); header("location:list.php");
說明:清除cookie中的數據。
添加粉絲頁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");
說明:一切都跟uid關聯起來,這里的關注與粉絲用集合來存儲。非常的合理。
小結:redis的使用,需要轉變思維。它與傳統的數據庫還是有區別的,要能夠巧妙的使用它的存儲數據的幾種類型。string、hash、list、set等等。來實現相關的功能。