验证此图像验证码.Jquery PHP & javascript.
Validating this image captcha code. Jquery PHP & javascript
>我让验证码在验证码中创建图像.php。在我的联系php页面上,它调用图像并重新加载,一切正常。我似乎找不到验证它的方法。
以下是创建验证码图像的代码:
<?php
session_start();
$word_1 = '';
$word_2 = '';
for ($i = 0; $i < 4; $i++)
{
$word_1 .= chr(rand(97, 122));
}
for ($i = 0; $i < 4; $i++)
{
$word_2 .= chr(rand(97, 122));
}
$_SESSION['random_number'] = $word_1.' '.$word_2;
$dir = '/addons/fonts/';
$image = imagecreatetruecolor(165, 50);
$font = "recaptchaFont.ttf"; // font style
$color = imagecolorallocate($image, 0, 0, 0);// color
$white = imagecolorallocate($image, 255, 255, 255); // background color white
imagefilledrectangle($image, 0,0, 709, 99, $white);
imagettftext ($image, 22, 0, 5, 30, $color, $dir.$font, $_SESSION['random_number']);
header("Content-type: image/png");
imagepng($image);
?>
这就是该文件中的所有内容。
接下来是我实际表格中的代码(联系我们页面):
<form id="contactForm" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<table>
<tr>
<td><span class="greyText">Enter text shown:</span></td>
<td>
<div id="captcha-wrap">
<div class="captcha-box">
<img src="captcha.php" alt="" id="captcha" />
</div>
<div class="text-box">
<label>Type the two words:</label>
<input name="captcha-code" type="text" id="captcha-code">
</div>
<div class="captcha-action">
<img src="./images/captcha-refresh.png" alt="" id="captcha-refresh" />
</div>
</div>
<span id="captchaInfo">*</span>
</td>
</tr>
<tr>
<td colspan="2">
<div class="seperator"> </div>
<input type="submit" name="submit" class="styleButton" style="float:right;margin-right:10px" value="SUBMIT" /></td>
</tr>
</table>
</form>
我在该表中还有其他用于姓名、电子邮件等的行,但我只是删除了它以使其更短。
在联系页面的文档就绪功能中,我有以下代码在单击按钮时刷新验证码:
// refresh captcha
$('img#captcha-refresh').click(function() {
change_captcha();
});
function change_captcha()
{
document.getElementById('captcha').src='jquery-captcha.php?rnd=' + Math.random();
}
接下来是我对整个表单本身的验证。它适用于所有内容,但验证码除外:
$(document).ready(function(){
//global vars
var form = $("#contactForm");
var name = $("#name");
var nameInfo = $("#nameInfo");
var telephone = $("#telephone");
var telephoneInfo = $("#telephoneInfo");
var email = $("#email");
var emailInfo = $("#emailInfo");
var message = $("#message");
var messageInfo = $("#messageInfo");
var captcha = $("#captcha-code");
var captchaInfo = $("#captchaInfo");
//On blur
name.blur(validateName);
telephone.blur(validatePhone);
email.blur(validateEmail);
message.blur(validateMessage);
captcha.blur(validateCaptcha);
//On key press
name.keyup(validateName);
telephone.keyup(validatePhone);
message.keyup(validateMessage);
captcha.keyup(validateCaptcha);
//On Submitting
form.submit(function(){
if(validateName() & validateEmail() & validatePhone() & validateMessage() & validateCaptcha())
return true
else
return false;
});
function validateName(){
//if it's NOT valid
if(name.val().length < 4){
name.addClass("error");
nameInfo.text("* Name Required");
nameInfo.addClass("error");
return false;
}
//if it's valid
else{
name.removeClass("error");
nameInfo.text("*");
nameInfo.removeClass("error");
return true;
}
}
function validateCaptcha(){
$.post("captchaValidate.php?"+$("#contactForm").serialize(), {
}, function(response){
if(response==1)
{
captcha.removeClass("error");
captchaInfo.text("*");
captchaInfo.removeClass("error");
return true;
}
else
{
captchaInfo.text("* Please enter the words you see in the box above. If you are having trouble seeing it, click the refresh button.");
captchaInfo.addClass("error");
captchaInfo.css({'float': 'left', 'margin-left': '5px'});
return false;
}
});
}
});
我再次删除了上面的一些代码以使其更短,但我保留了 validateName 函数,以便您可以看到我是如何完成其余代码的。
在验证验证码中,它指向此页面/代码:
<?php
session_start();
if(@strtolower($_REQUEST['captcha-code']) == strtolower($_SESSION['random_number']))
{
echo 1;// YAY!
}
else
{
echo 0; // invalid code
}
?>
对此的任何帮助将不胜感激,我已经卡了这么久!我已经尝试了一些方法,但似乎没有任何效果。
阅读以下内容:--
PHP jQuery 验证验证码示例
http://captcha.biz/doc/php/samples/php-jquery-validation-captcha-sample.html
PHP 验证码
http://www.9lessons.info/2011/01/php-captcha-code.html
jquery-validation/demo/captcha
https://github.com/jzaefferer/jquery-validation/blob/master/demo/captcha/index.php
如果您不介意使用简单的数学验证码,那么这个验证码很容易集成到您自己的代码中。 它同时使用 PHP 和 jQuery,因此无论是否启用 Javascript,它都可以工作。 我写了一个教程,并提供了一个项目下载,其中包含所有开箱即用的项目文件。
使用 jQuery 和 PHP 制作简单的数学验证码http://www.sinawiwebdesign.com/blog/topics/programming/jquery-and-php-and-catcha-tutorial/
我希望这有所帮助。
使用 jquery 验证在 php 中创建简单的验证码 示例(如果您有任何帮助)
创建验证码需要三个文件
capcha.php
form.php
check-capcha.php
下载字体 Arial.ttf添加到字体文件夹 http://www5.miele.nl/apps/vg/nl/miele/mielea02.nsf/0e87ea0c369c2704c12568ac005c1831/07583f73269e053ac1257274003344e0?OpenDocument
<?php
session_start();
$string = '';
for ($i = 0; $i < 5; $i++) {
// this numbers refer to numbers of the ascii table (lower case)
$string .= chr(rand(97, 122));
}
$_SESSION['random_code'] = $string;
$data['code'] = $string;
$dir = 'fonts/';
if(!is_dir($dir)){
mkdir($dir);
}
$image = imagecreatetruecolor(170, 60);
$black = imagecolorallocate($image, 0, 0, 0);
$color = imagecolorallocate($image, 200, 100, 90); // red
$white = imagecolorallocate($image, 255, 255, 255);
imagefilledrectangle($image,0,0,399,99,$white);
imagettftext ($image, 30, 0, 10, 40, $color, $dir."arial.ttf", $_SESSION['random_code']);
$save= "test.png";
imagepng($image,$save);
ob_start();
imagedestroy($image);
echo json_encode($data);
?>
form.php
<script src="https://code.jquery.com/jquery-1.12.3.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.15.0/jquery.validate.js"></script>
<script type="application/javascript">
$(document).ready(function(e) {
$('#test').validate({
rules:{
capcha:{required: true,
remote: {
url: "check-capcha.php",
type: "post",
data:{hiddencapcha:function(){return $('#hidden_capcha').val()}}
}
}
},
messages:{
capcha:{
remote:"invalid capcha"
}
}
});
$.ajax({
url: "capcha.php",
success: function( result ) {
var newd = JSON.parse(result);
$('input[name=hidden_capcha]').val(newd.code);
$('#capchas').attr('src',"test.png?"+ new Date().getTime());
},error: function(){ alert(result)}
});
$('#generate_capcha').click(function(){
$.ajax({
url: "capcha.php",
success: function( result ) {
var newd = JSON.parse(result);
$('input[name=hidden_capcha]').val(newd.code);
$('#capchas').attr('src',"test.png?"+ new Date().getTime());
},error: function(){ alert(result)}
});
});
});
</script>
<form name="test" id="test">
<span id="capchas_images"><img id="capchas" src="test.png" /></span> <a href="javascript:void(0)" id="generate_capcha">Refresh</a>
<input type="text" name="capcha" />
<input type="hidden" name="hidden_capcha" id="hidden_capcha" />
</form>
check-capch.php
<?php
if($_POST['capcha']==$_POST['hiddencapcha'])
{
echo "true";
}else{
echo "false";
}
?>
http://myphpinformation.blogspot.in/2016/05/create-simple-capcha-in-php-with-jquery-validation.html
- 正在验证8个真/假复选框或复选框中的2个
- jQuery工具验证器自定义效果-添加&消除影响
- 使用javascript&自定义验证器检查开始日期和结束日期
- 单选按钮的验证(启用)&使用Jquery Optimization从复选框中选择submit按钮
- 输入验证&使用javascript或asp返回
- Jquery&Ajax表单验证功能
- 表单验证和提交时提交文本&输入按钮上带有验证的图像
- Firefox加载项验证提供了document.write&innerHTML错误
- 验证文本框&运行另一个函数javascript
- 使代码变短&表单验证
- Ionic&Angular JS表单验证在占位符中显示消息
- JQuery表单验证&按钮波纹
- 无法验证表单是否已用CasperJS&jQuery
- REGEXP的JavaScript中的标题验证,可以采用字母数字,也可以采用特殊字符[,/()&-:.space
- 我如何验证一个文本框,它应该只允许字符a-Z&a-z,不是整数也不是特殊字符
- 引导验证程序&图像选取器-如果选择了两个可用的选择输入中的任何一个,如何验证表单
- Facebook连接,验证一个网站., amp;和,
- 如何验证只接受第一个字母作为字符&在javascript中保留为数字
- 验证&Web服务的验证规则
- javascript表单验证不适用于chrome&FF