當前位置:名人名言大全網 - 端午節短信 - 如何用javascript代碼判斷用戶在php中輸入的驗證碼是否正確?

如何用javascript代碼判斷用戶在php中輸入的驗證碼是否正確?

驗證碼壹般在服務器中隨機生成,在html頁面上通過ajax獲取,與用戶輸入的驗證碼進行比對。

& ltscript type = " text/JavaScript " & gt;

var xmlHttp

函數createXMLHttpRequest()

{

如果(窗口。ActiveXObject)

{

xmlHttp = new ActiveX object(" Microsoft。XMLHTTP”);

}

else if(窗口。XMLHttpRequest)

{

xmlHttp = new XMLHttpRequest();

}

}

函數doit()

{

createXMLHttpRequest();

xmlhttp . onreadystatechange = handleStateChange;

xmlHttp.open("get "," auth_code.php ",true);

xmlhttp . send(null);

}

函數handleStateChange()

{

if(xmlHttp.readyState==4)

{

if(xmlhttp . status = = 200 | | xmlhttp . status = = 0)

{

checkAuthCodeInput(xmlhttp . responsetext);

}

}

}

函數checkAuthCodeInput(authcode)

{

if(document . getelementbyid(" custom ")。值= =驗證)

{

返回true

}

返回false

}

& lt/script & gt;

& ltimg src = " auth _ code . PHP " id = " img _ auth _ code "/& gt;

& lta href = " document . getelementbyid(' img _ auth _ code ')。src='auth_code.php?code = '+math . random();"& gt我看不清楚。請換壹個。

//本頁面用於隨機獲取驗證碼rand _ auth _ code.php。

& lt?服務器端編程語言(Professional Hypertext Preprocessor的縮寫)

函數get_auth_code()

{

for($ I = 0;$ i & lt5;$i++)

{

$_GLOBALS['rand_str']。=strtoupper(dechex(rand(0,15)));

rand _ str _ width+= imagefontwidth($ I);

}

}

echo $ _ GLOBALS[' rand _ str '];

& gt

& lt?服務器端編程語言(Professional Hypertext Preprocessor的縮寫)

include _ once(" rand _ auth _ code . PHP ");//導入生成驗證碼的頁面。

$ img _ width = 100;

$ img _ height = 25

$ img = imagecreatetruecolor($ img _ width,$ img _ height);

$ img _ BG _ color = imagecolorlallocate($ img,0,0,0);

imagefill($img,0,0,$ img _ BG _ color);

$ img _ font _ color = imagecolorlocate($ img,225,225,225);

get_auth_code()。//調用方法生成隨機驗證碼。

$ img _ font _ x = $ img _ width/2-$ rand _ str _ width/2;

$ img _ font _ y =($ img _ height-imagefont width(5))/2;

imagestring($img,5,$img_font_x,$img_font_y,$_GLOBALS[rand_str],$ img _ font _ color);

頭(" Content-Type:image/JPEG ");

image JPEG($ img);

image destroy($ img);

unset($ _ GLOBALS[' rand _ str ']);

& gt

不知道是否符合妳的要求。