PHP Generate picture Verification Code Practice Notes _php Tutorial

Source: Internet
Author: User
PHP generated graphics verification code needs to use the PHP GD library and the session to the instance, so the GD library generated images to the user to see, and then by the user input validation submitted to the server and the session stored values to verify, let us look at the whole process.

Windows System GD Library open

Locate the php.ini file Extension=php_gd2.dll remove the front;

Linux System GD Library open

# #检测GD库是否安装命令
php5-m | Grep-i GD
Or
Php-i | Grep-i--color GD
# #如未安装GD库, install the server with the following method
# # # If the source is installed, then add parameters
--with-gd
# # # If it's a Debian Linux system, install it with Apt-get, as follows
Apt-get Install PHP5-GD
# # # If it's a centos system, install it with Yum, as follows
Yum Install PHP-GD
# # # If it's a SUSE Linux system, install with YaST, as follows
Yast-i PHP5_GD

Well, the PHP GD library has been well, let's look at the PHP generate graphics code Image example

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

1, Verification Code Introduction

Verification code is a series of randomly generated numbers or symbols displayed in the form of a picture on the page, the user's visual identification of the verification code information, while the submission of the operation, you need to submit the characters on the picture at the same time, the input submitted verification successful before using a feature. If the submitted character differs from the server session save, the commit information is considered invalid. To avoid automatic program parsing and parsing of images, it is common to randomly generate some interferon on the image or distort the characters to increase the difficulty of automatic identification. After the user submits, the verification code entered by the user is compared to the string saved in session Sessi on, which achieves the verification effect. When the user submits the form, the page that receives the form checks whether the Sessi on and the client-submitted form values are consistent, not
Consistent does not read or write to the database. Session Sessi on allows a small subset of user information to be stored on the server; This information is temporary and is automatically deleted when the user leaves the site.

2. PHP implementation process
PHP Web files are treated as normal HTML Web pages, and can be written in the usual way of editing HTML when editing. Since PHP consumes a very small amount of system resources in use, and has open source code, and is free of charge, now PHP has been more Web application, the following is the process of PHP implementation Verification code:


(1) Generate random numbers
Define the numbers and letters used to display on the image;

Cyclic random extraction of four-bit defined letters and numbers;
The number of words Fulianqi will be four bits;
Save the generated numbers and letters, and place the generated random numbers in the Sessi on variable, and compare them with the content submitted by the user in the future.

The code is as follows Copy Code
<? 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 a picture
Use the picture creation function to determine the size of the picture you create.

The code is as follows Copy Code
<? Php
$ i m = i magecreate (60, 20);
? >


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

The code is as follows Copy Code

<? 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, N., $ gray);
$ li = i magecolor A l loca te ($ I m, 220, 220,
220);
? >


(4) Adding interferon
Without affecting the user input, add a number of interference lines,
interfere with pixels.

The code is as follows Copy Code
<? Php
F or ($ i= 0; $ i< 3; $ i+ +)
{
I mageline ($ i m, rand (0,), rand (0, +), rand (+ 0, +), $ li);
}
F or ($ i= 0; $ i<; $ i+ +)
{
I magesetp I xe L ($ i M, rand ()%, rand ()% 30
, $ gray);
}
? >


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

The code is as follows Copy Code
<? Php
I magest ring ($ i m, 5, 5, $ au t hnu m _ Ses2
Sion, $ wh i te);
? >


(6) Output image
Open Sessi on function;
Use the function to output the image.

The code is as follows Copy Code

<? Phph eader ("Content-type:i mage/png");
Sessi On_Start ();
I magepng ($ i m);
? >

The above verification code shows the following results

In the need to call the verification code to verify the page, the user filled out the verification code form, the system will submit the form verification code data and the above Sessi on variables, if the equality is correct, you can continue; Inequality is an error that terminates the work being done by the user and implements the authentication function of the user using the verification code.

Cases

Untitled Document

The code is as follows Copy Code




Untitled Document






The verifycode.php file code is as follows

The code is as follows Copy Code

/*
Image verification Code Powered by Kason Test http://www.bKjia.c0m */
Session_Start ();
$num =4;//number of verification codes
$width =80;//Verification Code width
$height =20;//Verification Code height
$code = ";
for ($i =0; $i < $num; $i + +)//Generate Verification code
{
Switch (rand (0,2))
{
Case 0: $code [$i]=CHR (rand (48,57)); break;//number
Case 1: $code [$i]=CHR (rand (65,90)); break;//Capital Letter
Case 2: $code [$i]=CHR (rand (97,122)); break;//Small Letter
}
}
$_session["Verifycode"]= $code;
$image =imagecreate ($width, $height);
Imagecolorallocate ($image, 255,255,255);
for ($i =0; $i <80; $i + +)//Generate interference pixels
{
$dis _color=imagecolorallocate ($image, Rand (0,2555), Rand (0,255), Rand (0,255));
Imagesetpixel ($image, rand (1, $width), rand (1, $height), $dis _color);
}
for ($i =0; $i < $num; $i + +)//print character to image
{
$char _color=imagecolorallocate ($image, Rand (0,2555), Rand (0,255), Rand (0,255));
Imagechar ($image, Max, ($width/$num) * $i, Rand (0,5), $code [$i], $char _color);
}
Header ("Content-type:image/png");
Imagepng ($image);//output image to browser
Imagedestroy ($image);//Release resources
?>

checkcode.php files are as follows

The code is as follows Copy Code


Ini_set (' display_errors ', ' Off ');
Session_Start ();
if ((Strtoupper ($_post["code")) = = Strtoupper (($_session["Verifycode"])) {
Print ("Verification code is correct,");
}else{
Print ("Captcha error,");
}
echo "submitted Captcha:". Strtoupper ($_post["code"]). ", correct captcha:". Strtoupper ($_session["Verifycode"]);
?>

http://www.bkjia.com/PHPjc/632946.html www.bkjia.com true http://www.bkjia.com/PHPjc/632946.html techarticle PHP generated graphics verification code needs to use the PHP GD Library and session to the instance, so the GD library generated images to the user to see, and then by the user input validation submitted to the server and the session stored values ...

  • 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.