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

目的:通过留言板(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

 <?php

 $redis = new Redis();
$redis->connect( "127.0.0.1", 6379 );
$redis->auth( "ghostwu" ); ?>

1、注册功能( reg.php )

 <!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>用户注册页面</title>
</head>
<body>
<h3>用户注册</h3>
<form action="do_reg.php" method="post">
<p>
用户名: <input type="text" name="user" />
</p>
<p>
密码:<input type="password" name="pwd" />
</p>
<p>
年龄: <input type="text" name="age" />
</p>
<p>
<input type="submit" value="注册" />
<input type="reset" value="重置" />
</p>
</form>
</body>
</html>

2、处理注册( do_reg.php )

 <?php
require "./redis_connect.php"; $user = $_POST['user'];
$pwd = md5( $_POST['pwd'] );
$age = $_POST['age']; $uid = $redis->get( "username:" . $user );
if( empty( $uid ) ) {
$uid = $redis->incr( "userid" );
$redis->hMset( "user:" . $uid, array( "uid" => $uid, "user" => $user, "pwd" => $pwd, "age" => $age ) );
$redis->rpush( "uid", $uid );
$redis->set( "username:" . $user, $uid );
header( "Location:./list.php" );
exit();
}else {
die( "user already exists " );
} ?>

3,登录(login.php)

 <!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>用户登录页面</title>
</head>
<body>
<?php
require "./redis_connect.php";
if( isset( $_POST['login'] ) || $_POST['login'] == '登录' ) {
$user = $_POST['user'];
$pwd = $_POST['pwd'];
$uid = $redis->get( "username:" . $user );
if( !empty( $uid ) ) {
$db_pwd = $redis->hget( "user:" . $uid, "pwd" );
if( md5( $pwd ) == $db_pwd ) {
$auth = md5( time() . $user . rand() );
$redis->set( "auth:" . $auth, $uid );
setcookie( "auth", $auth, time() + 86400 );
header( "Location:./list.php" );
}else {
echo "<script>alert('用户密码错误');</script>";
}
}else {
echo "<script>alert('该用户不存在');</script>";
}
}
?>
<h3>用户登录</h3>
<form action="" method="post">
<p>
用户名: <input type="text" name="user" />
</p>
<p>
密码:<input type="password" name="pwd" />
</p>
<p>
<input type="submit" value="登录" name="login" />
</p>
</form>
</body>
</html>

4,列表页( list.php )

 <meta charset="utf-8" />
<?php
require( "./redis_connect.php" );
?>
<a href="./reg.php">注册</a>
<?php
if( !empty( $_COOKIE['auth'] ) ) {
$login_uid = $redis->get( "auth:" . $_COOKIE['auth'] );
$userName = $redis->hget( "user:" . $login_uid, "user" );
?>
欢迎您:<?php echo $userName; ?> | <a href="./logout.php">退出</a>
<?php
}else {
?>
<a href="./login.php">登录</a>
<?php
}
?>
<?php
$total = $redis->lsize( "uid" );
$pageSize = 3;
$p = isset( $_GET['p'] ) ? $_GET['p'] : 1;
$page = ceil( $total / $pageSize );
$uids = $redis->lrange( "uid", ( $p - 1 ) * $pageSize, ( ( $p - 1 ) * $pageSize + $pageSize - 1 ) );
$userList = array();
foreach( $uids as $uid ) {
$userList[] = $redis->hgetall( "user:" . $uid );
}
?> <h3>列表数据</h3>
<table>
<tr>
<th>uid</th>
<th>用户名</th>
<th>年龄</th>
<th>操作</th>
</tr>
<?php
foreach( $userList as $user ) {
?>
<tr>
<td><?php echo $user['uid']; ?></td>
<td><?php echo $user['user']; ?></td>
<td><?php echo $user['age']; ?></td>
<td>
<a href="delete.php?uid=<?php echo $user['uid']; ?>">删除</a>
<a href="edit.php?uid=<?php echo $user['uid']; ?>">修改</a>
<?php
if( !empty( $_COOKIE['auth'] ) && ( $login_uid != $user['uid'] ) ) {
?>
<a href="./do_fans.php?login_id=<?php echo $login_uid; ?>&uid=<?php echo $user['uid']; ?>">关注</a>
<?php
}
?>
</td>
</tr>
<?php
}
?>
<!--分页开始-->
<tr>
<td colspan="4">
<?php
for( $i = 1; $i <= $page; $i++ ) {
?>
<a href="?p=<?php echo $i; ?>"><?php echo $i; ?></a>
<?php
}
?>
</td>
</tr>
</table> <h3>我关注了谁</h3>
<table>
<tr>
<th>uid</th>
<th>用户名</th>
<th>年龄</th>
</tr>
<?php
$myWatchIds = $redis->smembers( "user:" . $login_uid . ":watch" );
foreach( $myWatchIds as $wId ){
$watchList = $redis->hgetall( "user:" . $wId );
?>
<tr>
<td><?php echo $watchList['uid']; ?></td>
<td><?php echo $watchList['user']; ?></td>
<td><?php echo $watchList['age']; ?></td>
</tr>
<?php
}
?>
</table>
<h3>我的fans</h3>
<table>
<tr>
<th>uid</th>
<th>用户名</th>
<th>年龄</th>
</tr>
<?php
$myFlowerIds = $redis->smembers( "user:" . $login_uid . ":flowers" );
foreach( $myFlowerIds as $fId ){
$flowerList = $redis->hgetall( "user:" . $fId );
?>
<tr>
<td><?php echo $flowerList['uid']; ?></td>
<td><?php echo $flowerList['user']; ?></td>
<td><?php echo $flowerList['age']; ?></td>
</tr>
<?php
}
?>
</table>

5,编辑 ( edit.php)

 <meta charset="utf-8" />
<?php
require "./redis_connect.php";
$uid = intval( $_GET['uid'] );
if( empty( $uid ) ){
header( "Location:./list.php" );
exit();
}
$userInfo = $redis->hgetall( "user:" . $uid );
?> <form action="do_edit.php" method="post">
<p>
用户名: <input disabled type="text" name="user" value="<?php echo $userInfo['user']; ?>" />
</p>
<p>
年龄:<input type="text" name="age" value="<?php echo $userInfo['age']; ?>" />
</p>
<p>
<input type="submit" value="修改" name="edit" />
</p>
<input type="hidden" value="<?php echo $userInfo['uid']; ?>" name="uid" />
</form>

6,处理更新( do_edit.php )

 <?php
require( "./redis_connect.php" );
$uid = intval( $_POST['uid'] );
$age = $_POST['age']; if( empty( $uid ) ) {
header( "Location:./edit.php" );
exit();
}
$res = $redis->hmset( "user:". $uid, array( "age" => $age ) );
if( $res ) {
header( "Location:./list.php" );
}else {
header( "Location:./edit.php" );
}
exit();
?>

7,删除(delete.php)

 <?php
require( "./redis_connect.php" );
$uid = intval( $_GET['uid'] );
if( empty( $uid ) ) {
header( "Location:./list.php" );
exit();
}
$userName = $redis->get( "user:" . $uid );
$redis->del( "user:" . $uid );
$redis->del( "username:" . $userName );
$redis->lrem( "uid", $uid );
header( "Location:./list.php" );
?>

8,注销( logout.php )

 <?php
require "./redis_connect.php";
if( isset( $_COOKIE['auth'] ) ){
$redis->del( "auth:" . $_COOKIE['auth'] );
setcookie( "auth", "", time() - 86400 );
header( "Location:./list.php" );
exit();
}
?>

9,互粉( do_fans.php )

 <?php
require "./redis_connect.php";
$login_id = intval( $_GET['login_id'] );
$uid = intval( $_GET['uid'] );
if( empty( $login_id ) || empty( $uid ) ) {
header( "Location:./list.php" );
exit();
}
//当前用户关注
$redis->sadd( "user:" . $login_id . ":watch", $uid );
//被当前用户关注
$redis->sadd( "user:" . $uid . ":flowers", $login_id );
header( "Location:./list.php" );
exit();
?>
上一篇:【Head First Servlets and JSP】笔记19:JavaBeans与JSP动作元素(


下一篇:#1122 JSP动作元素