Verification code generation and application example
In the Write user authentication page, such as registration, login, in order to enhance the security of user login, add verification code verification.
The verification code generates a PNG image through GD and assigns the $randval random number to $_session[' Login_check_num ', which is judged correctly by comparing the $_post of the user input. To achieve the required functionality, you need to modify the PHP tutorial. ini file so that PHP supports the GD library.
Call this page and generate a CAPTCHA picture if the following formula is true
if ($_get["action"]== "Verifycode")
{
Rand_create ();
}
Verification Code Picture Generation
function Rand_create ()
{
Notifies the browser that a PNG image will be exported
Header ("Content-type:image/png");
Get the random number generator seed ready.
Srand (Double) microtime () *1000000);
Prepare the relevant parameters for the picture
$im = Imagecreate (62,20);
$black = Imagecolorallocate ($im, 0,0,0); RGB Black Identifier
$white = Imagecolorallocate ($im, 255,255,255); RGB White Identifier
$gray = Imagecolorallocate ($im, 200,200,200); RGB Gray Identifiers
Start drawing
Imagefill ($im, 0,0, $gray);
while (($randval =rand ()%100000) <10000); {
$_session["Login_check_num"] = $randval;
Drawing a four-bit integer verification code into a picture
Imagestring ($im, 5, 3, $randval, $black);
}
adding interfering pixels
for ($i =0; $i <200; $i + +) {
$randcolor = Imagecolorallocate ($im, Rand (0,255), Rand (0,255), Rand (0,255));
Imagesetpixel ($im, Rand ()%70, Rand ()%30, $randcolor);
}
Output validation Picture
Imagepng ($im);
Destroy image identifiers
Imagedestroy ($im);
}
Inspection Verification Code
function Rand_check ()
{
if ($_post["reg_rand"] = = $_session["Login_check_num"]) {
return true;
}
else{
Exit ("Verification code input error");
}
}
?>
Verification code is a public automatic program that distinguishes users from computers and people. In the CAPTCHA test, a computer that acts as a server automatically generates an issue that is answered by the user. This problem can be generated and judged by a computer, but only humans can answer it. Since the computer is unable to answer the CAPTCHA question, the user who answers the question can be considered human.
*/
Session_Start ();
$string = null;
$im = Imagecreatetruecolor (60,25); Create a true Color map 60*25
$BG = Imagecolorallocate ($im, 255,255,255);//white background
Imagefill ($im, 0,0, $BG); filled White
$x = 5;//
$y = 0;//text coordinates
for ($i =0; $i <4; $i + +)
{
$char = Mt_rand (0,9);
$string. = $char;
$y = Mt_rand (0,10);
$ccolor = Imagecolorallocate ($im, Mt_rand (0,230), Mt_rand (0,230), Mt_rand (0,230));
Imagechar ($im, 6, $x, $y, $char, $ccolor);//Fill text
$x + = Mt_rand (10,15);
}
for ($i =0; $i
{
$x 1 = mt_rand (0,80);
$x 2 = Mt_rand (0,80);
$y 1 = mt_rand (0,30);
$y 2 = Mt_rand (0,30);
$x 2 = $x 1 +mt_rand (1,5);
$y 2 = $y 1 +mt_rand (1,5);
$LC = Imagecolorallocate ($im, Mt_rand (0,230), Mt_rand (0,230), Mt_rand (0,230));
Imageline ($im, $x 1, $y 1, $x 2, $y 2, $LC);//Fill line
}
$_session[' Code ' = MD5 ($string);
Header ("Content-type:image/jpeg");
Imagepng ($im);
Imagedestroy ($im);
For more detailed information, please see: http://www.bKjia.c0m/phper/php-cy/33707.htm
Of course we can also be able to live with the same knowledge of PHP Ajax to verify the functionality of the instance.
http://www.bkjia.com/PHPjc/629715.html www.bkjia.com true http://www.bkjia.com/PHPjc/629715.html techarticle Verification code generation and application instances in the Write user authentication page, such as registration, login, in order to enhance the security of user login, add verification code verification. Verification code generated PNG image by GD ...