使用redis技术实现注册登录列表以及关注功能

https://www.cnblogs.com/hopelooking/p/6513572.html

redis命令中文网参考网址:http://www.redis.cn/commands.html

首先我们需要下载一个类文件那就是predis

git地址:git clone git://github.com/nrk/predis.git

zip地址:https://github.com/nrk/predis/archive/v1.0.1.zip

然后我是嘴边创建了个本地文件引用的

接下来一个页面一个页面的看代码:

先连接redis,来一个redis.php

复制代码

<?php
    header("Content-type: text/html; charset=utf-8");
    //载入redis文件
    require './predis/autoload.php';
    //连接redis
    $redis = new Predis\Client(array(
        'host'=>'127.0.0.1',
        'port'=>6379
    ));
?>

复制代码

注册:register.php

复制代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>注册</title>
</head>
<body>
    <form action="register_a.php" method="post">
        <table>
            <tr>
                <th>用户名:</th>
                <td><input type="text" name="username" id="username" /></td>
            </tr>
            <tr>
                <th>邮箱:</th>
                <td><input type="text" name="email" id="email" /></td>
            </tr>
            <tr>
                <th>密码:</th>
                <td><input type="password" name="password" id="password" /></td>
            </tr>
            <tr>
                <th></th>
                <td><input type="submit" value="注册" /></td>
            </tr>
        </table>
    </form>
</body>
</html>

复制代码

register_a.php

复制代码

<?php
    include('./redis.php');
    if(!isset($_POST['username']) || !isset($_POST['email']) || !isset($_POST['password'])){
        echo "填写信息不完整";
        exit;
    }

    if(!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL)){
        echo "邮箱格式不正确";
        exit;
    }

    if(strlen($_POST['password'])<6){
        echo "邮箱密码不安全";
        exit;
    }

    //判断邮箱是否被注册
    if($redis->hexists('email.to.id',$_POST['email'])){

        echo "邮箱已经被注册";
        exit;
    }

    //密码设置函数
    function bcryptHash($rawPassword,$round = 8){
        if($round < 4 || $round >31) $round = 8;
        $salt = '$2a$' . str_pad($round,2,'0',STR_PAD_LEFT) . '$';
        $randomValue = openssl_random_pseudo_bytes(16);
        $salt .= substr(strtr(base64_encode($randomValue),'+','.'),0,22);
        return crypt($rawPassword,$salt);
    }

    $hashedPassword = bcryptHash($_POST['password']);

    //存储用户资料
    $userID = $redis->incr('user:count');//获取一个自增id
    //存储
    $redis->hmset(
        "user:{$userID}",
        array(
            'uid'=>$userID,
            'email'=>$_POST['email'],
            'password'=>$hashedPassword,
            'username'=>$_POST['username']
        )
    );
    //记录一下邮箱和用户id的关系
    $redis->hset('email.to.id',$_POST['email'],$userID);
    echo "注册成功";
    header('location:login.php');
?>

复制代码

登录:login.php

复制代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>登录</title>
</head>
<body>
    <form action="login_a.php" method="post">
        <table>
            <tr>
                <th>邮箱:</th>
                <td><input type="text" name="email" id="email" /></td>
            </tr>
            <tr>
                <th>密码:</th>
                <td><input type="password" name="password" id="password" /></td>
            </tr>
            <tr>
                <th><input type="button" value="注册" onclick="zhuce();" /></th>
                <td><input type="submit" value="登录" /> <a href="#">忘记密码?</a></td>
            </tr>
        </table>
    </form>
</body>
<script type="text/javascript">
    function zhuce(){
        location.href='register.php';
    }
</script>
</html>

复制代码

login_a.php

按 Ctrl+C 复制代码

按 Ctrl+C 复制代码

登录成功进入home.php

复制代码

<?php
    include('./redis.php');
    $count = $redis->get('user:count');
    for($i=1;$i<=$count;$i++){
        $res[] = $redis->hgetall('user:'.$i);
    }

    //我的关注
    $myguanzhu = $redis->smembers("user:{$_COOKIE['myuid']}:following");
    foreach ($myguanzhu as $key => $v) {
        $gzinfo[] = $redis->hget("user:{$v}",'username');
    }


    //我的粉丝
    $myfen = $redis->smembers("user:{$_COOKIE['myuid']}:followers");
    foreach ($myfen as $kkk => $vv) {
        $fsinfo[] = $redis->hget("user:{$vv}",'username');
    }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
    <title>欢迎页</title>
    <style type="text/css">
        li{
            float: left;
            margin-left:20px;
            list-style-type:none;
        }
    </style>
</head>
<body>
<h3>当前用户</h3>
<p><?php
$myuid = $_COOKIE['myuid'];
echo $redis->hget("user:{$myuid}",'email');
?><span><a href="loginout.php">退出</a></span></p>
<hr/>
    <h3>用户列表</h3>
    <table>
        <tr>
            <th>用户ID</th>
            <th>用户</th>
            <th>邮箱</th>
            <th>操作</th>
        </tr>
        <?php foreach($res as $v){ ?>
            <tr>
                <td><?php echo $v['uid'];?></td>
                <td><?php echo $v['username'];?></td>
                <td><?php echo $v['email'];?></td>
                <td>
                    <a href="addfans.php?uid=<?php echo $v['uid'];?>&myuid=<?php echo $_COOKIE['myuid'];?>">加关注</a>
                    <!-- <a href="del.php?uid=<?php echo $v['uid'];?>&myuid=<?php echo $_COOKIE['myuid'];?>">删除</a> -->
                </td>
            </tr>
        <?php } ?>
    </table>
<hr/>
<li>
    <h3>我的关注</h3>
    <?php if($myguanzhu==null):?>
        暂无关注
    <?php else:?>
        <?php foreach($gzinfo as $k=>$va){ ?>
        <p><?php echo $va;?></p>
        <?php } ?>
    <?php endif;?>
</li>

<li style="margin-left:100px;">
    <h3>我的粉丝</h3>
    <?php if($myfen==null):?>
        暂无粉丝
    <?php else:?>
        <?php foreach($fsinfo as $a=>$b){ ?>
        <p><?php echo $b;?></p>
        <?php } ?>
    <?php endif;?>
</li>
</body>
</html>

复制代码

实现关注和被关注:addfans.php

复制代码

<?php
    include('./redis.php');
    $uid = $_GET['uid'];
    $myuid = $_GET['myuid'];
    if($uid==$myuid){
        echo '不能给自己点关注';
        exit;
    }
    
    //添加redis
    $redis->sadd("user:".$myuid.":following",$uid);
    $redis->sadd("user:".$uid.":followers",$myuid);
    echo "关注成功";
?>

复制代码

最后是退出登录 loginout.php

<?php
    setcookie('myuid',null);
    header('location:login.php');
?>

基本上数据类型都是字符串或者hash

redis数据表预览

通往牛逼的路上,在意的只有远方!

猜你喜欢

转载自blog.csdn.net/zhangdaohong/article/details/82780093