page contents

Redis 仿微博demo

以下内容希望帮助到大家!

attachments-2020-05-9BDmsq9A5ec8b15e427b6.png

一、用户注册登录

attachments-2020-05-vldut87p5ec8b22aaef78.jpg

  include './header.php';
    include './function.php';

    $username = p('username');
    $password = p('password');
    $password2 = p('password2');

    if ($password != $password2) {
        redirect("./login.php", "两次密码输入不一致", 3);
    }

    if (!$username || !$password) {
        redirect("./login.php", "请输入用户名或秘密", 3);
    }

    $redis = connRedis();
    /*
        用户表设置
        user:id:1:username
        user:id:1:password

        user:username 1
    */
    $id = $redis->incr("global:user");
    $redis->set("user:id:$id:username", $username);
    $redis->set("user:id:$id:password", $password);
    $redis->set("user:username:$username", $id);

    //维护一个最新50个用户的表
    $redis->lpush("newuser", $id);
    $redis->ltrim("newuser", 0, 49);

    redirect("./login.php", "用户名:$username 注册成功", 3)
  include './header.php';
    include './function.php';

    $username = p('username');
    $password = p('password');
    if (!$username || !$password) {
        redirect("./login.php", "请输入用户名或秘密", 3);
    }

    $redis = connRedis();
    $id = $redis->get("user:username:$username");
    $oldPassword = $redis->get("user:id:$id:password");
    closeRedis($redis);
    if ($password != $oldPassword) {
        redirect("./login.php", "用户名或秘密不正确", 3);
    }
    //设置cookie
    setcookie("id", $id);
    setcookie("username", $username);
    setcookie("password", $password);
    redirect("./home.php", "登录成功", 3);


二、发表动态

attachments-2020-05-of2LM26P5ec8b23cbce98.jpg

include './header.php';
    include './function.php';

    $status = p('status');
    if (empty($status)) {
        redirect("./home.php", "请输入内容", 3);
    }
    /*
        post表(发动态表)
        post:id:1:uid 
        post:id:1:content
    */
    $redis = connRedis();
    $id = $redis->incr("global:post");

    $redis->hMset("post:id:$id", array("uid" => $_COOKIE['id'], "content" => $status, "time" => date("Y-m-d H:i:s", time()), "username" => $_COOKIE['username']));

    //最近50条发布的信息
    $redis->lpush("newpost", $id);
    $redis->ltrim("newpost", 0, 49);

    //获取我的粉丝,并把我的动态发给他
    $fans = $redis->smembers("flowing:userid:{$_COOKIE['id']}");
    $fans[] = $_COOKIE['id'];
    foreach ($fans as $fansid) {
        $redis->lpush("receivepost:$fansid", $id);
    }


三、关注页

attachments-2020-05-J8QAuMlv5ec8b24b65a63.jpg

 include './header.php';
    include './function.php';

    $uid = g("uid");
    $f = g("f");
    $redis = connRedis();
    $u = $redis->get("user:id:$uid:username");
    if (!$u) {
        redirect("./home.php", "非法数据", 3);
    }
    if ($f) {
        //关注
        $redis->sAdd("flow:userid:{$_COOKIE['id']}", $uid);
        $redis->sAdd("flowing:userid:$uid", $_COOKIE['id']);
        $msg = "关注成功";
    }else{
        //取消
        $redis->srem("flow:userid:{$_COOKIE['id']}", $uid);
        $redis->srem("flowing:userid:$uid", $_COOKIE['id']);
        $msg = "取消关注成功";
    }
    redirect("./profile.php?u=$u", $msg, 3);    


四、热点页

attachments-2020-05-kVVgJuda5ec8b2729aa8f.jpg

  include_once("./header.php");
    include_once("./function.php");
    if (!$_COOKIE['id']) {
        redirect("./login.php", "请先登录", 3);
    }
    $redis = connRedis();
    $data = $redis->sort("newuser", array("get" => "user:id:*:username", "limit" => array(0,1), "sort" => "desc"));
    //获取最新发布的动态id
    $newpost = $redis->lRange("newpost", 0, -1);    



attachments-2020-05-HwlLfq4a5ec8b12b7c3ef.jpg

  • 发表于 2020-05-23 13:15
  • 阅读 ( 488 )

你可能感兴趣的文章

相关问题

0 条评论

请先 登录 后评论
Pack
Pack

1135 篇文章

作家榜 »

  1. 轩辕小不懂 2403 文章
  2. 小柒 1316 文章
  3. Pack 1135 文章
  4. Nen 576 文章
  5. 王昭君 209 文章
  6. 文双 71 文章
  7. 小威 64 文章
  8. Cara 36 文章