PHP implementation of digital verification code and digital operation verification Code, PHP digital Verification code Operation _php Tutorial

Source: Internet
Author: User

PHP implementation of digital verification code and digital operation verification Code, PHP digital Verification code operation


In this paper, the digital Verification Code and digital operation verification Code for PHP implementation are described. Share to everyone for your reference. Specific as follows:

1. Digital Verification Code:

<?php//The first instance is a digital verification code, the most common verification code. How many numbers can be decided by yourself.  $num is the generated verification code containing several numbers getvalidate (4,60,20), function getvalidate ($num, $w, $h) {$code = "";  for ($i =0; $i < $num; $i + +) {$code. = rand (0,9); The//code variable ends with a $num number and is a string.  Inherited if a four-bit digital verification code is generated, the Header ("Content-type:image/png") can be generated with Rand (1000,9999);  $img = Imagecreate ($w, $h);//Create a picture, the length-width parameter is defined at the beginning. $black = Imagecolorallocate ($img, 0,0,0);//define Black $gray = Imagecolorallocate ($img, 200,200,200);//define Gray $bgcolor =  Imagecolorallocate ($img, 255,255,255);//Background color white Imagefill ($img, 0,0, $gray);  Imagefill ($image, ing $x, int $y, int $color), performing area fills with a color color at the coordinates x, y of the image images//to the verification code to draw the border, feel the verification code Meimei, in fact, the border is to draw him a rectangle  Imagerectangle ($img, 0,0, $w-1, $h-1, $black);//Black box/*imagestring ($image, $font, $x, $y, $s, $col) Use the col color to draw the string s to the X, y of the image represented by image (XY is the upper-left coordinate of the string), and the upper-left corner of the entire image is (0,0) if the font is 1,2,3,4,5 use the built-in font *///once to draw four numbers, so that four character positions are randomly  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}//substr () function returns a part of the string, substr (String,start,length), the string to intercept, start position, length of the Intercept//generate a good number, the following to give some interference in the Captcha area, Prevents some tools from automatically identifying//1. Method One: Add a lot of noise to the background//imagesetpixel ($image, $x, $y, $color), draw a dot for the image image in the x, y coordinates (0,0 in the upper left corner) for ($ i = 0; $i <80; $i + +) {Imagesetpixel ($img, rand (0, $w), rand (0, $h), $black);//These points are distributed within this background}//2. Method Two: Draw a few dashed lines $style = a  Rray ($black, $gray);  Imagesetstyle ($img, $style);  Imagesetstyle ($image, $style), sets the style of the drawing line, the array of pixels $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. Numeric Operation Verification Code:

<?phpgetvalidate (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 is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/1040821.html www.bkjia.com true http://www.bkjia.com/PHPjc/1040821.html techarticle PHP Implementation of digital verification code and digital operation verification Code, PHP Digital Verification Code operation This paper describes the implementation of PHP digital verification Code and digital operation verification code. Share to everyone for your reference ...

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