PHP generates image verification code exercise NOTE _ PHP Tutorial

Source: Internet
Author: User
PHP generates image verification code exercise notes. Php needs to use phpgd library and session to generate a graphic verification code. in this way, the gd library generates images for users to see, then, the user enters the verification code and submits it to the server and the stored values in the session for php verification to generate the graphic verification code. The php gd library and session are used for the instance, so that the gd library generates an image for the user to see, then the user enters the verification and submits it to the server and the stored values in the session for verification. let's take a look at the entire process.

Windows GD Library enabled

Find the php. ini file extension = php_gd2.dll and remove the previous one.

Linux GD Library enabled

# Check whether the GD Library has the Installation Command
Php5-m | grep-I gd
Or
Php-I | grep-I -- color gd
# If the GD library is not installed, install it on the server as follows:
### If the installation is source code, add the parameter
-- With-gd
### If it is a debian linux system, use apt-get to install it, as shown below:
Apt-get install php5-gd
### If it is a CentOS system, install it using yum, as shown below:
Yum install php-gd
### If the suse linux system is used, install it using yast, as shown below:
Yast-I php5_gd

Okay, php GD Library is ready. let's take a look at the graph verification code Image instance generated by php.

First, let's introduce the simple concept of the verification code!

1. verification code introduction

A verification code is a string of randomly generated numbers or symbols displayed on the page in the form of images, the user can identify the verification code information with the naked eye, while submitting the operation, you must submit all the characters in the image at the same time. you can use this function only after the input is submitted for verification. If the submitted characters are different from those saved by the server session, the submitted information is considered invalid. To avoid automatic program analysis and resolution of images, some interferon is randomly generated on images or characters are distorted to increase the difficulty of automatic identification. After the user submits the request, the verification code entered by the user is compared with the string saved in the session sessi on to verify the result. When a user submits a form, the receiving form page checks whether the sessi on generated by the server is consistent with the form value submitted by the client.
If they are consistent, they are not read or written to the database. Session sessi on allows a small part of user information to be stored on the server. This type of information is temporary and will be automatically deleted when the user leaves the website.

2. PHP implementation process
PHP webpage files are processed as common HTML webpage files. during editing, you can use the regular HTML editing method to compile them. Because PHP consumes a considerable amount of system resources and has open source code and is free of charge, PHP has been used by more websites, the following describes how PHP implements the verification code:


(1) generate a random number
Defines the numbers and letters used to display on the image;

Four defined letters and numbers are randomly selected cyclically;
Connects the characters obtained from numbers to a total of four digits;
Save the generated numbers and letters, and place the generated random number in the sessi on variable to compare it with the content submitted by the user in the future.

The code is as follows:
<? Php
$ Aut hnum_session = ';'
$ St r = a' bcdef ghij k l mnopqrstuv wxyz 1234567890'
;
$ L = strlen ($ str );
F or ($ I = 1; $ I <= 4; $ I ++)
{
$ Num = rand (0, $ l-1 );
$ Aut hnum_session. = $ str [$ num]; $ aut hnum_session. = $ str [$ num];
}
$ _ Sessi on ["authnum_ses sion"];
? >


(2) create an image
Use the image creation function to determine the size of the created image.

The code is as follows:
<? Php
$ I m = I magecreate (60, 20 );
? >


(3) set the color
Use the function to create a background color;
Use the function to create a font color.

The code is as follows:

<? Php
$ B lack = ImageColor A ll ocate ($ I m, 0, 0, 0 );
$ White = ImageColor A ll ocate ($ I m, 255,255,
255 );
$ Gray = I mageColor A ll ocate ($ I m, 200,200,
200 );
I magefill ($ I m, 68, 30, $ gray );
$ Li = I mageColor A l loca te ($ I m, 220,220,
220 );
? >


(4) Add interferon
Add several interference lines,
Interference pixels.

The code is as follows:
<? Php
F or ($ I = 0; $ I <3; $ I ++)
{
I mageline ($ I m, rand (0, 30), rand (0, 21), rand (20, 40), rand (0, 21), $ li );
}
F or ($ I = 0; $ I <90; $ I ++)
{
I magesetp I xe l ($ I m, rand () % 70, rand () % 30
, $ Gray );
}
? >


(5) write the characters in the upper left corner of the image
Use the I magestri ng function to write characters on the image.

The code is as follows:
<? Php
I magest ring ($ I m, 5, 12, 5, $ au t hnu m _ ses2
Sion, $ wh I te );
? >


(6) output image
Enable sessi on;
Use functions to output images.

The code is as follows:

<? PhpH eader ("Content-type: I mage/png ");
Sessi on_start ();
I magePNG ($ I m );
? >

The result of the preceding verification code is as follows:

On the page on which you need to call the verification code for verification, the user enters the verification code form. The system compares the verification code data submitted in the form with the preceding sessi on variable. if they are equal, the verification is correct, you can continue. if they are not equal, an error occurs. you can terminate your ongoing work and use the verification code to verify your work.

Example

Untitled Document

The code is as follows:




Untitled Document

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.