使用jQuery / AJAX / PHP轻松设置验证码

recaptcha2

什么是验证码?

如今,当您使用表格时,您需要某种形式的保护,使其免受僵尸程序和垃圾邮件发送者的攻击。 减少垃圾邮件的一种方法是使用一种称为Captcha(验证码)的东西-它简单地将人类可读的单词放入其中,使用时必须在框中输入以证明它们是人类。 有很多免费的,我用了几个,发现Google reCaptcha非常容易安装和使用。

jQuery Captcha演示
下载源文件

  • jquery4ucaptcha.zip
    • showform.php
    • jquerycaptcha.js
    • validateform.php
    • recaptchalib.php

怎么运行的

captcha-diagram

解决了一个常见问题

用户输入了验证码并输入了错误的密码。 现在,当他们单击返回时,他们将丢失刚刚填写的字段中的所有表单数据! 灾害! 幸运的是,我想出了一种发送AJAX请求的方法,这样,如果验证码错误,就不会丢失任何表单输入数据。 如果验证码正确,则仅提示用户提交。

如何设置表格验证码

步骤1.您需要从Google reCaptcha网站获取自己的设置密钥。 您将同时需要两个私钥和一个公钥才能使它同时起作用。

步骤2.下载并保存recaptchalib.php

步骤3. jQuery代码– jquerycaptcha.js

//Validate the Recaptcha' Before continuing with POST ACTION
function validateCaptcha()
{
	challengeField = $("input#recaptcha_challenge_field").val();
	responseField = $("input#recaptcha_response_field").val();

	var html = $.ajax({
		type: "POST",
		url: "../php/validateform.php",
		data: "form=signup&recaptcha_challenge_field=" + challengeField + "&recaptcha_response_field=" + responseField,
		async: false
		}).responseText;

	//console.log( html );
	if(html == "success") {
		//Add the Action to the Form
		$("form").attr("action", "../php/db-process-form.php");
		$("#submit").attr("value", "Submit");
		//Indicate a Successful Captcha
		$("#captcha-status").html("

Success! Thanks you may now proceed.

"); } else { $("#captcha-status").html("

The security code you entered did not match. Please try again.

"); Recaptcha.reload(); } }

步骤4. PHP Code1 – validateform.php

require_once("recaptchalib.php");

$privatekey = "[yourprivatekeyhere]";
$resp = recaptcha_check_answer ($privatekey,
         $_SERVER["REMOTE_ADDR"],
         $_POST["recaptcha_challenge_field"],
         $_POST["recaptcha_response_field"]);

if (!$resp->is_valid) {
	// What happens when the CAPTCHA was entered incorrectly
    echo "fail";
} else {
	echo "success";
}

步骤5. PHP Code2 – showcaptcha.php

require_once("recaptchalib.php");
$publickey = "[yourpublickeyhere]";
// show the captcha
echo recaptcha_get_html($publickey);

步骤6. HTML代码

  
  
   
I agree to the terms-and-conditions Terms and Conditions 
 
  
   
 
  
  

Type in the words below (separated by a space):

< ?php require_once("phpfunc-showcaptcha.php"); ?>

第7步。
最好的办法是隐藏验证码,直到用户填写表格或在“同意条款和条件”按钮上打勾。 这是您的操作方式。

$(document).ready(function() {
	if ($('input[name=termsckb]').attr('checked') != true) {
		$('#captcha-wrap').hide();
	}

	$('#termsckb').change(function() {
		 if ($('input[name=termsckb]').attr('checked') == true) {
			 $('#captcha-wrap').show();
			 $("#signupbutton").attr("value", "I am human!");
		 }
		 else {
			 $('#captcha-wrap').hide();
			 $("#signupbutton").attr("value", "Submit");
		 }
	});
});

步骤8.自定义验证码样式
您可以更改验证码的样式和颜色以适合您的网站。 官方网站上有4种不同的选择:红色,白色,黑色和透明。

  
  
   
  

有关更多信息,请参见自定义reCAPTCHA的外观 。 我个人更喜欢clear(透明)选项。

From: https://www.sitepoint.com/setup-user-friendly-captcha-jqueryphp/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个使用jQuery实现动态验证码的示例: HTML代码: ``` <form> <label for="email">电子邮件:</label> <input type="text" id="email" name="email"><br> <label for="captcha">验证码:</label> <input type="text" id="captcha" name="captcha"> <img src="#" alt="验证码" id="captchaImg"> <a href="#" id="refreshCaptcha">换一张</a><br> <input type="submit" value="提交"> </form> ``` JavaScript代码: ``` $(document).ready(function() { var captchaUrl = 'captcha.php'; //验证码图片的URL地址 var captchaId = ''; //验证码ID //刷新验证码图片 function refreshCaptcha() { captchaId = ''; //清空验证码ID $('#captchaImg').attr('src', captchaUrl + '?t=' + new Date().getTime()); //重新加载图片 } //点击“换一张”链接时,刷新验证码 $('#refreshCaptcha').click(function() { refreshCaptcha(); return false; //阻止默认链接行为 }); //在页面加载时,刷新验证码 refreshCaptcha(); //表单提交时,验证验证码是否正确 $('form').submit(function() { var captchaInput = $('#captcha'); //验证码输入框 var captchaValue = captchaInput.val(); //输入的验证码 if (captchaValue === '') { alert('请输入验证码'); captchaInput.focus(); return false; } $.ajax({ url: 'checkCaptcha.php', //验证验证码的URL地址 type: 'POST', data: { id: captchaId, value: captchaValue }, success: function(result) { if (result === 'true') { //验证码正确,表单提交 return true; } else { //验证码错误,刷新验证码 alert('验证码错误'); refreshCaptcha(); captchaInput.val(''); captchaInput.focus(); return false; } }, error: function(jqXHR, textStatus, errorThrown) { alert('出现错误:' + textStatus); refreshCaptcha(); captchaInput.val(''); captchaInput.focus(); return false; } }); return false; }); }); ``` 在上面的代码中,我们首先定义了一个变量`captchaUrl`,表示验证码图片的URL地址。然后定义了一个`refreshCaptcha`函数,用于刷新验证码图片。在函数内部,我们首先清空了验证码ID,然后使用jQuery更新了验证码图片的`src`属性,加上了一个随机参数`t`,以便每次刷新图片都会产生新的URL地址,从而避免浏览器缓存。接着,在页面加载时,我们调用了`refreshCaptcha`函数,以显示初始的验证码图片。 接下来,我们使用jQuery绑定了“换一张”链接的`click`事件,当用户点击该链接时,会调用`refreshCaptcha`函数,刷新验证码。 最后,我们使用jQuery绑定了表单的`submit`事件,在表单提交之前,首先验证用户输入的验证码是否正确。如果输入的验证码为空,会弹出提示框,要求用户输入验证码。如果输入的验证码不为空,则使用jQuery的`ajax`方法向服务器端发送验证请求,包括验证码ID和输入的验证码值。服务器端会根据这些参数,验证验证码是否正确,并返回一个布尔值。如果验证码正确,则返回`true`,表单提交;否则,返回`false`,并弹出提示框,要求用户重新输入验证码。在验证失败时,我们还调用了`refreshCaptcha`函数,刷新验证码图片,以便用户重新输入验证码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值