如何使验证码检查代码工作

时间:2018-01-07 10:08:37

标签: javascript php jquery captcha

当我点击我的提交按钮时,我一直收到一个空白的消息表单,我相信告诉我我的JavaScript代码没有传递给register.php?

我的表格

<form onsubmit="return false" action="" method="POST" autocomplete="off" id="register_form">

  <!--  CAPTCHA Code Container  -->
  <div class="form-group col-lg-6 col-md-6 col-sm-6 col-xs-12">
    <label for="txt_Captcha">CAPTCHA Code: </label>
    <img id="captcha_img" src="captcha.php" alt="CAPTCHA">

    <input class="form-control" type="text" name="txt_Captcha" id='txt_Captcha' value="" size="9" maxlength="8" placeholder="Place above CAPTCHA code here" required">

  </div>

  <input type="submit" class="btn btn-block btn-primary" name="submit" role="button" onclick="send()" value="Create account" />
</form>

我的表单中的JavaScript

<script>
function send(){
  var captcha_code = document.getElementById("txt_Captcha").value;

  if (… || captcha_code == ""){
    alert("Please fill in all fields")

  } else {

    var parameters= …"&txt_Captcha="+captcha_code;

    var ajax=new XMLHttpRequest();

    ajax.onreadystatechange=function(){
      if(ajax.readyState === 4 && ajax.status === 200) {
        alert(ajax.responseText);
      }
    };
    ajax.open("POST","register.php",false);
    ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    ajax.send(parameters);
  }
}
</script>

这是我的register.php

<?php 
session_start();

if(!empty($_POST) && isset($_POST['submit'])){

  if (… || empty($_POST["txt_Captcha"])){
    echo 'Value(s) in the form missing, please fill them all out!';
    exit();
  }

  ...

  else if($_POST['txt_Captcha'] != $_SESSION['txt_captcha']){
    echo 'Captcha Code is incorrect.';
    exit();
  } // That also does not work I believe

 //Note: I'm not inserting this value into my db, I just need to verify is whether user has input the correct captcha for the rest of the code to proceed

} else {
 ...
}
}
?>

这是我的captcha.php

<?php

create_captcha_img();

function create_captcha_img(){

  // initialise image
  $captcha_img = imagecreatetruecolor(160, 50) or die("Cannot Initialize new GD image stream");

  // set background and allocate drawing colours
  $bg_color = imagecolorallocate($captcha_img, 0x66, 0xCC, 0xFF); //fill background color
  imagefill($captcha_img, 0, 0, $bg_color);

  $line_color = imagecolorallocate($captcha_img, 0x33, 0x99, 0xCC);
  $text_color1 = imagecolorallocate($captcha_img, 0x00, 0x00, 0x00);
  $text_color2 = imagecolorallocate($captcha_img, 0xFF, 0xFF, 0xFF);

  // draw random lines on canvas
  for($i=0; $i < 10; $i++) {
    imagesetthickness($captcha_img, rand(1,3));
    imageline($captcha_img, rand(0,160), 0, rand(0,160), 45, $line_color);
  }

  session_start();

  // Carving test into the image using a mixture of TTF fonts
  $fonts = array();
  $fonts[] = "fonts/modifier_font.ttf";
  $fonts[] = "fonts/destroy_font.ttf";

  // add random digits to canvas using random black/white colour
  $rand_digit = '';
  for($x = 10; $x <= 140; $x += 20) {
    $text_color = (rand() % 2) ? $text_color1 : $text_color2;
    $rand_digit .= ($captcha_num = rand(0, 9));
    imagettftext($captcha_img, 20, rand(-25,25), $x, rand(29, 45), $text_color, $fonts[array_rand($fonts)], $captcha_num);
  }

  // record digits in session variable
  $_SESSION['txt_captcha'] = $rand_digit;

  header('Content-type:image/jpeg'); // Informing Browser there is a image file is coming
  imagejpeg($captcha_img); // Converting Image into JPEG
  imagedestroy($captcha_img); // Clearing Cache Img after use
}
 ?>

似乎我的txt_captcha也给了我关于图片中显示的那个的价值,但我无法检查它,因为我一直在得到一个空白的JavaScript消息。

0 个答案:

没有答案
相关问题