PHP login verification code implementation and use method, php login verification code
This article describes how to implement and use the PHP logon verification code. We will share this with you for your reference. The details are as follows:
1. Create a code. php verification code file.
Before that, you must open the GD library of php, modify the configuration of the php. ini file, and cancel the semicolon before extension = php_gd2.dll. The Code is as follows:
<? Phpsession_start (); // generate the verification code image Header ("Content-type: image/PNG"); $ im = imagecreate (44,18); $ back = ImageColorAllocate ($ im, 245,245,245); imagefill ($ im, 1000000, $ back); // background srand (double) microtime ); // generate four digits for ($ I = 0; $ I <4; $ I ++) {$ font = ImageColorAllocate ($ im, rand (100,255 ), rand (0,100), rand (100,255); $ authnum = rand (); $ vcodes. = $ authnum; imagestring ($ im, 5, 2 + $ I * 10, 1, $ authnum, $ font) ;}for ($ I = 0; $ I <100; $ I ++) // Add interference pixels {$ randcolor = ImageColorallocate ($ im, rand (0,255), rand (0,255), rand (0,255); imagesetpixel ($ im, rand () p, rand () 0, $ randcolor);} ImagePNG ($ im); ImageDestroy ($ im ); $ _ SESSION ['checknum'] = $ vcodes;?>
2. display the verification code Image
Add the verification code to the page
<input type="text" name="passcode" >
3. Determine and obtain the value of the Verification Code
The verification code passes the $ _ SESSION ['checknum'] = $ vcodes; value in the first step of the Code. Therefore, the value of the verification code exists in $ _ SESSION ['checknum. On the verification page, use the following code,
... Session_start (); // start the SESSION $ code = $ _ POST ["passcode"]; if ($ code = $ _ SESSION ["Checknum"]) {...}
You can complete the verification code logon.
Run the following command: