您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)redis+php怎么實(shí)現(xiàn)微博注冊與登錄功能,小編覺得挺實(shí)用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
本文實(shí)例講述了redis+php實(shí)現(xiàn)微博注冊與登錄功能。分享給大家供大家參考,具體如下:
(一)、微博功能概況
微博用戶賬號注冊
微博用戶登錄
微博發(fā)布
添加微博好友(粉絲)
微博推送
微博冷數(shù)據(jù)寫入mysql數(shù)據(jù)庫
(二)、redis數(shù)據(jù)結(jié)構(gòu)設(shè)計
這節(jié)分享微博用戶注冊與登錄:
我們完全采用redis作為數(shù)據(jù)庫來實(shí)現(xiàn)注冊于登錄
先來看一下redis數(shù)據(jù)結(jié)構(gòu)的設(shè)計:
注冊用戶表: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
(三)、核心代碼說明
注冊代碼:
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(); //判斷用戶是否注冊過 $info = $r->get("user:username:".$username.":userid"); if($info){ exit('該用戶已經(jīng)注冊過'); } //將用戶數(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('密碼輸入錯誤'); } /**設(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)于“redis+php怎么實(shí)現(xiàn)微博注冊與登錄功能”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。