PHP generates picture verification code

Source: Internet
Author: User
Tags php file rand

Verification code is very important in Web application, which is usually used to prevent users from submitting forms maliciously, such as malicious registration and login, forum malicious irrigation, etc. This article will illustrate the use of PHP to generate common authentication code

Let's look at the general effect.

So let's just post the code right next.

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 <?php $image = imagecreatetruecolor (100, 30); Create Canvas $imagecolor = Imagecolorallocate ($image, 255, 255, 255); Background color Imagefill ($image, 0, 0, $imagecolor); Fill background color for ($i =0 $i <4; $i + +) {//loop 4 digits $fontsize = 6; $fontcolor = Imagecolorallocate ($image, rand (0), rand (0, 200) , rand (0, 200)); $fontcontent = rand (0, 9); $x = $i *100/4 + rand (5, 15); $y = rand (5, 10); Imagestring ($image, $fontsize, $x, $y, $fontcontent, $fontcolor); For ($i =0 $i <200; $i + +) {//Loop add interference point $pointcolor = Imagecolorallocate ($image, Rand, Rand), Rand (50, 20 0)); $x = rand (1, 99); $y = rand (1, 29); Imagesetpixel ($image, $x, $y, $pointcolor); For ($i =0 $i <3; $i + +) {//Loop add interference line $linecolor = Imagecolorallocate ($image, Rand (), Rand (M), rand (100, 250 )); $x 1 = rand (1, 25); $x 2 = rand (50, 75); $y 1 = rand (1, 15); $y 2 = rand (15, 25); Imageline ($image, $x 1, $y 1, $x 2, $y 2, $linecolor); Header ("Content-type:image/png"); Imagepng ($image); Imagedestroy ($image);?>

We'll share one more. Can generate Chinese verification code

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 <?php//1.qi enables the GD Library GD Library to provide a series of APIs for working with pictures, using the GD library to work with pictures, or to generate pictures. The GD library on the site is typically used to generate thumbnails or to add watermarks to images or to generate reports on Web site data. Session_Start ();  //converting GBK encoded strings into UTF-8 strings, the first argument is written GBK because this php file is stored in the host code is GBK coding//UTF-8 Coding Browser general support, versatility, here is converted to UTF-8 $str = Iconv ("GBK", "Utf-8", "all mortal beings green mountains and monuments open Heart will be clouds unroll happiness will always accompany you"); if (!is_string ($str) | | |!mb_check_encoding ($STR, "Utf-8")) {exit ("not a string or not Utf-8"); $zhongwenku _size; Gets the length of the string by UTF-8 encoding $zhongwenku _size = Mb_strlen ($str, "UTF-8");  //Import the above characters into the array $zhongwenku = Array (); For ($i =0 $i < $zhongwenku _size $i + +) {$zhongwenku [$i] = Mb_substr ($str, $i, 1, "UTF-8");}   $result = "";  //Picture four characters to write for ($i =0 $i <4; $i + +) {switch (rand (0, 1)) {case 0: $result. = $zhongwenku [rand (0, $zhongwenku _si ZE-1)]; Break Case 1: $result. =dechex (rand (0,15)); Break }  }   $_session["check" = $result;  //Create a true Color picture width 100, high $img = Imagecreatetruecolor (100, 30);  //distribution background color $BG = imagecolorallocate ($img, 0, 0, 0);  //Assign text color $te = ImagecOlorallocate ($img, 255,255,255);  //write String on the picture//imagestring ($img, Rand (3,8), Rand (1,70), Rand (1,10), $result, $te);  //A special font can be written on the image according to the load font imagettftext ($img, Rand (2, 9), $te, "Msyh." TTF ", $result);   $_session["Check" = $result;   for ($i =0 $i <3; $i + +) {//$t = Imagecolorallocate ($img, rand (0, 255), rand (0, 255), rand (0, 255));//Draw line Imagelin E ($img, 0, rand (0), rand (70,100), rand (0), $te);   $t = Imagecolorallocate ($img, rand (0, 255), rand (0, 255), rand (0, 255)); Add noise for the picture for ($i =0 $i <200; $i + +) {Imagesetpixel ($img, rand (1), rand (1,), $t);//Send HTTP header information Specifies that this time is sent in image JPEG header ("Content-type:image/jpeg"); Output JPEG image to browser imagejpeg ($IMG);  ?>

Let's take one more example.

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61-62 <?php   Session_Start (); function Random ($len) {$srcstr = "1a2s3d4f5g6hj8k9qwertyupzxcvbnm"; Mt_srand (); $strs = ""; for ($i = 0; $i < $len; $i + +) {$strs. = $srcstr [Mt_rand (0)];} return $strs;  //Randomly generated string $str = random (4);  //Verify code picture width $width = 50;  //Verification code Picture Height $height = 25;  //Declare the picture format of the layer you want to create @ header ("Content-type:image/png");  //Create a layer $im = Imagecreate ($width, $height);  //Background color $back = Imagecolorallocate ($im, 0xFF, 0xFF, 0xFF);  //Blur point Color $pix = Imagecolorallocate ($im, 187, 230, 247);  //FONT Color $font = imagecolorallocate ($im, 41, 163, 238);  //Draw the Blur Action Point Mt_srand (); for ($i = 0; $i < 1000 $i + +) {Imagesetpixel ($im, Mt_rand (0, $width), Mt_rand (0, $height), $pix),  //output characters imag Estring ($im, 5, 7, 5, $STR, $font);  //Output Rectangle Imagerectangle ($im, 0, 0, $width-1, $height-1, $font);  //Output picture imagepng ($im);   Imagedestroy ($im);   $STR = MD5 ($STR);  //Select Cookie//setcookie ("Verification", $str, time () + 7200, "/");  //Select session $_session["Verification"] = $str;?>

Next, just call on the page:

?

1

If you want to achieve "can't see clearly?" for one more effect, add the following JS to the page

?

1 2 3 function changing () {document.getElementById (' checkpic '). src= "/images/checkcode.php?" +math.random (); }

The above mentioned is the entire content of this article, I hope you can enjoy.

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.