我有captcha在captcha.php中创建一个图像。在我的联系人php页面上,它要求图像&它有一个重新加载,并且一切正常。我似乎无法找到验证它的方法。验证图片验证码。 jquery PHP&javascript
下面是创建验证码图像的代码:
<?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功能,所以你可以看到我是如何几乎没有他们的休息。
在其引导到这个页面/代码validateCaptcha代码:
<?php
session_start();
if(@strtolower($_REQUEST['captcha-code']) == strtolower($_SESSION['random_number']))
{
echo 1;// YAY!
}
else
{
echo 0; // invalid code
}
?>
任何帮助将非常感激我已经坚持了这么久!我已经尝试了一些东西,但似乎没有任何工作。
任何帮助,第二个链接似乎很接近我有什么,但我的代码无法找到$ _SESSION [ 'RANDOM_NUMBER']。如果我试图回应它或任何东西以查看它是什么,它将显示为空白 – Catia 2012-08-09 17:33:29