redis+php实现微博功能(一)
(一)、微博功能概况
微博用户账号注册
微博用户登录
微博发布
添加微博好友(粉丝)
微博推送
微博冷数据写入mysql数据库
(二)、redis数据结构设计
这节分享微博用户注册与登录:
我们完全采用redis作为数据库来实现注册于登录
先来看一下redis数据结构的设计:
注册用户表:user
set global:userid
set user:userid:1:username zhangshan
set user:userid:1:password 1212121212
set user:username:zhangshan:userid 1
发布微博表:post
set post:postid:3:time timestamp
set post:postid:3:userid 5
set post:postid:3:content 测试发布哈哈哈哈
incr global:postid
set post:postid:$postid
(三)、核心代码说明
注册代码:
include("function.php");
用户表单提交数据接收
$username = I('username');
$password = I('password');
$pwd = I('password2');
if(!$username || !$password || !$pwd){
exit('用户名密码不能够为空~');
}
if($password!=$pwd){
exit('两次密码输入不一致哦~');
}
//连接redis调用公用方法
$r = redis_connect();
//判断用户是否注册过
$info = $r->get("user:username:".$username.":userid");
if($info){
exit('该用户已经注册过');
}
//将用户数据存入redis中
$userid = $r->incr('global:userid');
$r->set("user:userid:".$userid.":username",$username);
$r->set("user:userid:".$userid.":password",$password);
$r->set("user:username:".$username.":userid",$userid);
header("location:home.php");
登录代码:
include("function.php");
//如果用户已经登录调整到微博列表页面
if(isLogin()!=false){
header("location:home.php");
exit;
}
$username = I('username');
$password = I('password');
if(!$username || !$password){
exit('数据输入不完整');
}
$r = redis_connect();
$userid = $r->get("user:username:".$username.":userid");
if(!$userid){
exit('用户不存在');
}
$password = $r->get("user:userid:".$userid."password:".$password);
if(!password){
exit('密码输入错误');
}
/**设置cookie登录成功**/
setcookie('username',$username);
setcookie('userid',$userid);
header("location:home.php");
function文件代码:
/*
*@desc 连接redis操作方法
*/
function redis_connect(){
$redis = new Redis();
$redis->connect('127.0.0.1',6379);
return $redis;
}
/*
*@desc 接收数据方法
**/
function I($post){
if(empty($post)){
return false;
}
return trim($_POST[$post]);
}
/**
*@desc 判断是否登录
***/
function isLogin(){
$username = $_COOKIE['username'];
$userid = $_COOKIE['userid'];
if(!$username || $userid){
return false;
}
return array('userid'=>$userid,'username'=>$username);
}
说明:代码写的可能比较简单,这里只是阐述实现原理
redis+php实现微博功能(一)的更多相关文章
- redis+php实现微博功能(二)
数据结构: set post:postid:3:time timestampset post:postid:3:userid 5 set post:postid:3:content 测试发布哈哈哈哈 ...
- redis+php实现微博功能(三)
个人主页显示微博列表(自己及关注人的微博列表) /*获取最新的50微博信息列表,列出自己发布的微博及我关注用户的微博 *1.根据推送的信息获取postid *2.根据postid获取发送的信息 */ ...
- 开启SharePoint Server 2013 中的“微博”功能——新闻源
熟悉SharePoint的朋友在2013之前的版本可以使用社区协作下的记事板.应用程序下的通知,来进行消息的发布,而且更有这两者的完美结合体讨论板,可供使用着根据站点属性进行添加而对现在的快消息时代, ...
- Redis Geo: Redis新增位置查询功能
转载于:http://www.itxuexiwang.com/a/shujukujishu/redis/2016/0216/144.html 移动互联网增进了人与人之间的联系,其中基于位置信息的服务( ...
- 通过Keepalived实现Redis Failover自动故障切换功能
通过Keepalived实现Redis Failover自动故障切换功能[实践分享] 参考资料: http://patrick-tang.blogspot.com/2012/06/redis-keep ...
- 利用redis分布式锁的功能来实现定时器的分布式
文章来源于我的 iteye blog http://ak478288.iteye.com/blog/1898190 以前为部门内部开发过一个定时器程序,这个定时器很简单,就是配置quartz,来实现定 ...
- 转:Redis Geo: Redis新增位置查询功能
原文来自于:http://www.infoq.com/cn/news/2015/07/redis-geo 移动互联网增进了人与人之间的联系,其中基于位置信息的服务(Location Based Ser ...
- Redis实现世界杯排行榜功能(实战)
转载请注明出处:https://www.cnblogs.com/wenjunwei/p/9754346.html 需求 前段时间,做了一个世界杯竞猜积分排行榜.对世界杯64场球赛胜负平进行猜测,猜对+ ...
- 【springboot】【redis】springboot+redis实现发布订阅功能,实现redis的消息队列的功能
springboot+redis实现发布订阅功能,实现redis的消息队列的功能 参考:https://www.cnblogs.com/cx987514451/p/9529611.html 思考一个问 ...
随机推荐
- 【Python】什么是闭包
文章转载自:点这里 在 Python 中很多教材都没有提及什么是闭包,但在定义一个 Decorator 时,就已经用到闭包了.如果不理解什么是闭包,则不可能清晰掌握Decorator 装饰器. 要形成 ...
- Qt 元对象系统(Meta-Object System)
(转自:http://blog.csdn.net/aladdina/article/details/5496891) Qt的元对象系统基于如下三件事情: 类:QObject,为所有需要利用原对象系统的 ...
- 【zznu-2060】 Minsum Plus(最小正子段和)
题目描述 题意简单到令人发指! 序列A由N个整数组成,从中选出一个连续的子序列,使得这个子序列的和为正数,且和为所有和大于零的子序列中的最小值. 将这个值输出,若无解,输出no solution. 输 ...
- C#常用函数与方法集合
1.DateTime 数字型 System.DateTime currentTime=new System.DateTime(); 1.1 取当前年月日时分秒 ...
- 一道简单的JavaScript面试题
好久没更新博客了,随便写点东西吧. 自从工作之后就特别忙,忙的过程中有时候挺迷茫的,可能是大多数时候写的都是简单的业务代码,很久没好好充电了.最近一直在零碎的上班路上等电梯时间里面学习<图解HT ...
- New Concept English Two 34 game over
$课文95 纯属虚构 1049. When the Ambassador or Escalopia returned home for lunch, his wife got a shock. 当艾 ...
- 敏捷BI与数据驱动机制
大数据这件事,整体上还是说的多一些,做的稍微少一点.大数据可以是荒凉高原上波澜壮阔的机房,也可以润物细无声般融入到日常生活和工作.换句话说,大数据应该是一种文化. 在个人层面,很多人对数字和计算并不敏 ...
- 如何看待 Kotlin 成为 Android 官方支持开发语言?
Google IO 2017宣布了 Kotlin 会成为 Android 官方开发语言.一时间朋友圈和Android圈被各种刷屏.当然我也顺势而为发布了一篇的文章<为什么我要改用Kotlin&g ...
- JavaScript动态加载js文件
/********************************************************************* * JavaScript动态加载js文件 * 说明: * ...
- 跟踪Makefile输出调试信息
/********************************************************************* * 跟踪Makefile输出调试信息 * 说明: * 有时 ...