php+redis实战留言板(todolist)与互粉功能

2018-06-22 05:35:57来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

目的:通过留言板(todolist)与互粉功能,掌握php操作redis的方法

相关数据操作命令

1,keys * 查看数据库所有的key

2,type + key: 如 type uid     查看数据key的类型

3,批量删除key, 如redis-cli keys "auth:*" | xargs redis-cli del   这行命令在linux终端执行, 删除 auth: 开头的所有key

4。。。。其他基本常用命令, 参照百度或者redis手册

 

0,连接redis

1 <?php
2 
3 $redis = new Redis();
4 $redis->connect( "127.0.0.1", 6379 );
5 $redis->auth( "ghostwu" );
6 
7 ?>

 

1、注册功能( reg.php )

 1 <!doctype html>
 2 <html>
 3     <head>
 4         <meta charset="utf-8" />
 5         <title>用户注册页面</title>
 6     </head>
 7     <body>
 8         <h3>用户注册</h3>
 9         <form action="do_reg.php" method="post">
10             <p>
11                 用户名: <input type="text" name="user" />
12             </p>
13             <p>
14                 密码:<input type="password" name="pwd" />
15             </p>
16             <p>
17                 年龄: <input type="text" name="age" />
18             </p>
19             <p>
20                 <input type="submit" value="注册" />
21                 <input type="reset" value="重置" />
22             </p>
23         </form>
24     </body>
25 </html>

2、处理注册( do_reg.php )

 1 <?php
 2 require "./redis_connect.php";
 3 
 4 $user = $_POST['user'];
 5 $pwd = md5( $_POST['pwd'] );
 6 $age = $_POST['age'];
 7 
 8 $uid = $redis->get( "username:" . $user );
 9 if( empty( $uid ) ) {
10     $uid = $redis->incr( "userid" );
11     $redis->hMset( "user:" . $uid, array( "uid" => $uid, "user" => $user, "pwd" => $pwd, "age" => $age ) );
12     $redis->rpush( "uid", $uid );
13     $redis->set( "username:" . $user, $uid );
14     header( "Location:./list.php" );
15     exit();
16 }else {
17     die( "user already exists " );
18 }
19 
20 ?>

 

3,登录(login.php)

 1 <!doctype html>
 2 <html>
 3     <head>
 4         <meta charset="utf-8" />
 5         <title>用户登录页面</title>
 6     </head>
 7     <body>
 8         <?php
 9             require "./redis_connect.php";
10             if( isset( $_POST['login'] ) || $_POST['login'] == '登录' ) {
11                 $user = $_POST['user'];
12                 $pwd = $_POST['pwd'];
13                 $uid = $redis->get( "username:" . $user );
14                 if( !empty( $uid ) ) {
15                     $db_pwd = $redis->hget( "user:" . $uid, "pwd" );
16                     if( md5( $pwd ) == $db_pwd ) {
17                         $auth = md5( time() . $user . rand() );
18                         $redis->set( "auth:" . $auth, $uid );
19                         setcookie( "auth", $auth, time() + 86400 );
20                         header( "Location:./list.php" );
21                     }else {
22                         echo "<script>alert('用户密码错误');</script>";
23                     }
24                 }else {
25                     echo "<script>alert('该用户不存在');</script>";
26                 }
27             }
28         ?>
29         <h3>用户登录</h3>
30         <form action="" method="post">
31             <p>
32                 用户名: <input type="text" name="user" />
33             </p>
34             <p>
35                 密码:<input type="password" name="pwd" />
36             </p>
37             <p>
38                 <input type="submit" value="登录" name="login" />
39             </p>
40         </form>
41     </body>
42 </html>

 

4,列表页( list.php )

  1 <meta charset="utf-8" />
  2 <?php
  3 require( "./redis_connect.php" );
  4 ?>
  5 <a href="./reg.php">注册</a>
  6 <?php
  7     if( !empty( $_COOKIE['auth'] ) ) {
  8         $login_uid = $redis->get( "auth:" . $_COOKIE['auth'] );
  9         $userName = $redis->hget( "user:" . $login_uid, "user" );
 10 ?>
 11     欢迎您:<?php echo $userName; ?> | <a href="./logout.php">退出</a>
 12 <?php
 13     }else {
 14 ?>
 15     <a href="./login.php">登录</a>
 16 <?php
 17     }
 18 ?>
 19 <?php
 20     $total = $redis->lsize( "uid" );
 21     $pageSize = 3;
 22     $p = isset( $_GET['p'] ) ? $_GET['p'] : 1;
 23     $page = ceil( $total / $pageSize );    
 24     $uids = $redis->lrange( "uid", ( $p - 1 ) * $pageSize, ( ( $p - 1 ) * $pageSize + $pageSize - 1  ) );
 25     $userList = array();
 26     foreach( $uids as $uid ) {
 27         $userList[] = $redis->hgetall( "user:" . $uid );
 28     }
 29 ?>
 30 
 31 <h3>列表数据</h3>
 32 <table>
 33     <tr>
 34         <th>uid</th>
 35         <th>用户名</th>
 36         <th>年龄</th>
 37         <th>操作</th>
 38     </tr>
 39     <?php
 40         foreach( $userList as $user ) {
 41     ?>
 42         <tr>
 43             <td><?php echo $user['uid']; ?></td>
 44             <td><?php echo $user['user']; ?></td>
 45             <td><?php echo $user['age']; ?></td>
 46             <td>
 47                 <a href="delete.php?uid=<?php echo $user['uid']; ?>">删除</a>
 48                 <a href="edit.php?uid=<?php echo $user['uid']; ?>">修改</a>
 49                 <?php
 50                     if( !empty( $_COOKIE['auth'] ) && ( $login_uid != $user['uid'] ) ) {
 51                 ?>
 52                         <a href="./do_fans.php?login_id=<?php echo $login_uid; ?>&uid=<?php echo $user['uid']; ?>">关注</a>
 53                 <?php
 54                     }
 55                 ?>
 56             </td>    
 57         </tr>
 58     <?php
 59         }
 60     ?>
 61         <!--分页开始-->    
 62         <tr>
 63             <td colspan="4">
 64                 <?php
 65                     for( $i = 1; $i <= $page; $i++ ) {
 66                 ?>
 67                         <a href="?p=<?php echo $i; ?>"><?php echo $i; ?></a>
 68                 <?php
 69                     }
 70                 ?>    
 71             </td>        
 72         </tr>    
 73 </table>
 74 
 75 <h3>我关注了谁</h3>
 76 <table>
 77     <tr>
 78         <th>uid</th>
 79         <th>用户名</th>
 80         <th>年龄</th>
 81     </tr>
 82     <?php
 83         $myWatchIds = $redis->smembers( "user:" . $login_uid . ":watch" );
 84         foreach( $myWatchIds as $wId ){
 85             $watchList = $redis->hgetall( "user:" . $wId );
 86 ?>
 87     <tr>
 88         <td><?php echo $watchList['uid']; ?></td>
 89         <td><?php echo $watchList['user']; ?></td>
 90         <td><?php echo $watchList['age']; ?></td>
 91     </tr>
 92 <?php
 93         }
 94     ?>
 95 </table>
 96 <h3>我的fans</h3>
 97 <table>
 98     <tr>
 99         <th>uid</th>
100         <th>用户名</th>
101         <th>年龄</th>
102     </tr>
103     <?php
104         $myFlowerIds = $redis->smembers( "user:" . $login_uid . ":flowers" );
105         foreach( $myFlowerIds as $fId ){
106             $flowerList = $redis->hgetall( "user:" . $fId );
107 ?>
108     <tr>
109         <td><?php echo $flowerList['uid']; ?></td>
110         <td><?php echo $flowerList['user']; ?></td>
111         <td><?php echo $flowerList['age']; ?></td>
112     </tr>
113 <?php
114         }
115     ?>
116 </table>

 

5,编辑 ( edit.php)

 1 <meta charset="utf-8" />
 2 <?php
 3     require "./redis_connect.php";
 4     $uid = intval( $_GET['uid'] );
 5     if( empty( $uid ) ){
 6         header( "Location:./list.php" );
 7         exit();
 8     }
 9     $userInfo = $redis->hgetall( "user:" . $uid );
10 ?>
11 
12 <form action="do_edit.php" method="post">
13     <p>
14         用户名: <input disabled type="text" name="user" value="<?php echo $userInfo['user']; ?>" />
15     </p>
16     <p>
17         年龄:<input type="text" name="age" value="<?php echo $userInfo['age']; ?>" />
18     </p>
19     <p>
20         <input type="submit" value="修改" name="edit" />
21     </p>
22     <input type="hidden" value="<?php echo $userInfo['uid']; ?>" name="uid" />
23 </form>

 

6,处理更新( do_edit.php )

 1 <?php
 2     require( "./redis_connect.php" );
 3     $uid = intval( $_POST['uid'] );
 4     $age = $_POST['age'];
 5 
 6     if( empty( $uid ) ) {
 7         header( "Location:./edit.php" );
 8         exit();
 9     }
10     $res = $redis->hmset( "user:". $uid, array( "age" => $age ) );
11     if( $res ) {
12         header( "Location:./list.php" );
13     }else {
14         header( "Location:./edit.php" );
15     }
16     exit();
17 ?>

 

7,删除(delete.php)

 1 <?php
 2     require( "./redis_connect.php" );
 3     $uid = intval( $_GET['uid'] );
 4     if( empty( $uid ) ) {
 5         header( "Location:./list.php" );
 6         exit();
 7     }
 8     $userName = $redis->get( "user:" . $uid );
 9     $redis->del( "user:" . $uid );
10     $redis->del( "username:" . $userName );
11     $redis->lrem( "uid", $uid );
12     header( "Location:./list.php" );
13 ?>

 

8,注销( logout.php )

1 <?php
2     require "./redis_connect.php";
3     if( isset( $_COOKIE['auth'] ) ){
4         $redis->del( "auth:" . $_COOKIE['auth'] );
5         setcookie( "auth", "", time() - 86400 );
6         header( "Location:./list.php" );
7         exit();
8     }
9 ?>

 

9,互粉( do_fans.php )

 1 <?php
 2     require "./redis_connect.php";
 3     $login_id = intval( $_GET['login_id'] );
 4     $uid = intval( $_GET['uid'] );
 5     if( empty( $login_id ) || empty( $uid ) ) {
 6         header( "Location:./list.php" );            
 7         exit();
 8     }
 9     //当前用户关注
10     $redis->sadd( "user:" . $login_id . ":watch", $uid );
11     //被当前用户关注
12     $redis->sadd( "user:" . $uid . ":flowers", $login_id );
13     header( "Location:./list.php" );            
14     exit();
15 ?>

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:php设计模式-策略模式

下一篇:php设计模式-单例模式