redis命令中文網參考網址:http://www.redis.cn/commands.html
首先我們需要下載一個類文件那就是predis
git地址:git clone git://github.com/nrk/predis.git
zip地址:https://github.com/nrk/predis/archive/v1.0.1.zip
然后我是嘴邊創建了個本地文件引用的
接下來一個頁面一個頁面的看代碼:
先連接redis,來一個redis.php
<?php header("Content-type: text/html; charset=utf-8"); //載入redis文件 require './predis/autoload.php'; //連接redis $redis = new Predis\Client(array( 'host'=>'127.0.0.1', 'port'=>6379 )); ?>
注冊:register.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <title>注冊</title> </head> <body> <form action="register_a.php" method="post"> <table> <tr> <th>用戶名:</th> <td><input type="text" name="username" id="username" /></td> </tr> <tr> <th>郵箱:</th> <td><input type="text" name="email" id="email" /></td> </tr> <tr> <th>密碼:</th> <td><input type="password" name="password" id="password" /></td> </tr> <tr> <th></th> <td><input type="submit" value="注冊" /></td> </tr> </table> </form> </body> </html>
register_a.php
<?php include('./redis.php'); if(!isset($_POST['username']) || !isset($_POST['email']) || !isset($_POST['password'])){ echo "填寫信息不完整"; exit; } if(!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL)){ echo "郵箱格式不正確"; exit; } if(strlen($_POST['password'])<6){ echo "郵箱密碼不安全"; exit; } //判斷郵箱是否被注冊 if($redis->hexists('email.to.id',$_POST['email'])){ echo "郵箱已經被注冊"; exit; } //密碼設置函數 function bcryptHash($rawPassword,$round = 8){ if($round < 4 || $round >31) $round = 8; $salt = '$2a$' . str_pad($round,2,'0',STR_PAD_LEFT) . '$'; $randomValue = openssl_random_pseudo_bytes(16); $salt .= substr(strtr(base64_encode($randomValue),'+','.'),0,22); return crypt($rawPassword,$salt); } $hashedPassword = bcryptHash($_POST['password']); //存儲用戶資料 $userID = $redis->incr('user:count');//獲取一個自增id //存儲 $redis->hmset( "user:{$userID}", array( 'uid'=>$userID, 'email'=>$_POST['email'], 'password'=>$hashedPassword, 'username'=>$_POST['username'] ) ); //記錄一下郵箱和用戶id的關系 $redis->hset('email.to.id',$_POST['email'],$userID); echo "注冊成功"; header('location:login.php'); ?>
登錄:login.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <title>登錄</title> </head> <body> <form action="login_a.php" method="post"> <table> <tr> <th>郵箱:</th> <td><input type="text" name="email" id="email" /></td> </tr> <tr> <th>密碼:</th> <td><input type="password" name="password" id="password" /></td> </tr> <tr> <th><input type="button" value="注冊" onclick="zhuce();" /></th> <td><input type="submit" value="登錄" /> <a href="#">忘記密碼?</a></td> </tr> </table> </form> </body> <script type="text/javascript"> function zhuce(){ location.href='register.php'; } </script> </html>
login_a.php
<?php include('./redis.php'); if(!isset($_POST['email']) || !isset($_POST['password'])){ echo "填寫信息不完整"; exit; } if(!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL)){ echo "郵箱格式不正確"; exit; } //獲取用戶id $userID = $redis->hget('email.to.id',$_POST['email']); if(!$userID){ echo "用戶名或者密碼錯誤!"; } //根據用戶id獲取密碼 $password = $redis->hget("user:{$userID}",'password');//存儲的密碼 //密碼驗證函數 function bcryptVerify($rawPassword,$storedHash){ return crypt($rawPassword,$storedHash) == $storedHash; } if(!bcryptVerify($_POST['password'],$password)){ echo "密碼錯誤!"; exit; } echo "登錄成功"; setcookie('myuid',$userID); header('location:home.php'); ?>
登錄成功進入home.php
<?php include('./redis.php'); $count = $redis->get('user:count'); for($i=1;$i<=$count;$i++){ $res[] = $redis->hgetall('user:'.$i); } //我的關注 $myguanzhu = $redis->smembers("user:{$_COOKIE['myuid']}:following"); foreach ($myguanzhu as $key => $v) { $gzinfo[] = $redis->hget("user:{$v}",'username'); } //我的粉絲 $myfen = $redis->smembers("user:{$_COOKIE['myuid']}:followers"); foreach ($myfen as $kkk => $vv) { $fsinfo[] = $redis->hget("user:{$vv}",'username'); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <title>歡迎頁</title> <style type="text/css"> li{ float: left; margin-left:20px; list-style-type:none; } </style> </head> <body> <h3>當前用戶</h3> <p><?php $myuid = $_COOKIE['myuid']; echo $redis->hget("user:{$myuid}",'email'); ?><span><a href="loginout.php">退出</a></span></p> <hr/> <h3>用戶列表</h3> <table> <tr> <th>用戶ID</th> <th>用戶</th> <th>郵箱</th> <th>操作</th> </tr> <?php foreach($res as $v){ ?> <tr> <td><?php echo $v['uid'];?></td> <td><?php echo $v['username'];?></td> <td><?php echo $v['email'];?></td> <td> <a href="addfans.php?uid=<?php echo $v['uid'];?>&myuid=<?php echo $_COOKIE['myuid'];?>">加關注</a> <!-- <a href="del.php?uid=<?php echo $v['uid'];?>&myuid=<?php echo $_COOKIE['myuid'];?>">刪除</a> --> </td> </tr> <?php } ?> </table> <hr/> <li> <h3>我的關注</h3> <?php if($myguanzhu==null):?> 暫無關注 <?php else:?> <?php foreach($gzinfo as $k=>$va){ ?> <p><?php echo $va;?></p> <?php } ?> <?php endif;?> </li> <li style="margin-left:100px;"> <h3>我的粉絲</h3> <?php if($myfen==null):?> 暫無粉絲 <?php else:?> <?php foreach($fsinfo as $a=>$b){ ?> <p><?php echo $b;?></p> <?php } ?> <?php endif;?> </li> </body> </html>
實現關注和被關注:addfans.php
<?php include('./redis.php'); $uid = $_GET['uid']; $myuid = $_GET['myuid']; if($uid==$myuid){ echo '不能給自己點關注'; exit; } //添加redis $redis->sadd("user:".$myuid.":following",$uid); $redis->sadd("user:".$uid.":followers",$myuid); echo "關注成功"; ?>
最后是退出登錄 loginout.php
<?php setcookie('myuid',null); header('location:login.php'); ?>
基本上數據類型都是字符串或者hash
redis數據表預覽