Almost all of the Web site login page will have a verification code, authentication code is a security protection mechanism, in the registration requirements must have manual operation to verify, to prevent the garbage register a large number of registered user accounts to occupy the server memory so that the server paralyzed.
The implementation of the picture verification code is very simple. First, a fixed number of characters are randomly selected from the specified character set, and an irregular method is used to draw the canvas, add some interference points and interference elements, and finally output the picture, a brand-new verification code is completed.
First show you the generated verification code:
Click Refresh:
If you are very satisfied with the results, please continue to look down.
The front end code is as follows:
PHP script file Verification code is as follows:
<?php session_start (); Turn on the session record authentication code data Vcode (4, 15);//Set the number of characters for the validation code and picture base width//vcode characters, font size, picture width, height function vcode ($num = 4, $size = $width = 0, $height = 0) {! $width && $width = $num * $size * 4/5 +;! $height && $height = $size + 10;//Set up inspection
Code Set $STR = "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVW";
Save the acquired authentication code $code = '; Randomly select character for ($i = 0; $i < $num $i + +) {$code. = $str [Mt_rand (0, strlen ($str)-1)];}//Create verification code Canvas $im = Imagecreatetrueco
Lor ($width, $height);
Background color $back _color = imagecolorallocate ($im, Mt_rand (0,100), Mt_rand (0,100), Mt_rand (0,100));
The text color $text _color = imagecolorallocate ($im, Mt_rand (255), Mt_rand (100, 255), Mt_rand (255));
Imagefilledrectangle ($im, 0, 0, $width, $height, $back _color); Draw the jamming line for ($i = 0; $i < 5; $i + +) {$font _color = imagecolorallocate ($im, Mt_rand (0, 255), Mt_rand (0, 255), Mt_rand (0, 25
5)); Imagearc ($im, Mt_rand (-$width, $width), Mt_rand (-$height, $height), Mt_rand ($width * 2), Mt_rand (20, $height * 2), Mt_rand (0, 360), Mt_rand (0, 360), $font _color); ///Draw Interference point for ($i = 0; $i < $i + +) {$font _color = imagecolorallocate ($im, Mt_rand (0, 255), Mt_rand (0, 255), Mt_rand (0,
255));
Imagesetpixel ($im, Mt_rand (0, $width), Mt_rand (0, $height), $font _color);
///random rotation angle array $array =array (5,4,3,2,1,0,-1,-2,-3,-4,-5); Output verification Code//Imagefttext (image, size, angle, x, y, color, fontfile, text) @imagefttext ($im, $size, Array_rand ($array), 12,
$size + 6, $text _color, ' C:\WINDOWS\FONTS\SIMSUN.TTC ', $code);
$_session["Verifycode"]= $code; No-cache access to the server on each request//max-age request again to access the server after 1s requests, must-revalidate the first send request accesses the server, and then no longer accesses the server/header ("
Cache-control:max-age=1, s-maxage=1, No-cache, must-revalidate ");
Header ("Cache-control:no-cache");
Header ("content-type:image/png;charset=gb2312");
Converts the picture to PNG format imagepng ($IM);
Imagedestroy ($im); }?>
OK, about the small series of PHP to introduce you to draw the picture verification to introduce so many people, I hope to help you!