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条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。