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 思考一个问 ...
随机推荐
- day5-logging模块
一.概述 好的程序开发,往往会兼顾到日志输出的需求,以便给用户提供必要的日志信息便于确认程序运行状态.排错等等.这些日志一般包括程序的正常运行日志.访问日志.错误日志.数据保存日志等类型.在pytho ...
- Inventory Update
依照一个存着新进货物的二维数组,更新存着现有库存(在 arr1 中)的二维数组. 如果货物已存在则更新数量 . 如果没有对应货物则把其加入到数组中,更新最新的数量. 返回当前的库存数组,且按货物名称的 ...
- VirtualBox安装Ubuntu搭建js环境的注意事项
1.使用CPU-Z工具检查主板是否支持虚拟技术,因为VirtulBox虚拟机需要主板支持虚拟技术 2.Ubuntu安装之后重启会出现:please remove this install medium ...
- 使用群晖NAS:配置Git server
1.首先在群晖的DSM的控制面板中创建一个用户例如是Git_test(我给了管理员权限) 2.在套件中心安装 Git server 3.打开Git server 勾选用户 Git_test 4.在控制 ...
- 十二、dbms_logmnr(分析重做日志和归档日志)
1.概述 作用:通过使用包DBMS_LOGMNR和DBMS_LOGMNR_D,可以分析重做日志和归档日志所记载的事务变化,最终确定误操作(例如DROP TABLE)的时间,跟踪用户事务操作,跟踪并还原 ...
- 【Html 学习笔记】第八节——表单实践
列举一些实践的例子: 1.点击按钮后跳转: <html> <body> <form action="1.html"> First <inp ...
- leetCode之Median of Two Sorted Arrays
[题目描述] There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of ...
- Python面向对象 --- 新旧式类、私有方法、类属性和类方法、静态方法
一.Python面向对象中的新旧式类 1)新式类(推荐使用):在定义类时,类后边括号里要继承基类(object).在python3.x中若没有指定父类,会默认使用的是object作为基类:在pytho ...
- a的样式
.myAucCItem a { color: rgb(71,71,71);} .myAucCItem a:hover { color: rgb(71,71,71); text-decoration: ...
- 【OpenCV入门教程之二】 一览众山小:OpenCV 2.4.8 or OpenCV 2.4.9组件结构全解析
本系列文章由zhmxy555(毛星云)编写,转载请注明出处. 文章链接:http://blog.csdn.net/poem_qianmo/article/details/19925819 作者:毛星云 ...