PHP Random Type Verification Code

Source: Internet
Author: User

The significance of development and use of verification code is to distinguish whether the operator is human or machine, to prevent the automatic script to the server catastrophic attack there are various types of verification code, such as: Static character verification Code, arithmetic verification code, drag-and-drop verification code, recognition text or identification of the item verification Code (advanced), The following is an introduction to the random switch arithmetic or character verification code one, using the function list

(1) Imagecreatetruecolor: Creating a canvas

(2) Imagecolorallocate: assigning colors to the canvas

(3) Imagefill: Canvas Fill Color

(4) Imagesetpixel: Set the interference point

(5) Imageline: Set interference line

(6) Imagestring: to draw a line of string horizontally

(7) Imagepng: Exporting images to a browser or file in PNG format

(8) Imagedestroy: Destroying pictures

Second, the realization process

Create canvas, background color, fill background colors, set interference points, setting noise lines, and add a string to the canvas, set the header to the browser output picture--output picture to browser--destroy picture

Third, the implementation of code

    Define ("WIDTH", 200);    Define ("HEIGHT", 50);         1. Create Canvas $image = Imagecreatetruecolor (WIDTH, HEIGHT);         2. Define the background color for the canvas $bgcolor = imagecolorallocate ($image, 255, 255, 255);         3. Fill background color Imagefill ($image, 0, 0, $bgcolor); 4. Set the interference point for ($i = 0; $i < $i + +) {$pointcolor = Imagecolorallocate ($image, Mt_rand (), Mt_rand (5        0, $), Mt_rand (50, 200));    Imagesetpixel ($image, Mt_rand (0, WIDTH), Mt_rand (0, HEIGHT), $pointcolor); }//5. Set the interference line for ($i = 0; $i < 3; $i + +) {$linecolor = Imagecolorallocate ($image, Mt_rand (), MT        _rand (a), Mt_rand (50, 200));    Imageline ($image, Mt_rand (0, width), mt_rand (0, height), Mt_rand (0, WIDTH), Mt_rand (0, height), $linecolor);    } $type =mt_rand (0,1);        if ($type ==0) {$num 1=mt_rand (0, 99);        $num 2=mt_rand (0, 99);               $cal = "+-"; $captcha = $num 1. $cal [Mt_rand (0, strlen ($cal)-1)]. $num 2. '        = ';   $captchaLen =strlen ($captcha);     $distance =width/2.0/$captchaLen; for ($i = 0; $i < $captchaLen; $i + +) {//font color $fontcolor = Imagecolorallocate ($image, Mt_ra            nd (0, +), Mt_rand (0, +), Mt_rand (0, 120));            Set font Content $fontcontent = substr ($captcha, $i, 1);            Sets the xy coordinate of the character $x = width/2.0-$distance * $captchaLen/2+ $i * $distance;            $y = Mt_rand (5, 30);        6. Fill the string into the canvas imagestring ($image, $x, $y, $fontcontent, $fontcolor);        }}else{$captcha = ' 2345678abcdefhijkmnpqrstuvwxyzABCDEFGHJKLMNPQRTUVWXY ';        $captchaLen = 4;        $distance =width/2.0/$captchaLen; for ($i = 0; $i < $captchaLen; $i + +) {//font color $fontcolor = Imagecolorallocate ($image, Mt_ra            nd (0, +), Mt_rand (0, +), Mt_rand (0, 120));            Set font Content $fontcontent = $captcha [Mt_rand (0, strlen ($captcha)-1)]; Sets the xy coordinate of a character $x = width/2.0-$distance * $captchaLen/2+$i * $distance;            $y = Mt_rand (5, 30);        6. Fill the string into the canvas imagestring ($image, $x, $y, $fontcontent, $fontcolor);         }}//7. Output image header information to the browser header (' Content-type:image/png ');         8. Output image to Browser imagepng ($image); 9. Destruction of picture Imagedestroy ($image);

  

PHP Random Type Verification Code

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.