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);    

四、热点页

image

3.18.jpg

评论

暂无评论