简体   繁体   English

使用jQuery和php验证验证码

[英]validate captcha using jQuery and php

I have a form in php which contains captcha code in it. 我在php中有一个表单,其中包含验证码代码。 I need to validate the captcha using jQuery. 我需要使用jQuery验证验证码。 If the captcha is wrong the form as display an alert saying the captcha you have entered is wrong. 如果验证码是错误的,则显示的表单会提示您输入的验证码是错误的。 Please tell me how can I validate it using jQuery. 请告诉我如何使用jQuery进行验证。 Here is the code.When I add below code it shows be an error undefined _SESSION. 这是代码。当我添加以下代码时,它显示为未定义的_SESSION错误。 How can I solve this. 我该如何解决。

Please help me how can I validate captcha using jQuery 请帮助我如何使用jQuery验证验证码

 $(function() { $("#XISubmit").click(function(){ var photo= document.forms["XIForm"]["photo"].value; var cap = '<?php echo $_SESSION["pass"]; ?>' // try this if (photo==null || photo=="" || photo !=cap) { alert("Please Enter captcha"); return false; } else { document.getElementById("XIForm").submit(); } }); }); 
 <div class="formLeft"> <h4>Admission</h4> <form name="XIForm" id="XIForm" method="POST" action="pdf/pdf.php"> <label>Security Validation</label> <img src="captcha_image.php" alt=""/> <input type="text" name="photo" maxlength="60" size="30"/> <br><br> </form> 

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);
}
?>

please check have started your session or not use 请检查是否已开始会话或不使用

session_start();

or check whether $_SESSION["pass"] is storing any value or not 或检查$_SESSION["pass"]是否存储任何值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM