Home > Article > Backend Development > Generate image verification code with php_PHP tutorial
Verification code is very important in WEB applications. It is usually used to prevent users from maliciously submitting forms, such as malicious registration and login, malicious forum flooding, etc. This article will explain how to use PHP to generate common verification codes through examples
Let’s take a look at the rough effect first
Then let’s just post the code next
?
|
<🎜>$image = imagecreatetruecolor(100, 30); //Create canvas<🎜> <🎜>$imagecolor = imagecolorallocate($image, 255, 255, 255); //Background color<🎜> <🎜>imagefill($image, 0, 0, $imagecolor); //Fill the background color<🎜> <🎜>for($i=0;$i<4;$i ){ //Loop 4 digits<🎜> <🎜>$fontsize = 6;<🎜> <🎜>$fontcolor = imagecolorallocate($image, rand(0, 200), 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 to add interference points<🎜> <🎜>$pointcolor = imagecolorallocate($image, rand(50, 200), rand(50, 200), rand(50, 200));<🎜> <🎜>$x = rand(1, 99);<🎜> <🎜>$y = rand(1, 29);<🎜> <🎜>imagesetpixel($image, $x, $y, $pointcolor);<🎜> <🎜>}<🎜> <🎜>for($i=0;$i<3;$i ){ //Loop to add interference lines<🎜> <🎜>$linecolor = imagecolorallocate($image, rand(100, 250), rand(100, 250), rand(100, 250));<🎜> <🎜>$x1 = rand(1, 25);<🎜> <🎜>$x2 = rand(50, 75);<🎜> <🎜>$y1 = rand(1, 15);<🎜> <🎜>$y2 = rand(15, 25);<🎜> <🎜>imageline($image, $x1, $y1, $x2, $y2, $linecolor);<🎜> <🎜>}<🎜> <🎜>header("content-type:image/png");<🎜> <🎜>imagepng($image);<🎜> <🎜>imagedestroy($image);<🎜> <🎜>?> |
Let me share with you another method that 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 43 44 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 |
//1.qi Enable the gd library. The GD library provides a series of APIs for processing images. You can use the GD library to process images or generate images. // On websites, the GD library is usually used to generate thumbnails or to add watermarks to images or to generate reports on website data. session_start();
// Convert the GBK encoded string into a UTF-8 string. The reason why the first parameter is written GBK is because the encoding of this php file stored in the host is GBK encoding // UTF-8 encoding is generally supported by browsers and has strong versatility. Let’s convert it to UTF-8 here $str = iconv("GBK", "utf-8", "All living things, green waters, green mountains, scenic spots and historic sites, open your mind and you will see clouds and clouds, and happiness will always be with you"); if(!is_string($str) || !mb_check_encoding($str,"utf-8")) { exit("Not a string or not utf-8"); } $zhongwenku_size; // Get the length of the string in 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 = "";
//Four characters to be written on the picture for($i=0; $i<4; $i ) { switch (rand(0, 1)) { case 0: $result.=$zhongwenku[rand(0, $zhongwenku_size-1)]; break; case 1: $result.=dechex(rand(0,15)); break; }
}
$_SESSION["check"] = $result;
// Create a true color picture with a width of 100 and a height of 30 $img = imagecreatetruecolor(100, 30);
//Assign background color $bg = imagecolorallocate($img, 0, 0, 0);
//Assign text color $te = imagecolorallocate($img, 255,255,255);
//Write string on the image //imagestring($img, rand(3,8), rand(1,70), rand(1,10), $result, $te);
//You can write special fonts on the picture according to the loaded font imagettftext($img, 13, rand(2, 9), 20 ,20, $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 lines imageline($img, 0, rand(0, 20), rand(70,100), rand(0, 20), $te); }
$t = imagecolorallocate($img, rand(0, 255),rand(0, 255),rand(0, 255)); // Add noise to the image for($i=0; $i<200; $i ) { imagesetpixel($img, rand(1, 100), rand(1, 30), $t); } //Send http header information. Specify that the jpeg in the image is sent this time header("Content-type: image/jpeg"); // Output jpeg images to the browser imagejpeg($img);
?> |
Let’s give another example
?
2 3
|
<🎜> <🎜> <🎜>session_start();<🎜> <🎜>function random($len) {<🎜> <🎜>$srcstr = "1a2s3d4f5g6hj8k9qwertyupzxcvbnm";<🎜> <🎜>mt_srand();<🎜> <🎜>$strs = "";<🎜> <🎜>for ($i = 0; $i < $len; $i ) {<🎜> <🎜>$strs .= $srcstr[mt_rand(0, 30)];<🎜> <🎜>}<🎜> <🎜>return $strs;<🎜> <🎜>}<🎜> <🎜> <🎜> <🎜>//Randomly generated string<🎜> <🎜>$str = random(4);<🎜> <🎜> <🎜> <🎜>//The width of the verification code image<🎜> <🎜>$width = 50;<🎜> <🎜> <🎜> <🎜>//The height of the verification code image<🎜> <🎜>$height = 25;<🎜> <🎜> <🎜> <🎜>//Declare the image format of the layer to be created<🎜> <🎜>@ header("Content-Type:image/png");<🎜> <🎜> <🎜> <🎜>//Create a layer<🎜> <🎜>$im = imagecreate($width, $height);<🎜> <🎜> <🎜> <🎜>//Background color<🎜> <🎜>$back = imagecolorallocate($im, 0xFF, 0xFF, 0xFF);<🎜> <🎜> <🎜> <🎜>//Blurred point color<🎜> <🎜>$pix = imagecolorallocate($im, 187, 230, 247);<🎜> <🎜> <🎜> <🎜>//Font color<🎜> <🎜>$font = imagecolorallocate($im, 41, 163, 238);<🎜> <🎜> <🎜> <🎜>//Draw the blur effect point<🎜> <🎜>mt_srand();<🎜> <🎜>for ($i = 0; $i < 1000; $i ) {<🎜> <🎜>imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $pix);<🎜> <🎜>}<🎜> <🎜> <🎜> <🎜>//Output characters<🎜> <🎜>imagestring($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 cookies<🎜> <🎜>//SetCookie("verification", $str, time() 7200, "/");<🎜> <🎜> <🎜> <🎜>//Select Session<🎜> <🎜>$_SESSION["verification"] = $str;<🎜> <🎜>?> |
1 |
If you want to achieve the "Can't see clearly? Change another" effect, add the following JS to the page
?
2 3
|