dpw70180 2015-07-06 13:20
浏览 91
已采纳

PHP:使用数据库限制登录尝试次数[关闭]

My database INSERT is not functioning.

Here is my code :

     <?php 
         if(isset($_POST['submit'])) {
         if(empty($row['login']) AND empty($row['password'])) {

        echo "Sorry, but that username and/or password you entered is  
              incorrect. Please try again.";

        $ipaddress = $_SERVER["REMOTE_ADDR"];
        $login = mysqli_real_escape_string($conn, $_POST['login']);

        $sql_login_tries = mysqli_query($conn,("Insert into login_attempts  
                           ('login','ip','tries','last_login')
                          Values ('$login','$ipaddress','1',Now())"));

        $sql_get_tries = mysqli_query($conn,("SELECT * FROM login_attempts 
                         where login = '$login'"))
                         or die(mysqli_error($conn));

       $number_of_tries = mysqli_num_rows($sql_get_tries); 

       echo $number_of_tries;

       if (($number_of_tries) > 2) {  

                header ("Location: /example.com/access_denied");

                exit() ;
                }
            }
          }  
        }
      ?>

I used "echo" at various points in the code. I noticed that: everything works right up to the point where I try to INSERT those values into the table "login_attempts";

P.S. I do have database-connection parameters set; I simply did not bother to include them here.

P.P.S. The redirect to "access_denied" page is purely for testing purposes. In reality, I plan to activate a CAPTCHA function, if the user has tried to log in 3 times unsuccessfully.

UPDATE ERROR MESSAGE

You have an error in your SQL syntax. Check the manual for the correct syntax to use near : (login, ip, tries, last_login) values ('mjackson', '111.11.11.111','1',NOW' at line 1

  • 写回答

2条回答 默认 最新

  • dsl2014 2015-07-06 14:06
    关注

    You should not be using single quotes around the field names in your insert query. Use backticks instead:

    Insert into login_attempts  
    (`login`,`ip`,`tries`,`last_login`)
    Values ('$login','$ipaddress','1',Now())
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题