Phprequire_once' String.func.php ';//Verify code via GD library/** * Add validation text * @param int $type * @param int $length*/functionBuildrandomstring ($type=1,$length=4){ $row=''; if($type==1){ $row=Join('',Range(0, 9)); }Elseif($type==2){ $row=Join('',Array_merge(Range(' A ', ' Z '),Range(' A ', ' Z '))); }Elseif($type==3){ $row=Join('',Array_merge(Range(' A ', ' Z '),Range(' A ', ' Z '),Range(0, 9))); }; $row=Str_shuffle($row); $row=substr($row, 0,$length); return$row;}/** * Generate thumbnails * @param int $type//contain numbers or English * @param int $length How many characters * @param int $pixel Interference dot density * @param int $dst _h interference The density of the line * @param the name of the string//verification code in $_session*/functionVerifyimage ($type=1,$length=4,$pixel=0,$line=0,$sess _name= "Verify"){ //session_start (); Create a canvas$width= 100; $height= 40; $image= Imagecreatetruecolor ($width,$height ); $white= Imagecolorallocate ($image, 255, 255, 255 ); $black= Imagecolorallocate ($image, 0, 0, 0 ); //fill the canvas with filled rectanglesImagefilledrectangle ($image, 1, 1,$width-2,$height-2,$white ); $chars= Buildrandomstring ($type,$length ); $_session[$sess _name] =$chars; //$fontfiles = Array ("Msyh. TTF "," MSYHBD. TTF "," Simli. TTF "," SimSun. TTC "," simyou. TTF "," stzhongs. TTF ");$fontfiles=Array("Simkai. TTF " ); //because the font file is larger, just keep a font, if the students need to add their own fonts, fonts in your computer's Fonts folder, directly run the input fonts will be able to see the corresponding font for($i= 0;$i<$length;$i++) { $size=Mt_rand(14, 18 ); $angle=Mt_rand(-15, 15 ); $x= 5 +$i*$size; $y=Mt_rand(20, 26 ); $fontfile= ".. /fonts/".$fontfiles[Mt_rand(0,Count($fontfiles)-1 )]; $color= Imagecolorallocate ($image,Mt_rand(50, 90),Mt_rand(80, 200),Mt_rand(90, 180 ) ); $text=substr($chars,$i, 1 ); Imagettftext ($image,$size,$angle,$x,$y,$color,$fontfile,$text ); } if($pixel) { for($i= 0;$i< 50;$i++) {Imagesetpixel ($image,Mt_rand(0,$width-1),Mt_rand(0,$height-1),$black ); } } if($line) { for($i= 1;$i<$line;$i++) { $color= Imagecolorallocate ($image,Mt_rand(50, 90),Mt_rand(80, 200),Mt_rand(90, 180 ) ); Imageline ($image,Mt_rand(0,$width-1),Mt_rand(0,$height-1),Mt_rand(0,$width-1),Mt_rand(0,$height-1),$color ); } } Header("Content-type:image/gif" ); Imagegif ($image ); Imagedestroy ($image );}
Main points: 1, if there is no declaration session_start (); 2, the font can be downloaded in the CMF input fonts to their own defined Fonts folder; 3,$_session [$sess _name ] can be compared by $_post to obtain the user input verification code.
The above is the introduction of the PHP full verification code, including PHP, verification code content, I hope the PHP tutorial interested in a friend helpful.