在線地址:https://docs.qq.com/doc/DQWlqWFVGZFd6eWdN
<?php
//創建一個對象
$redis = new Redis();
$redis->connect('localhost');//連接redis服務器
//如果redis服務器設置了密碼,則使用如下代碼驗證
$redis->auth('123456');
//字符串類型操作
$redis->set('username','李白');
$redis->set('age',12);
//哈希類型的操作
$redis->hSet('user:id:1','name','宋江');
$redis->hmset('user:id:2',['name'=>'小黑','age'=>12,'email'=>'[email protected]']);
//鏈表操作
$redis->lpush('user_list','王剛');
$redis->lpush('user_list','lisi');
$redis->lpush('user_list','wangwu');
echo 'ok';
<?php
//創建一個對象
$redis = new Redis();
$redis->connect('localhost');//連接redis服務器
//如果redis服務器設置了密碼,則使用如下代碼驗證
$redis->auth('123456');
//獲取字符串數據
$username = $redis->get('username');
echo $username;
echo '<br/>';
echo $redis->get('age');
echo '<hr>';
//獲取哈希類型
$data = $redis->hGetAll('user:id:2');
echo '<pre>';
print_r($data);
echo '<hr>';
//獲取鏈表類型
$info = $redis->lRange('user_list',0,-1);
print_r($info);
$redis->flushAll();
案例:
<!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="zh-cn">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>網頁標題</title>
<meta name="keywords" content="關鍵字列表" />
<meta name="description" content="網頁描述" />
<link rel="stylesheet" type="text/css" href="" />
<style type="text/css"></style>
<script type="text/javascript"></script>
</head>
<body>
<table width="250">
<form action="login.php" method="post">
<tr>
<td colspan="2" align="center">用戶登錄</td>
</tr>
<tr>
<td>用戶名</td>
<td><input type="text" name="username" /></td>
</tr>
<tr>
<td>密碼</td>
<td><input type="text" name="password" /></td>
</tr>
<tr>
<td></td>
<td align=""><input type="reset" name="取消"/> <input type="submit" name="登錄"/></td>
</tr>
</form>
</table>
</body>
</html>
<?php
$redis = new Redis();
$redis->connect('localhost');
$redis->auth('123456');
//構建一個計數器的一個鍵盤,使用登錄的用戶名和其他字符串拼接即可
//接收數據
$username = $_POST['username'];
$password = $_POST['password'];
//指定用戶名密碼
$pass = '123456';
$login_key=$username.'loing_number';
if($redis->get($login_key) >= 3){
echo '登錄失敗次數過多,明天再來登錄吧,已鎖定賬戶';
die;
}
if($password==$pass){
//登錄成功
echo 'login success';
}else{
//登陸失敗 。密碼錯誤
//開啓計數器 進行統計
$redis->incr($login_key);
$redis->setTimeout($login_key,15); //設置賬戶鎖
// $redis->expire($login_key,15); //設置賬戶鎖
echo 'Login fail';
}
//在什麼情況下開始計數,密碼輸入錯誤的情況下
//驗證密碼