Joke Collection Website - Public benefit messages - How to use javascript code to judge whether the verification code entered by the user in php is correct?

How to use javascript code to judge whether the verification code entered by the user in php is correct?

The verification code is generally generated randomly in the server, obtained through ajax on the html page, and compared with the verification code entered by the user.

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

var xmlHttp

Function createXMLHttpRequest ()

{

If (window. ActiveXObject)

{

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

}

Else if (window. XMLHttpRequest)

{

xmlHttp = new XMLHttpRequest();

}

}

Function doit ()

{

createXMLHttpRequest();

xmlhttp . onreadystatechange = handleStateChange;

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

xmlhttp . send(null);

}

Function handleStateChange ()

{

if(xmlHttp.readyState==4)

{

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

{

checkAuthCodeInput(xmlhttp . responsetext);

}

}

}

Function checkAuthCodeInput(authcode)

{

if(document . getelementbyid(" custom ")。 Value = = verification)

{

Return true

}

Returns 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 I can't see clearly. Please change it.

//This page is used to randomly obtain the verification code rand _ auth _ code.php.

& lt? Server-side programming language (abbreviation of professional hypertext preprocessor)

The function 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? Server-side programming language (abbreviation of professional hypertext preprocessor)

include _ once(" rand _ auth _ code . PHP "); //Import the page that generates the verification code.

$ 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()。 //Call the method to generate a random verification 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);

Header ("content-type: image/JPEG");

image JPEG($ img);

image destroy($ img);

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

& gt

I wonder if it meets your requirements.