Php 使用jquery进行验证码验证
我有一份申请表,需要使用jquery验证验证码。如果输入的验证码错误,则会显示一个警告框,提示显示请再次输入验证码输入的验证码错误。如何使用jquery执行此操作??请帮帮我。我不熟悉jquery 代码如下:Php 使用jquery进行验证码验证,php,jquery,html,Php,Jquery,Html,我有一份申请表,需要使用jquery验证验证码。如果输入的验证码错误,则会显示一个警告框,提示显示请再次输入验证码输入的验证码错误。如何使用jquery执行此操作??请帮帮我。我不熟悉jquery 代码如下: $(function() { $("#XISubmit").click(function(){ event.preventDefault() val=$('#vercode').val() $.ajax({
$(function() {
$("#XISubmit").click(function(){
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_image.php'>"); }
else{
document.getElementById("XIForm").submit();
}
}
});
});
});
document.getElementById("XIForm").submit();
});
**Html page :**
<label>Security Validation</label>
<span><img src="captcha_image.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_image.php**
<?
// *** CAPTCHA image generation ***
// *** http://frikk.tk ***
session_start();
// *** Tell the browser what kind of file is come'n at 'em! ***
header("Content-Type: image/jpeg");
// *** Send a generated image to the browser ***
die(create_image());
// *** Function List ***
function create_image()
{
// *** Generate a passcode using md5
// (it will be all lowercase hex letters and numbers ***
$md5 = md5(rand(0,9999));
$pass = substr($md5, 10, 5);
// *** Set the session cookie so we know what the passcode is ***
$_SESSION["pass"] = $pass;
// *** Create the image resource ***
$image = ImageCreatetruecolor(100, 20);
// *** We are making two colors, white and black ***
$clr_white = ImageColorAllocate($image, 255, 255, 255);
$clr_black = ImageColorAllocate($image, 0, 0, 0);
// *** Make the background black ***
imagefill($image, 0, 0, $clr_black);
// *** Set the image height and width ***
imagefontheight(15);
imagefontwidth(15);
// *** Add the passcode in white to the image ***
imagestring($image, 5, 30, 3, $pass, $clr_white);
// *** Throw in some lines to trick those cheeky bots! ***
imageline($image, 5, 1, 50, 20, $clr_white);
imageline($image, 60, 1, 96, 20, $clr_white);
// *** Return the newly created image in jpeg format ***
return imagejpeg($image);
// *** Just in case... ***
imagedestroy($image);
}
?>
$(函数(){
$(“#XISubmit”)。单击(函数(){
event.preventDefault()
val=$('#vercode').val()
$.ajax({
类型:“post”,
url:“验证captcha.php”,
数据:{'code':val},
成功:功能(数据){
如果(数据=='false'){
警报('重新输入验证码,您输入了错误的验证码')
$(“#cap”).html(“”;}
否则{
document.getElementById(“XIForm”).submit();
}
}
});
});
});
document.getElementById(“XIForm”).submit();
});
**Html页面:**
安全验证
**captcha_image.php**
您可以执行的步骤
将“提交”按钮更改为“提交”按钮
单击按钮时,调用verify-captcha.php
如果验证码是好的,提交表格
else警报错误消息
以下是已修改(未测试)的脚本:
$(函数(){
$(“#XISubmit”)。单击(函数(){
event.preventDefault()
val=$('#vercode').val()
$.ajax({
类型:“post”,
url:“验证captcha.php”,
数据:{'code':val},
成功:功能(数据){
如果(数据=='false'){
警报('重新输入验证码,您输入了错误的验证码')
$(“#cap”).html(“”;}
否则{
$(“#XIForm”).submit();
}
}
});
});
});
只是一个建议。使用真正的CAPTCHA库,如reCAPTCHA。console.log(data)
在控制台中产生什么?但请注意,提交表单后最好重新检查。客户端解决方案很容易被绕过。它不起作用。获取警报消息重新输入验证码。如果输入了正确的验证码,您确定您的php脚本返回了正确的验证码吗?您可能需要检查php脚本是否正常工作。是否可以发布html?安全验证
<?php
session_start();
if ($_POST["code"] != $_SESSION["cap_code"] || $_SESSION["cap_code"]=='') {
echo 'false';
}else{
echo 'true';}
$(function() {
$("#XISubmit").click(function(){
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").submit();
}
}
});
});
});