The implementation principle of PHP verification code is comprehensively parsed. A small case of php verification code is provided to parse the verification code comprehensively.
Expansion
We need to enable gd expansion. You can use the following code to check whether gd expansion is enabled.
<?phpecho "Hello World!!!!";echo phpinfo();?>
Then, press Ctrl + F on the browser to find the gd option to verify whether you have installed the extension. If not, you need to install the extension on your own.
Background Image
Imagecreatetruecolor
Black background is generated by default.
<? Php // use imagecreatetruecolor () of gd; create a background image $ image = imagecreatetruecolor ); // when displaying this image, you must first declare the header information header ('content-type: image/png '); imagepng ($ image); // release resources, destroy the execution object imagedestroy ($ image );
Imagecolorallocate
Create a fill color and attach it using imagefill (image, x, y, color.
<? Php // use imagecreatetruecolor () of gd; create a background image $ image = imagecreatetruecolor (255,255,255); // generate a fill color $ bgcolor = imagecolorallocate ($ image ); // fill the fill color with imagefill ($ image, $ bgcolor) on the background image ); // when displaying this image, you must first declare the header information header ('content-type: image/png '); imagepng ($ image); // release resources, destroy the execution object imagedestroy ($ image );
Imagepng
Before using this method, you must first set the header information. Otherwise, the image will not be displayed normally.
Imagedestory (image)
Releasing resources in a timely manner reduces the pressure on server requests.
Simple Digital Verification Code
Imagecolorallocate
Generate color information to facilitate future authorization.
$ Fontcolor = imagecolorallocate ($ image, rand (0,255), rand (0,255), rand (0,255 ));
Imagestring
Write the content information to the corresponding position of the image.
Imagestring ($ image, $ fontsize, $ x, $ y, $ fontcontent, $ fontcolor );
Added recognition interference
// Add a point // generate some interference points. Here, there are 200 for ($ I = 0; $ I <200; $ I ++) {$ pointcolor = imagecolorallocate ($ image, rand (50,255), rand (50,255), rand (50,255); imagesetpixel ($ image, rand (0,100), rand ), $ pointcolor);} // Add a line // generate some interference lines. Five for ($ I = 0; $ I <5; $ I ++) {// set it to a light-colored line to prevent noise. $ linecolor = imagecolorallocate ($ image, rand (50,255), rand (50,255), rand (50,255); imageline ($ image, rand (), $ linecolor );}
Mixed Verification Code
<? Php // use imagecreatetruecolor () of gd; create a background image $ image = imagecreatetruecolor (255,255,255, 40); // generate a fill color $ bgcolor = imagecolorallocate ($ image ); // fill the fill color with imagefill ($ image, $ bgcolor) on the background image ); /////// generate a random 4-digit verification code with a mix of letters and numbers for ($ I = 0; $ I <4; $ I ++) {$ fontsize = rand (6, 8); $ fontcolor = imagecolorallocate ($ image, rand (0,255), rand (0,255), rand (0,255 )); // to prevent the user from being difficult to identify, some ambiguous letters and numbers are removed. $ rawstr = 'signature'; $ fontcontent = substr ($ rawstr, rand (0, strlen ($ rawstr), 1); // avoid overlapping images generated $ x + = 20; $ y = rand (); imagestring ($ image, $ fontsize, $ x, $ y, $ fontcontent, $ fontcolor);} // generates interference points. Here, there are 200 for ($ I = 0; $ I <200; $ I ++) {$ pointcolor = imagecolorallocate ($ image, rand (50,255), rand (50,255), rand (50,255); imagesetpixel ($ image, rand (0,100 ), rand (), $ pointcolor);} // generate some interference lines here 4 for ($ I = 0; $ I <4; $ I ++) {// set it to a light-colored line to prevent noise. $ linecolor = imagecolorallocate ($ image, rand (50,255), rand (50,255), rand (50,255); imageline ($ image, rand (), $ linecolor);} header ('content-type: image/png '); imagepng ($ image); // release resources and destroy the execution object imagedestroy ($ image );
Use verification code
Time to enable session
Note:Start the session
Verification Principle
The verification process is to compare the verification code entered by the client with the verification code in the session domain. That is:
if(isset($_REQUEST['checkcode'])){ session_start(); if($_REQUEST['checkcode']==$_SESSION['checkcode']){ echo "<font color='green'>Success!</font>"; }else{ echo "<font color='red'>Failed!</font>"; } exit(); }
Optimization Verification
However, it is easy to verify the case sensitivity of letters. So we need to convert all the user input values into lowercase letters.
If (strtolower ($ _ REQUEST ['checkcode']) ==$ _ SESSION ['checkcode']) {·}
Small Cases
Generate Verification Code
<? Phpsession_start (); // It must be declared at the beginning of php to enable session // use imagecreatetruecolor () of gd; create a background image $ image = imagecreatetruecolor (, 40 ); // generate the fill color $ bgcolor = imagecolorallocate ($ image, 255,255,255); // fill the fill color to the background image imagefill ($ image, $ bgcolor ); /////// generate a random 4-digit verification code with a mix of letters and numbers $ checkcode = ''; for ($ I = 0; $ I <4; $ I ++) {$ fontsize = rand (6, 8); $ fontcolor = imagecolorallocate ($ image, rand (0,255), rand (0,255), rand (0,255 )); // some ambiguous letters and numbers are removed to avoid user identification. $ rawstr = 'abcdefghjkmnopqrstuvwxyz23456789 '; $ fontcontent = substr ($ rawstr, rand (0, strlen ($ rawstr), 1); // concatenate the forthcoming Verification Code $ checkcode. = $ fontcontent; // avoid overlapping images generated $ x + = 20; $ y = rand (); imagestring ($ image, $ fontsize, $ x, $ y, $ fontcontent, $ fontcolor);} // save it to the session variable $ _ SESSION ['checkcode'] = $ checkcode; // generate interference points, here are 200 for ($ I = 0; $ I <200; $ I ++) {$ pointcolor = imagecolorallocate ($ image, rand (50,255), rand (50,255 ), rand (50,255); imagesetpixel ($ image, rand (0,100), rand (), $ pointcolor );} // generate some interference lines. Here there are 4 for ($ I = 0; $ I <4; $ I ++) {// set the line to a light color, prevents leeching. $ linecolor = imagecolorallocate ($ image, rand (50,255), rand (50,255), rand (50,255); imageline ($ image, rand ), rand (), $ linecolor);} header ('content-type: image/png '); imagepng ($ image ); // release the resource and destroy the execution object imagedestroy ($ image );
Form Verification
<? Phpheader ("Content-Type: text/html; charset = utf8"); if (isset ($ _ REQUEST ['checkcode']) {session_start (); if (strtolower ($ _ REQUEST ['checkcode']) ==$ _ SESSION ['checkcode']) {echo "<font color = 'green'> Success! </Font> ";}else {echo" <font color = 'red'> Failed! </Font> ";}exit () ;}?> <! DOCTYPE html>
Summary
Finally, let's make a summary.
• Using php to create verification codes requires the support of gd extension.
• Use imagecreatetruecolor to generate the background color and use imagefill to fill in a color generated by imagecolorallocate.
• Use imagestring to combine the verification code with the background image
• Use imagesetpixel to add interference points
• Use imageline to add interference lines
• Enable the session_start () method at the beginning before using session
• Use JavaScript to dynamically modify the src of the verification code to meet the user's "change one" requirement.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.