簡體   English   中英

使用jQuery和php驗證驗證碼

[英]validate captcha using jQuery and php

我在php中有一個表單,其中包含驗證碼代碼。 我需要使用jQuery驗證驗證碼。 如果驗證碼是錯誤的,則顯示的表單會提示您輸入的驗證碼是錯誤的。 請告訴我如何使用jQuery進行驗證。 這是代碼。當我添加以下代碼時,它顯示為未定義的_SESSION錯誤。 我該如何解決。

請幫助我如何使用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);
}
?>

請檢查是否已開始會話或不使用

session_start();

或檢查$_SESSION["pass"]是否存儲任何值

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM