本文實(shí)例講述了redis+php實(shí)現(xiàn)微博注冊(cè)與登錄功能。分享給大家供大家參考,具體如下:
(一)、微博功能概況
微博用戶賬號(hào)注冊(cè)
微博用戶登錄
微博發(fā)布
添加微博好友(粉絲)
微博推送
微博冷數(shù)據(jù)寫入mysql數(shù)據(jù)庫
(二)、redis數(shù)據(jù)結(jié)構(gòu)設(shè)計(jì)
這節(jié)分享微博用戶注冊(cè)與登錄:
我們完全采用redis作為數(shù)據(jù)庫來實(shí)現(xiàn)注冊(cè)于登錄
先來看一下redis數(shù)據(jù)結(jié)構(gòu)的設(shè)計(jì):
注冊(cè)用戶表:user
set global:userid
set user:userid:1:username zhangshan
set user:userid:1:password 1212121212
set user:username:zhangshan:userid 1
發(fā)布微博表:post
set post:postid:3:time timestamp
set post:postid:3:userid 5
set post:postid:3:content 測試發(fā)布哈哈哈哈
incr global:postid
set post:postid:$postid
(三)、核心代碼說明
注冊(cè)代碼:
include("function.php");
//用戶表單提交數(shù)據(jù)接收
$username = I('username');
$password = I('password');
$pwd = I('password2');
if(!$username || !$password || !$pwd){
exit('用戶名密碼不能夠?yàn)榭諂');
}
if($password!=$pwd){
exit('兩次密碼輸入不一致哦~');
}
//連接redis調(diào)用公用方法
$r = redis_connect();
//判斷用戶是否注冊(cè)過
$info = $r->get("user:username:".$username.":userid");
if($info){
exit('該用戶已經(jīng)注冊(cè)過');
}
//將用戶數(shù)據(jù)存入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");
//如果用戶已經(jīng)登錄調(diào)整到微博列表頁面
if(isLogin()!=false){
header("location:home.php");
exit;
}
$username = I('username');
$password = I('password');
if(!$username || !$password){
exit('數(shù)據(jù)輸入不完整');
}
$r = redis_connect();
$userid = $r->get("user:username:".$username.":userid");
if(!$userid){
exit('用戶不存在');
}
$password = $r->get("user:userid:".$userid."password:".$password);
if(!password){
exit('密碼輸入錯(cuò)誤');
}
/**設(shè)置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 接收數(shù)據(jù)方法
**/
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);
}
說明:代碼寫的可能比較簡單,這里只是闡述實(shí)現(xiàn)原理
更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《php+redis數(shù)據(jù)庫程序設(shè)計(jì)技巧總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《PHP基本語法入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對(duì)大家PHP程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:- PHP實(shí)現(xiàn)發(fā)送微博消息功能完整示例
- redis+php實(shí)現(xiàn)微博(三)微博列表功能詳解
- redis+php實(shí)現(xiàn)微博(二)發(fā)布與關(guān)注功能詳解
- PHP+redis實(shí)現(xiàn)微博的拉模型案例詳解
- PHP+redis實(shí)現(xiàn)微博的推模型案例分析
- vue+php實(shí)現(xiàn)的微博留言功能示例
- php微信分享到朋友圈、QQ、朋友、微博
- PHP調(diào)用微博接口實(shí)現(xiàn)微博登錄的方法示例
- php新浪微博登錄接口用法實(shí)例
- 基于PHP實(shí)現(xiàn)發(fā)微博動(dòng)態(tài)代碼實(shí)例