Home  >  Article  >  Backend Development  >  How to determine the number of errors in php

How to determine the number of errors in php

藏色散人
藏色散人Original
2021-11-08 09:47:481192browse

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.

How to determine the number of errors in php

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(&#39;127.0.0.1&#39;, 6379);
//模拟数据库信息
$userinfo = array(&#39;xxx&#39;=>&#39;000000&#39;,&#39;yyy&#39;=>&#39;111111&#39;);
//接受用户输入信息
$name = $_POST[&#39;username&#39;];
$passwd = $_POST[&#39;userpasswd&#39;];
//判断是否已经错误三次
if($redis->get($name) >=3){
    //以错三次  为该用户设置操作等待时间  60秒
    $redis->expire($name,60);
    echo &#39;请您1分钟后在登入吧&#39;;exit();
}
//判断用户是否存在
if(!array_key_exists($name,$userinfo)){
    echo &#39;用户名不存在&#39;;
    exit();
}
//判断密码是否正确
if($passwd != $userinfo[$name]){
    //当前错误不是第一次  将错误次数加 1
    if($redis->exists($name)){
        $redis->incr($name);
    }else{
        //第一次错误将错误信息存入redis中
        $redis->set($name,1);
    }
    echo &#39;密码不正确&#39;;
    exit();
}else{
    echo &#39;成功&#39;;
}

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn