Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用jquery验证验证码_Php_Jquery_Html - Fatal编程技术网

Php 使用jquery验证验证码

Php 使用jquery验证验证码,php,jquery,html,Php,Jquery,Html,我有一份申请表,需要使用jquery验证验证码。如果输入的验证码错误,则会显示一个警告框,提示您再次输入验证码。输入的验证码错误。。如何使用jquery实现这一点。。请帮助我,我是jquery的新手 这是密码 $(function() { $("#XISubmit").click(function(){ $(document).ready(function(){ $("#XIForm").submit(function(event) { event.preventDef

我有一份申请表,需要使用jquery验证验证码。如果输入的验证码错误,则会显示一个警告框,提示您再次输入验证码。输入的验证码错误。。如何使用jquery实现这一点。。请帮助我,我是jquery的新手

这是密码

 $(function() {
        $("#XISubmit").click(function(){
    $(document).ready(function(){
$("#XIForm").submit(function(event) {
event.preventDefault()
val=$('#vercode').val()
$.ajax({
        type:"post",
        url:"verify-captcha.php",
        data:{'code':val},
        success:function(data){
                 if(data=='false'){
                alert('Re-enter Captcha, You Entered wrong captcha code')
                $("#cap").html("<img  src='captcha.php'>"); }
                            else{  $("#XIForm").unbind('submit')}
                            }

        });                   
});

});


            document.getElementById("XIForm").submit();

                });

    <label>Security Validation</label>  

      <label>Security Validation</label>    

 <span><img   src="captcha.php"></span><input type="text" name="vercode" id="vercode" size="10" style="margin-top: -1px; float: left; width: 115px; margin-right: 12px;"></li><div id="msg"></div>

    captcha.php

    <?php
session_start();
$ranStr = md5(microtime());
$ranStr = substr($ranStr, 0, 6);
$_SESSION['cap_code'] = $ranStr;
$newImage = imagecreatefromjpeg("cap_bg.jpg");
$txtColor = imagecolorallocate($newImage, 0, 0, 0);
imagestring($newImage, 5, 5, 5, $ranStr, $txtColor);
header("Content-type: image/jpeg");
imagejpeg($newImage);
?>



verify-captcha.php
<?php
session_start(); 
if ($_POST["code"] != $_SESSION["cap_code"] || $_SESSION["cap_code"]=='')  { 
 echo 'false';
}else{
echo 'true';}
$(函数(){
$(“#XISubmit”)。单击(函数(){
$(文档).ready(函数(){
$(“#XIForm”).submit(函数(事件){
event.preventDefault()
val=$('#vercode').val()
$.ajax({
类型:“post”,
url:“验证captcha.php”,
数据:{'code':val},
成功:功能(数据){
如果(数据=='false'){
警报('重新输入验证码,您输入了错误的验证码')
$(“#cap”).html(“”;}
else{$(“#XIForm”).unbind('submit')}
}
});                   
});
});
document.getElementById(“XIForm”).submit();
});
安全验证
安全验证

captcha.php
verify-captcha.php
Html代码

<span><img   src="captcha.php"></span><input type="text" name="vercode" id="vercode" size="10" style="margin-top: -1px; float: left; width: 115px; margin-right: 12px;"></li><div id="msg"></div>

captcha.php的代码

<?php
session_start();
$ranStr = md5(microtime());
$ranStr = substr($ranStr, 0, 6);
$_SESSION['cap_code'] = $ranStr;
$newImage = imagecreatefromjpeg("cap_bg.jpg");
$txtColor = imagecolorallocate($newImage, 0, 0, 0);
imagestring($newImage, 5, 5, 5, $ranStr, $txtColor);
header("Content-type: image/jpeg");
imagejpeg($newImage);
?>
<?php
session_start(); 
if ($_POST["code"] != $_SESSION["cap_code"] || $_SESSION["cap_code"]=='')  { 
 echo 'false';
}else{
echo 'true';}

验证码的Jquery代码

$(document).ready(function(){
$("#form").submit(function(event) {
event.preventDefault()
val=$('#vercode').val()
$.ajax({
        type:"post",
        url:"verify-captcha.php",
        data:{'code':val},
        success:function(data){
                 if(data=='false'){
                alert('Re-enter Captcha, You Entered wrong captcha code')
                $("#cap").html("<img  src='captcha.php'>"); }
                            else{  $("#form").unbind('submit')}
                            }

        });                   
});

});
$(文档).ready(函数(){
$(“#表格”)。提交(功能(事件){
event.preventDefault()
val=$('#vercode').val()
$.ajax({
类型:“post”,
url:“验证captcha.php”,
数据:{'code':val},
成功:功能(数据){
如果(数据=='false'){
警报('重新输入验证码,您输入了错误的验证码')
$(“#cap”).html(“”;}
else{$(“#表单”).unbind('submit')}
}
});                   
});
});
verify-captcha.php的代码

<?php
session_start();
$ranStr = md5(microtime());
$ranStr = substr($ranStr, 0, 6);
$_SESSION['cap_code'] = $ranStr;
$newImage = imagecreatefromjpeg("cap_bg.jpg");
$txtColor = imagecolorallocate($newImage, 0, 0, 0);
imagestring($newImage, 5, 5, 5, $ranStr, $txtColor);
header("Content-type: image/jpeg");
imagejpeg($newImage);
?>
<?php
session_start(); 
if ($_POST["code"] != $_SESSION["cap_code"] || $_SESSION["cap_code"]=='')  { 
 echo 'false';
}else{
echo 'true';}

为此,您需要javascript中的CAPTCHA值。但是,攻击者可以读取此值并绕过CAPTCHA。因此,这使得CAPTCHA保护毫无用处。@mesutozer XHR?哈希?@bjb568不认为XHR是一个选项?哈希是什么意思?@mesutozer为什么XHR不是一个选项?它肯定是一个选项,但我想不起来。我知道哈希是什么意思,但我不明白它在这种情况下是如何相关的?这里可以绕过captcha重新生成。旧的captcha应该因尝试失败而无效。它不起作用。表单在提交时没有输入captcha代码。这是编辑过的代码。