Redis PHP操作实例

本实例简单的介绍了用redis进行用户的增删改查操作,注:php操作redis需要开启redis扩展。

连接redis实例

#redis.php
<?php
    //实例化 
    $redis = new Redis();
    //连接服务器 
    $redis->connect("localhost",6379);
    //授权
    $redis->auth("123456");

添加用户表单

#add.php
<form action="reg.php" method="post">
    <p>用户名:<input type="text" name="username"/></p>
    <p>密码:<input type="password" name="password"/></p>
    <p>年龄:<input type="text" name="age" /></p>
    <p><input type="submit" value="提交">&nbsp;&nbsp;<input type="reset" value="重置"></p>
</form>

注册操作

#reg.php
<?php

require("redis.php");
$username = $_POST['username'];
$password = md5($_POST['password']);
$age = $_POST['age'];
$userid = $redis->incr('userid');
$res = $redis->hmset('user:'.$userid,array('uid'=>$userid,'username'=>$username,'password'=>$password,'age'=>$age));
//将用户存入链表中,方便统计用户数量
$redis->rpush('uid',$userid);
//存入用户名,用于登录时用户名判断是否存在
$redis->set("username:".$username,$userid);
if($res){
    header("location:list.php");
}else{
    header("location:add.php");
}

用户列表

#list.php
<p>
    <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");
?>
欢迎您,<?=$name?> <a href="logout.php">退出</a>
<?php else:?>
    <a href="login.php">登录</a>
<?php endif;?>
</p>
<?php
    //用户总数
    $user_count = $redis->lsize("uid");
    //页大小
    $page_size = 3;
    //当前页码
    $page_num = (!empty($_GET['page'])) ? $_GET['page']:1;
    //页总数
    $page_count = ceil($user_count/$page_size);

    $ids = $redis->lrange("uid",($page_num-1)*$page_size,(($page_num-1)*$page_size+$page_size-1));
    foreach($ids as $val){
        $data[] = $redis->hgetall("user:".$val);
    }
    
?>
<table border="1">
    <tr>
        <th>uid</th><th>username</th><th>age</th><th>操作</th>
    </tr>
    <?php foreach ($data as $key => $value):?>
    <tr>
        <td><?php echo $value['uid']?></td>
        <td><?php echo $value['username']?></td>
        <td><?php echo $value['age']?></td>
        <td>
            <a href="edit.php?id=<?=$value['uid']?>">编辑</a>
            <a href="del.php?id=<?=$value['uid']?>">删除</a>
            <?php if (!empty($_COOKIE['auth'])&&$id!=$value['uid']):?>
                <a href="addfans.php?id=<?=$value['uid']?>&uid=<?=$id?>">加关注</a>
            <?php endif;?>
        </td>
    </tr>
    <?php endforeach?>
    </tr>
    <tr>
        <td colspan="4">
            <a href="?page=<?=(($page_num-1)<=1)?1:($page_num-1)?>">上一页</a>
            <a href="?page=<?=(($page_num+1)>=$page_count)?$page_count:$page_num?>">下一页</a>
            <a href="?page=1">首页</a>
            <a href="?page=<?=$page_count?>">尾页</a>
            当前<?=$page_num?>页&nbsp;
            总共<?=$page_count?>页&nbsp;
            总共<?=$user_count?>个用户
        </td>
    </tr>
</table>
<?php if(!empty($_COOKIE['auth'])): ?>
<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 endforeach;?>
</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 endforeach;?>
</table>
<?php endif;?>
list.php

删除用户操作

#del.php
<?php
    require("redis.php");
    $uid = $_GET['id'];
    $redis->del("user".$uid);
    $redis->lrem("uid",$uid);
    header('location:list.php');

编辑用户表单

#edit.php
<?php
    require("redis.php");
    $uid = $_GET['id'];
    $data = $redis->hgetall("user".$uid);
?>
<form action="editok.php" method="post">
    <p>用户名:<input type="text" name="username" value="<?=$data['username']?>"/></p>
    <p>年龄:<input type="text" name="age" value="<?=$data['age']?>" /></p>
    <input type="hidden" name="uid" value="<?=$data['uid']?>"/>
    <p><input type="submit" value="提交修改">&nbsp;&nbsp;<input type="reset" value="重置"></p>
</form>

编辑用户信息入库

#editok.php
<?php
require("redis.php");
$uid = $_POST['uid'];
$username = $_POST['username'];
$age = $_POST['age'];

$res = $redis->hmset('user'.$uid,array('username'=>$username,'age'=>$age));
if($res){
    header("location:list.php");
}else{
    echo "更新失败";
}

登录:

#login.php
<?php
    require("redis.php");
    if(isset($_POST['submit'])){
        $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()+3600);
                header("location:list.php");
            }
        }
    }
?>
<form action="" method="post">
    <p>用户名:<input type="text" name="username"/></p>
    <p>密码:<input type="password" name="password"/></p>
    <p><input type="submit" name="submit" value="登录"></p>
</form>

退出登录:

#logout.php
<?php
    setcookie("auth","",time()-1);
    header("location:list.php");

添加关注:

#addfans.php
<?php

    require("redis.php");
    $id = $_GET['id'];
    $uid = $_GET['uid'];
    $redis->sadd("user:".$uid.":following",$id);
    $redis->sadd("user:".$id.":followers",$uid);
    header("location:list.php");

效果图如下:

 

posted on 2017-08-17 14:02  gimin  阅读(239)  评论(0编辑  收藏  举报