The implementation method of php to determine the number of errors: 1. Instantiate the redis database; 2. Simulate database information; 3. Accept user input information; 4. Determine the number of incorrect information inputs.
The operating environment of this article: Windows 7 system, PHP version 7.1, DELL G3 computer
php How to determine the number of errors?
php combined with redis to limit the number of incorrect user login passwords
I wrote a small dome using the native process-oriented method, and replaced the database information with an array.
Overall The idea is like this, how to use it specifically, put it in the framework, modify it and optimize it, and it will be ok.
php code:
<?php /** * Created by PhpStorm. * User: rjj * Date: 2017/7/2 * Time: 20:58 */ //实例化redis数据库 $redis= new Redis(); $redis->connect('127.0.0.1', 6379); //模拟数据库信息 $userinfo = array('xxx'=>'000000','yyy'=>'111111'); //接受用户输入信息 $name = $_POST['username']; $passwd = $_POST['userpasswd']; //判断是否已经错误三次 if($redis->get($name) >=3){ //以错三次 为该用户设置操作等待时间 60秒 $redis->expire($name,60); echo '请您1分钟后在登入吧';exit(); } //判断用户是否存在 if(!array_key_exists($name,$userinfo)){ echo '用户名不存在'; exit(); } //判断密码是否正确 if($passwd != $userinfo[$name]){ //当前错误不是第一次 将错误次数加 1 if($redis->exists($name)){ $redis->incr($name); }else{ //第一次错误将错误信息存入redis中 $redis->set($name,1); } echo '密码不正确'; exit(); }else{ echo '成功'; }
html code
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>错误限制</title> </head> <body> <form method="post" action="redisLogin.php"> <input type="text" name="username" placeholder="请输入用户名"> <input type="text" name="userpasswd" placeholder="请输入密码"> <button type="submit">登入</button> </form> </body> </html>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to determine the number of errors in php. For more information, please follow other related articles on the PHP Chinese website!