Describes in detail the principles of PHP verification code implementation

Source: Internet
Author: User
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. Expansion

We need to enable gd expansion. you can use the following code to check whether gd expansion is enabled.

 

Then in the browserCtrl+FFind 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.

 Imagecolorallocate

Create a fill color and attach it using imagefill (image, x, y, color.

  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);
Increase recognition interference points
// 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 line
// Generate some interference lines 5 for ($ I = 0; $ I <5; $ I ++) {// Set it to a light color line, prevents leeching. $ linecolor = imagecolorallocate ($ image, rand (50,255), rand (50,255), rand (50,255); imageline ($ image, rand ), rand (), $ linecolor );}
Mixed verification code
   When the verification code is used to enable the session
   

Note: You must 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 "Success!";         }else{                    echo "Failed!";            }                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']){···}
Verification code generated in small cases
    Form verification
                 
         Verification code informationScript function change () {document. getElementById ("image_checkcode"). src = './store. php? R = '+ Math. random ();} script        
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 a detailed description of the principles of PHP verification code implementation. For more information, see other related articles in the first PHP community!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.