PHP Verification Code generation code _php Tips

Source: Internet
Author: User

Verification code is usually used to secure our site registration or login is not injected, but in order to be more secure we usually generate some mixed authentication code, the next one to see examples.

In our development login module or the forum of the irrigation module, in order to prevent malicious submission, need to use the verification Code, verification code is used to distinguish between people and machines a means, of course, this means is not foolproof, but will always play some role.

Verification code to achieve the requirements of the GD library support, did not open the GD library of children's shoes need to open the GD library, in fact, the production and use of verification code is very simple, just need 4 steps can be done: Create a verification code base map, display the contents of the verification code, increase the interference elements, output verification code, below we take steps to split:

The first step: Create a verification code base diagram

$image = Imagecreatetruecolor (100, 30); Create a bottom with a width of 100 high to 30 the background color of the base figure is black and is a system-defined 
$bgcolor = imagecolorallocate ($image, 255, 255, 255);  Assign a white background color to the bottom chart created above 
imagefill ($image, 0, 0, $bgcolor);//fill white background color 

Step Two: Display the contents of the authentication code

Output verification Code content for 
($i =0 $i < 4; $i + +) { 
  $fontsize = 6; 
  $fontcolor = Imagecolorallocate ($image, Rand (0,120), Rand (0,120), Rand (0,120)); 
  $data = ' qwertyuipkjhgfdsazxcvbnm23456789 '; 
  $content = substr ($data, rand (0, strlen ($data)), 1); 
  $x = ($i *100/4) + rand (5,9);  
  $y = rand (5,10); 
  Imagestring ($image, $fontsize, $x, $y, $content, $fontcolor); Output a line of strings horizontally on an image 
 

Step three: Increase interference elements

Add interference point element for 
($i =0; $i < $i + +) { 
  $pointcolor = imagecolorallocate ($image, Rand (50,200), Rand (50,200), RA nd (50,200)); 
  Imagesetpixel ($image, Rand (0,99), Rand (0,29), $pointcolor); 
} Open source software: phpfensi.com 
//increase the interference line element  line and the color of the point must be controlled better than the color of the code number to avoid the phenomenon of verification code number is invisible for 
($i =0; $i < 4; $i + +) { 
  $ LineColor = Imagecolorallocate ($image, Rand (100,240), Rand (100,240), Rand (100,240)); 
  Imageline ($image, Rand (0,99), Rand (0,29), Rand (0,99), Rand (0,29), $linecolor); 
 

Fourth step: Output Verification Code

The output created by the image  must be output before the image output header information to specify the output of the image type 
header ("Content-type:image/png"); 
Imagepng ($image); 
Destroying images 

At this point, a simple verification code is achieved, about the implementation of the verification code note has been written in the comments, the use of verification code, we generally need to use the session to save to verify, here is not to give a detailed description.

The above is the PHP code to generate coding, in the important code has been commented, we can carefully study the content of the annotation, but also very important, I hope you can have some harvest it

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.