The example of this article describes the implementation of PHP digital verification Code and digital operation verification code. Share to everyone for your reference. Specifically as follows:
1. Digital Verification Code:
<?php//The first instance is the digital captcha, the most common authentication code.
How many numbers can be decided by yourself.
$num is the generated verification code containing several digital getvalidate (4,60,20);
function Getvalidate ($num, $w, $h) {$code = "";
For ($i =0 $i < $num; $i + +) {$code. = rand (0,9); The//code variable is finally $num a number and is a string.
Inherited if it is to generate a four-bit digital authentication code, you can use Rand (1000,9999) to generate Header ("content-type:image/png");
$img = Imagecreate ($w, $h);//Create a picture, the long width parameter is a beginning to define good. $black = Imagecolorallocate ($img, 0,0,0);//define Black $gray = Imagecolorallocate ($img, 200,200,200);//define Grey $bgcolor = Imagecol
Orallocate ($img, 255,255,255);//Background color white Imagefill ($img, 0,0, $gray);
Imagefill ($image, ing $x, int $y, int $color), in the image image coordinates x,y with color color to perform area fill/to the verification code to draw a border, the sense of verification code Meimei, in fact, draw a border is to draw him a rectangle
Imagerectangle ($img, 0,0, $w-1, $h-1, $black)/////////*imagestring ($image, $font, $x, $y, $s, $col) with black frame Use the col color to draw the string s to the X,y (XY is the upper-left coordinate of the string) represented by image the upper-left corner of the entire image is (0,0) if font is 1,2,3,4,5, then use the built-in font *//one is to draw a four digit method, so that four character positions are random and so on $
Strx = rand (5,10);
for ($i = 0; $i < $num; $i + +) {$strops = rand (1,6); Imagestring ($img, 5, $strx, $strops, substr ($code, $i, 1), $Black);
$strx + + rand (8,12); The Strx is the horizontal axis, which needs to be added each time before the coordinates are guaranteed to not overlap. Ordinate no matter if the//SUBSTR () function returns a portion of the string, substr (String,start,length), the string to intercept, the start position, the length to intercept//generate a number, the following to give the verification code region some interference, Prevents some tools from automatically identifying//1. Method One: Add a lot of noise to the background//imagesetpixel ($image, $x, $y, $color), draw a point for ($) in the image image using color in the x,y coordinates (the upper-left corner of the image is 0,0). i = 0 $i <80; $i + +) {Imagesetpixel ($img, rand (0, $w), rand (0, $h), $black);//These points are distributed in this context}//2. Method Two: Draw a few dashed lines $style
= Array ($black, $gray);
Imagesetstyle ($img, $style);
Imagesetstyle ($image, $style), set the style of the drawing line, the pixel-composed array $y 1 = rand (0, $h);
$y 2 = rand (0, $h);
$y 3 = rand (0, $h);
$y 4 = rand (0, $h);
Imageline ($img, 0, $y 1, $w, $y 2,img_color_styled);
Imageline ($img, 0, $y 3, $w, $y 4,img_color_styled);
Imagepng ($IMG);
Imagedestroy ($IMG);
}?>
2. Digital Operation Verification Code:
<?php getvalidate (100,30);
function Getvalidate ($w, $h) {$img = Imagecreate ($w, $h);
$gray = Imagecolorallocate ($img, 255,255,255);
$black = Imagecolorallocate ($img, Rand (0,200), Rand (0,200), Rand (0,200));
$red = Imagecolorallocate ($img, 255, 0, 0);
$white = Imagecolorallocate ($img, 255, 255, 255);
$green = imagecolorallocate ($img, 0, 255, 0);
$blue = imagecolorallocate ($img, 0, 0, 255);
Imagefilledrectangle ($img, 0, 0, $black);
for ($i = 0; $i < $i + +) {Imagesetpixel ($img, rand (0, $w), rand (0, $h), $gray);
$num 1 = rand (1,99);
$num 2 = rand (1,99);
Imagestring ($img, 5, 5, rand (1,10), $num 1, $red);
Imagestring ($img, 5,30,rand (1,10), Getrand (), $white);
Imagestring ($img, 5,45,rand (1,10), $num 2, $green);
Imagestring ($img, 5,65,rand (1,10), "=", $blue);
Imagestring ($img, 5,80,rand (1,10), "?", $red);
Header ("Content-type:image/png");
Imagepng ($IMG);
Imagedestroy ($IMG);
function Getrand () {$code = rand (0,1);
Switch ($code) {case 0:return "+";
Break
Case 1:return "-";
Break
Default: # code ... break;
}}?>
I hope this article will help you with your PHP programming.