Redis 之仿微博demo
一、用户注册登录
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);
二、发表动态
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); }
三、关注页
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);
四、热点页
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);