<?php //實例化 $redis = new Redis(); //鏈接服務器 $redis->connect("localhost"); //受權 $redis->auth("lamplijie");
<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>
<?php require("redis.php"); $username = $_POST['username']; $password = md5($_POST['password']); $age = $_POST['age']; echo $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");
<a href="add.php">註冊</a> <?php require("redis.php"); if(!emptyempty($_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 = (!emptyempty($_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(!emptyempty($_COOKIE['auth']) && $id!=$v['uid']){?> <a href="addfans.php?id=<?php echo $v['uid']?>&uid=<?php echo $id?>">加關注</a> <?php } ?> </td> </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 } ?>
<?php require("redis.php"); $uid = $_GET['id']; $redis->del("user:".$uid); $redis->lrem("uid",$uid); header("localhost:list.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>
<?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); }
<?php require("redis.php"); $username = $_POST['username']; $pass = $_POST['password']; $id = $redis->get("username:".$username); if(!emptyempty($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>
<?php setcookie("auth","",time()-1); header("location:list.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");
固然,採用sdiff user:1:following user:2:following語句,用戶1能夠向用戶2推薦關注(即用戶1的關注與用戶2的關注的差集)。php