亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

redis+php實現(xiàn)微博(一)注冊與登錄功能詳解

 更新時間:2019年09月23日 10:00:33   作者:巴八靈  
這篇文章主要介紹了redis+php實現(xiàn)微博注冊與登錄功能,結合實例形式分析了php結合redis實現(xiàn)微博注冊及登錄相關操作步驟與實現(xiàn)技巧,需要的朋友可以參考下

本文實例講述了redis+php實現(xiàn)微博注冊與登錄功能。分享給大家供大家參考,具體如下:

(一)、微博功能概況

微博用戶賬號注冊

微博用戶登錄

微博發(fā)布

添加微博好友(粉絲)

微博推送

微博冷數(shù)據(jù)寫入mysql數(shù)據(jù)庫

(二)、redis數(shù)據(jù)結構設計

這節(jié)分享微博用戶注冊與登錄:
我們完全采用redis作為數(shù)據(jù)庫來實現(xiàn)注冊于登錄
先來看一下redis數(shù)據(jù)結構的設計:

注冊用戶表: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('用戶名密碼不能夠為空~');
}
if($password!=$pwd){
  exit('兩次密碼輸入不一致哦~');
}
//連接redis調用公用方法
$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)登錄調整到微博列表頁面
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('密碼輸入錯誤');
}
/**設置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);
}

說明:代碼寫的可能比較簡單,這里只是闡述實現(xiàn)原理

更多關于PHP相關內容感興趣的讀者可查看本站專題:《php+redis數(shù)據(jù)庫程序設計技巧總結》、《php面向對象程序設計入門教程》、《PHP基本語法入門教程》、《PHP數(shù)組(Array)操作技巧大全》、《php字符串(string)用法總結》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總

希望本文所述對大家PHP程序設計有所幫助。

相關文章

最新評論