Using image processing technology to generate verification code (PHP graphic Image typical Application tutorial 3)

Source: Internet
Author: User
using image processing technology to generate verification code (PHP graphic Image typical Application tutorial 3)

There are many ways to implement verification code, such as digital verification Code, graphics Verification Code and text Verification code. A verification code generated using image processing techniques is described in this section.

In the previous article, "How to use the GD2 function to add text to a picture (PHP graphic image typical application tutorial 2)", we introduced how to add text in the picture, then we continue today to show you how to use image technology to generate verification code.

The following describes the use of image processing technology to generate verification code, the following code:

<?phpsession_start ();//Tell the browser that this file is a PNG image header (' content-type:image/png ');//create Image $image = Imagecreatetruecolor (50,20);//Fill Color-PS in the click on the canvas to fill the color Imagefill ($image, 0,0,imagecolorallocate ($image, 149,188,205));// Add interfering pixels, loop 100 times for ($i = 0; $i < $i + +) {    $randcolor = imagecolorallocate ($image, rand (0, 255), rand (0, 255), R and (0, 255));//The    Imagesetpixel ($image, rand (1), Rand (1,18), $randcolor);} Set Color $red = Imagecolorallocate ($image, 255,255,255); $code = ""; for ($i = 1; $i <=4; $i + +) {    $rand _code = rand (1,9);//Generate 1-9 of random numbers    imagestring ($image, 5, 5+ ($i-1) *10, 2, $rand _code, $red);//write text to a picture c4/> $code. = $rand _code;} Generate Picture Imagepng ($image);//Destroy picture, free memory Imagedestroy ($image); >

The output is:

Above is the simplest example of generating a verification code, and below we describe an example of the tension used in daily development. The steps are as follows:

(1) Create a checks.php file that uses the GD2 function to create a 4-bit verification code and save the generated verification code in the session variable with the following code:

<?phpsession_start ();        Initializes the session variable header ("Content-type:image/png");                          Sets the format for creating images $image_width=70;                          Set the image width $image_height=18;                Set Image Height Srand (microtime () *100000); Set the seed for random number for ($i =0; $i <4; $i + +) {//loop outputs a 4-bit random number $new _number.=dechex (rand (0,15));}       $_session[check_checks]= $new _number;    The obtained random number verification code is written to the session variable $num_image=imagecreate ($image _width, $image _height);         Create a canvas imagecolorallocate ($num _image,255,255,255);                                Set the color of the canvas for ($i =0; $i <strlen ($_session[check_checks]); $i + +) {//loop to read the verification code $font=mt_rand (3,5) in the SESSION variable;               Set Random font $x=mt_rand (1,8) + $image _width* $i/4;                   Sets the x-coordinate where the random character is located $y=mt_rand (1, $image _HEIGHT/4); Sets the y-coordinate where the random character is located $color=imagecolorallocate ($num _image,mt_rand (0,100), Mt_rand (0,150), Mt_rand (0,200));   Set the color of the character imagestring ($num _image, $font, $x, $y, $_session[check_checks][$i], $color); Horizontal output character}IMAGEPNG ($num _image);          Generates Image Imagedestroy ($num _image) in PNG format; Releasing image Resources?>

In the above code, when the verification code is output, the position, color, and font of each character are obtained by random numbers, which can generate a variety of verification codes in the browser, and also prevent malicious users from attacking the website system.

(2) Create a user login form and invoke the checks.php file, output the contents of the image in the form page, submit the form information, and use the IF condition statement to determine if the verification code entered is correct. If the user fills in a verification code equal to the randomly generated verification code, the user is prompted to log in successfully!, and the code is as follows:

<?phpsession_start ();           Initialize sessionif ($_post["Submit"]!= "") {    $checks =$_post["checks"];        Gets the value of the Captcha text box    if ($checks = = "") {           //If the value of the verification code is empty, the popup message        echoes "<script> alert (' Verification code cannot be null '); window.location.href= ' index.php ';</script> ";    } If the user enters a verification code value equal to the value of a randomly generated verification code, the popup login succeeds if    ($checks ==$_session[check_checks]) {        echo "<script> alert (' User Login successful! '); window.location.href= ' index.php ';</script> ";    } else{            //Otherwise pop-up captcha incorrect prompt message        echo "<script> alert (' you entered the CAPTCHA incorrectly! '); window.location.href= ' index.php ';</script> ";    }}? >

Everyone can try on their own computer, here do not post pictures, about the use of image processing technology to generate verification code content is introduced here, below we will introduce the use of GD2 function drawing geometry (PHP graphic image of the typical application tutorial 4)!

Related Article

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.