A comprehensive analysis of the implementation of PHP Verification code with PHP Verification code small case _php Tips

Source: Internet
Author: User
Tags rand strlen

Expand

We need to open the GD expansion, you can use the following code to see whether to open the GD expansion.

<?php

echo "Hello world!!!!";

Echo Phpinfo ();
? >

Then in the browser ctrl+f find the GD option to verify that they have not installed this expansion, if not, you need to fully install this expansion.

Background image

Imagecreatetruecolor

Default Generation black Background

<?php
//Using GD Imagecreatetruecolor (); Create a background image
$image = Imagecreatetruecolor (100,30);
In the display of this picture must first declare header information header
(' Content-type:image/png ');

Imagepng ($image);

Releasing resources, destroying the execution object
Imagedestroy ($image);

Imagecolorallocate

Creates a fill color and attaches it using the Imagefill (Image,x,y,color) method.

<?php
//Using GD Imagecreatetruecolor (); Create a background image
$image = Imagecreatetruecolor (100,30);

Generate Fill Color
$bgcolor = imagecolorallocate ($image, 255,255,255);
Fills the fill color onto the background image
Imagefill ($image, 0,0, $bgcolor);
In the display of this picture must first declare header information header
(' Content-type:image/png ');

Imagepng ($image);

Releasing resources, destroying the execution object
Imagedestroy ($image);

Imagepng

Before using this method, be sure to set the header information, otherwise it will not display the normal picture

Imagedestory (image)

Timely release of resources can reduce the pressure on server requests.

Simple Digital Verification Code

Imagecolorallocate

Generate color information to facilitate the disposal of the pending.

$fontcolor =imagecolorallocate ($image, Rand (0,255), Rand (0,255), Rand (0,255));

Imagestring

Write the content information to the corresponding position in the picture.

Imagestring ($image, $fontsize, $x, $y, $fontcontent, $fontcolor);

Increased identification interference

Add dots

//Generate some interference points, here are 200 for
($i =0; $i <200; $i + +) {
  $pointcolor = imagecolorallocate ($image, Rand ( 50,255), Rand (50,255), Rand (50,255));
  Imagesetpixel ($image, Rand (0,100), Rand (0,30), $pointcolor);
}

Add line

//Generate some interference lines here are 5
for ($i =0; $i <5; $i + +) {
  //set to light-colored line to prevent distracting
  $linecolor = imagecolorallocate ($image, Rand (50,255), Rand (50,255), Rand (50,255));
  Imageline ($image, Rand (0,99), Rand (0,29), Rand (0,99), Rand (0,29), $linecolor);



Digital Letter Mixed Verification Code

<?php//Using GD Imagecreatetruecolor (); Create a background image $image = Imagecreatetruecolor (100,40);
Generate Fill Color $bgcolor = imagecolorallocate ($image, 255,255,255);

Fills the fill color onto the background image Imagefill ($image, 0,0, $bgcolor);
  Generates a random 4-bit alphanumeric and digitally mixed captcha for ($i =0 $i <4; $i + +) {$fontsize = rand (6,8);
  $fontcolor = Imagecolorallocate ($image, Rand (0,255), Rand (0,255), Rand (0,255));
  In order to avoid the user difficult to identify, removed some ambiguous letters and numbers $rawstr = ' abcdefghjkmnopqrstuvwxyz23456789abcdefghjklmnopqrstuvwxyz ';
  $fontcontent = substr ($rawstr, Rand (0,strlen ($RAWSTR)), 1);
  Avoid the resulting picture overlap $x + 20;
  $y = rand (10,20);  
Imagestring ($image, $fontsize, $x, $y, $fontcontent, $fontcolor); //Generate some disturbing points, here are 200 for ($i =0 $i <200; $i + +) {$pointcolor = Imagecolorallocate ($image, Rand (50,255), Rand (50,255),
  Rand (50,255));
Imagesetpixel ($image, Rand (0,100), Rand (0,30), $pointcolor); //Generate some noise lines here are 4 for ($i =0 $i <4; $i + +) {//set to light-colored line to prevent distracting $linecolor = Imagecolorallocate ($image, Rand (50,255), Rand (
  50,255), Rand (50,255)); Imageline ($image, Rand (0,99), Rand(0,29), Rand (0,99), Rand (0,29), $linecolor);

Header (' content-type:image/png ');

Imagepng ($image);

 Releasing resources, destroying the execution object Imagedestroy ($image);

Using verification Code

Time to open the session

Note: open session must be at the beginning of the place

The principle of verification

The validation process is the validation code entered by the client and the validation code that exists in the session domain. That

if (Isset ($_request[' Checkcode ')) {
    session_start ();
    if ($_request[' Checkcode ']==$_session[' Checkcode ']) {
      echo "<font color= ' green ' >Success!</font>"; 
    } else{
      echo "<font color= ' red ' >Failed!</font>";  
    }
    Exit ();
  }

Tuning validation

But the simple way to verify that there is a bit bad is that the case of letters is error-prone. So we're going to do a transformation that converts all the values entered by the user into lowercase.

if (Strtolower ($_request[' Checkcode ')) ==$_session[' Checkcode ']) {...}

Small case

Generate Verification Code

<?php session_start ()//must be declared at the very beginning of PHP to open session//Use GD Imagecreatetruecolor (); Create a background image $image =

Imagecreatetruecolor (100,40);
Generate Fill Color $bgcolor = imagecolorallocate ($image, 255,255,255);

Fills the fill color onto the background image Imagefill ($image, 0,0, $bgcolor);
Generates a random 4-digit letter and a digital hybrid verification code $checkcode = ';
  For ($i =0 $i <4; $i + +) {$fontsize = rand (6,8);
  $fontcolor = Imagecolorallocate ($image, Rand (0,255), Rand (0,255), Rand (0,255));
  In order to avoid the user difficult to identify, removed some ambiguous letters and numbers $rawstr = ' abcdefghjkmnopqrstuvwxyz23456789 ';
  $fontcontent = substr ($rawstr, Rand (0,strlen ($RAWSTR)), 1);
  Stitching is about to be born in the verification code $checkcode. = $fontcontent;
  Avoid the resulting picture overlap $x + 20;
  $y = rand (10,20);  
Imagestring ($image, $fontsize, $x, $y, $fontcontent, $fontcolor);

}//Save to Session variable $_session[' Checkcode ']= $checkcode; Generates some disturbing points, here are 200 for ($i =0 $i <200; $i + +) {$pointcolor = Imagecolorallocate ($image, Rand (50,255), Rand (50,255),
  Rand (50,255));
Imagesetpixel ($image, Rand (0,100), Rand (0,30), $pointcolor); //Generate some noise lines here are 4 for ($i =0; $i <4; $i + +) {//set to light-colored line to prevent distracting $linecolor = Imagecolorallocate ($image, Rand (50,255), Rand (50,255), Rand (50,255));

Imageline ($image, Rand (0,99), Rand (0,29), Rand (0,99), Rand (0,29), $linecolor);

Header (' content-type:image/png ');

Imagepng ($image);

 Releasing resources, destroying the execution object Imagedestroy ($image);

Form validation

<?php header ("Content-type:text/html;charset=utf8");
      if (Isset ($_request[' Checkcode ')) {session_start (); if (Strtolower ($_request[' Checkcode ')) ==$_session[' Checkcode '] {echo <font color= ' green ' >success!</font 
      > ";  
      }else{echo "<font color= ' red ' >Failed!</font>";
    } exit (); }?> <! DOCTYPE html>  

Summarize

Finally, let's summarize.
• Use PHP to make verification code requires the support of GD expansion.
• Use the Imagecreatetruecolor method to generate a background color and fill a color generated by imagecolorallocate with Imagefill.
• Use imagestring to combine validation code and background graphics
• Use Imagesetpixel to add interference points
• Use Imageline to add interference lines
• Open the Session_Start () method at the beginning before using session
• Use JavaScript to dynamically modify the validation code SRC to meet the user's need for a "change".

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.