我已經使用驗證碼的形式,如果我嘗試點擊提交(通過鼠標)甚至做出正確的驗證碼警告I'mm越來越之後「驗證碼錯誤」,而可悲的事情如果我按下輸入鍵(通過鍵盤),即使提供錯誤的驗證碼後,它會顯示「謝謝」文本。jQuery的PHP驗證碼驗證
的index.php
<img src="get_captcha.php" alt="" id="captcha" />
<br clear="all" />
<input name="code" class="smoothborder" type="text" id="code" required="required" onblur="checkcode(this.value)">
<div id="wrong" class="error" style="display: none;">Wrong Captcha</div>
</div>
<img src="refresh.jpg" width="25" alt="" id="refresh" style="margin-top:66px;margin-left:10px;" />
function checkcode(value){
$('#wrong').hide();
$.post("contact.php",{ namevalue: "code" }, function(data) {
var returndata = data;
if(returndata == value){
} else {
$('#wrong').show();
$('#code').val('');
}
});
}
Contact.php
<?php
ob_start();
session_start();
if(isset($_REQUEST['namevalue']) != ''){
echo $_SESSION['random_number'];
}else{
$name = $_POST['name'];
$email = $_POST['email'];
$mobile = $_POST['mobile_no'];
$url = $_POST['url'];
$comment = $_POST['comment'];
$response = $_POST['response'];
$Chooseoption = $_POST['ddlselectionvalue'];
$to = '[email protected]';
$from = $name . ' <' . $email . '>';
$subject = 'Message from ' . $name;
$message = 'Select Option: ' . $Chooseoption . '<br/>
Name: ' . $name . '<br/>
Email: ' . $email . '<br/>
Mobile No.: ' . $mobile . '<br/>
URL: ' . $url . '<br/>
Message: ' . nl2br($comment) . '<br/>
Response: ' . $response . '<br/>';
echo $result = sendmail($to, $subject, $message, $from);
if ($result==1){
$result = 'Thank you! We have received your message.';
header('location:index.php?result='.$result);
ob_flush();
exit();
}else{
$result = 'Sorry, unexpected error. Please try again later';
header('location:index.php?result='.$result);
ob_flush();
exit();
}
}
function sendmail($to, $subject, $message, $from) {
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: ' . $from . "\r\n";
$result = mail($to,$subject,$message,$headers);
if ($result) return 1;
else return 0;
}
?>
Get_Captcha.php
<?php
session_start();
$string = '';
for ($i = 0; $i < 5; $i++) {
$string .= chr(rand(97, 122));
}
$_SESSION['random_number'] = $string;
$dir = 'fonts/';
$image = imagecreatetruecolor(165, 50);
$num = rand(1,2);
if($num==1)
{
$font = "Capture it 2.ttf";
}
else
{
$font = "Molot.otf";
}
$num2 = rand(1,2);
if($num2==1)
{
$color = imagecolorallocate($image, 113, 193, 217);
}
else
{
$color = imagecolorallocate($image, 163, 197, 82);
}
$white = imagecolorallocate($image, 255, 255, 255);
imagefilledrectangle($image,0,0,399,99,$white);
imagettftext ($image, 30, 0, 10, 40, $color, $dir.$font, $_SESSION['random_number']);
header("Content-type: image/png");
imagepng($image);
?>