PHP Generate picture Verification Code Practice notes

Source: Internet
Author: User
Tags chr install php php file rand

Windows System GD Library open

Find the php.ini file Extension=php_gd2.dll remove the front; it's okay.

Linux System GD Library open

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

Well, the PHP GD library has been well, let's look at PHP to generate graphics verification code picture instances

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

1, authentication Code Introduction

Verification code is a series of randomly generated numbers or symbols in the form of pictures displayed on the page, by the user to identify the visual identity of the authentication code information, in the submission of the operation, it is necessary to submit the characters on the picture at the same time, the input submitted to verify the successful use of a function. If the committed character is different from the server session, the submission information is considered invalid. In order to avoid automated program analysis and resolution of pictures, usually randomly generated in the picture some interferon or the characters to distort, increase the difficulty of automatic recognition. The validation results are compared to the strings saved by the user's input of the validation code in session Sessi on after the user submits. When the user submits the form, the pages that receive the form check that the Sessi on and the client-submitted form values are consistent and do not
Consistency does not read or write to the database. Session Sessi on allows small portions of user information to be stored on the server, which is temporary and is automatically deleted when the user leaves the site.

2. PHP implementation process
PHP Web files are treated as generic HTML Web pages, and can be edited using the usual method of editing HTML. Because PHP consumes a very small amount of system resources in use, and has open source code, and is free, now PHP has been more Web applications, the following is the process of PHP implementation Verification code:


(1) Generating random numbers
Define the numbers and letters to be displayed on the picture;

The loop randomly extracts four-bit-defined letters and numbers;
will be through the number of words Fulianqi to a total of four bits;
Save the generated numbers and letters, place the generated random numbers in the Sessi on variable, and compare them to 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 created.

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, +, $ 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,
Disturbing 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 (a), 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 results as follows

In the need to call the verification code to verify the page, the user fills in the verification code form, the system will submit the form of the Verification code data and the above Sessi on variable on the comparison, if the equality of the verification correct, you can continue; Inequality is an error that terminates the work being done by the user and enables the user to verify the authentication code.

Cases

No title document

The code is as follows Copy Code
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title> Untitled Document </title>
<script language= "JavaScript" >
function Refresh_code ()
{
Form1.imgcode.src= "Verifycode.php?a=" +math.random ();
}
</script>
<body>
<form id= "Form1" Name= "Form1" method= "Post" action= "checkcode.php" >
<label for= "Code" > Verification Code:</label>
<input type= "text" name= "code" id= "TextField"/>

<a href= "Javascript:refresh_code ()" > Can't see clearly? Change a </a>
<input type= "Submit" name= "button" id= "button" value= "submitted"/>
</form>
</body>

The verifycode.php file code is as follows

The code is as follows Copy Code

<?php
/*
Picture verification code powered by Kason Test Http://www.111cn.net * *
Session_Start ();
Number of $num =4;//verification code
$width =80;//Verification Code width
$height =20;//Authentication Code Height
$code = ';
For ($i =0, $i < $num; $i + +)//generate the 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;//Uppercase letters
Case 2: $code [$i]=CHR (rand (97,122)); break;//lowercase Letters
}
}
$_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 characters to image
{
$char _color=imagecolorallocate ($image, Rand (0,2555), Rand (0,255), Rand (0,255));
Imagechar ($image, ($width/$num) * $i, Rand (0,5), $code [$i], $char _color);
}
Header ("Content-type:image/png");
Imagepng ($image);//output image to browser
Imagedestroy ($image);/Free Resources
?>

checkcode.php files are as follows

  code is as follows copy code

<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<?php
Ini_set (' display_errors ', ' off ');
Session_Start ();
  if ( Strtoupper ($_post["code"]) = = = Strtoupper (($_session["Verifycode"))) {
 print ("Verify code correct,");
 }else{
    Print ("Validation code error,");
 }
  Echo submitted verification code: ". Strtoupper ($_post[" code "]).", Correct verification Code: ". Strtoupper ($_session[" Verifycode ");
?

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.