douyan8266 2015-07-07 05:36
浏览 121

PHP:验证码验证码无法正常工作;

I am creating a very simple Captcha verification process.

(When a user tries to log into my website 3 times, without success, he is re-directed to the Captcha Page (captcha.php), to verify that he is not a spam-bot)

The Captcha value itself is randomly generated from a string of letters and numbers. 6 digits in all.

And then, I compare this value with the value entered by the user into a text-box. if the values match, the user may proceed. If not, the page reloads, an error message is displayed, and a new captcha is generated.

Very simple. No javascript, no ajax.

Except for one thing : the Captcha is NOT re-directing if the user provides the correct value.

Instead, it gives the error message every time.

  <?php session_start();

   include 'database_connect.php';

   function getRandomString($length)  {
   $validCharacters =  
   "ABCDEFGHIJKLMNPQRSTUVWXYZ123456789";
    $validCharNumber = strlen($validCharacters);
    $result = "";

    for ($i = 0; $i < $length; $i++) {
    $index = mt_rand(0, $validCharNumber - 1);
    $result .= $validCharacters[$index];
    }
    return $result; }

    $captcha_value = getRandomString(6);

    ?>

   <!DOCTYPE html>
   <html>
   <head>
        <META HTTP-EQUIV="Cache-Control" CONTENT="no-cache">
        <META HTTP-EQUIV="Cache-Control" CONTENT="no-store">
        <META HTTP-EQUIV="Pragma" CONTENT="no-cache">
        <META HTTP-EQUIV="Expires" CONTENT="0">
        <META CHARSET="UTF-8">      
        <title>Captcha Test</title>         
  </head>
 <body>
    <fieldset><legend>CAPTCHA :</legend>

     <?php 
            if (isset($_POST['submit'])) {

                $post_captcha = $_POST['captcha'];

                if ($post_captcha == $captcha_value) {

                $clear_failed_logins =  mysqli_query($conn,("Delete FROM 
                                        login_attempts where login = 
                                        '$_POST[login]'")) 
                                        or die(mysqli_error($conn)); 

                header ("Location: /example.com/login.php");    
                exit();     
            }
            else {

            echo "<p style='color:red; font-weight:bold;'>The value you  
                   entered is not correct! Please try again.</p>";
             }
          }
        ?> 

        <p>Please input the characters you see into the text-box below :</p>
        <p>  <?= $captcha_value ?> </p>
        <form method="POST" action="captcha.php">
        <input type="text" name="captcha" id="captcha" size=10 
                     autocomplete="off" required><br><br><br> 
        <input type="submit" name="submit" id="submit" value="SUBMIT"> 
        </form>
    </fieldset> 
  • 写回答

1条回答 默认 最新

  • dongpang2029 2015-07-07 05:44
    关注

    You can't redirect with header in php after you output something to the user, so move that if else block to the top of you page before something get output. If you want to redirect at this position try a meta refresh

     <meta http-equiv="refresh" content="0; URL=http://www.domain.com">
    

    Additionally you should store the captcha value in the form because it get regenerated every time the page get refreshed (so after a form submit), i suggest you to try it with reCaptcha, that is easy to implement and alot more secure

    评论

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用