Oneself in the past self-taught PHP painting verification code tutorial, now take this part of the notes to share with you, hoping to help Everyone.
incidentally, I will be in the back of the whole set of Css3,php,mysql development Notes pack to Baidu cloud, there is a need to go directly to the Baidu Cloud download, so that after you develop can directly turn the notes without Baidu search so Troublesome.
Note Link: Http://pan.baidu.com/s/1qYdQdKK password: pvj2
The following is mainly from the theory + practice code to explain, followed by code Examples.
first, the introduction of verification code
The CAPTCHA is an abbreviation for the automatic distinction between computer and human Turing Tests. is a public automatic program that distinguishes users from computers and People.
second, The Verification Code application scenario?
(all to distinguish between a person or a machine, shielding a machine request)
A) login, Registration To determine the pre-submission, man/machine check;
B) before the release, reply to the information, man/machine check;
C) when the suspected machine request, the Person/machine check;
..................
third, the core technical analysis of Verification Code service
Implementation Steps:
1. Generate basemaps;
2. Generate Verification content;
3. Generate the Verification code picture;
4. Verifying the contents of the verification;
Technical Points:
A) the implementation of the basemap, and the addition of interference elements;
Rely on PHP image processing library GD
Http://php.net/gd
B) Generate validation Content
Simple random number generation, using PHP function Mt_rand ();
Random number + letter generation requires the basis of ASCII code theory;
Random Chinese generation, need UTF-8 coding theory foundation;
C) Verify that the content is stored on the service side;
PHP Operation Session Basics Required
D) Verifying the Content
Requires a front-end Ajax foundation;
Precautions:
A) rely on GD extension
※B) before outputting the picture, the image header information must be output in advance;
C) The default output for this method is a black background
From the theoretical section, then follow the steps to write the following code step-by-step:
Example of verification code: ( Basic Verification Code )
| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 666768 |
<?php session_start(); // 先制作底图 $image= imagecreatetruecolor(100, 30); $bgcolor= imagecolorallocate($image, 255, 255, 255);//生成底片颜色,默认为黑色 imagefill($image, 0, 0, $bgcolor);//x,y轴上的位置/*// 在地图上显示随机数字 for($i=0;$i<4;$i++){ $fontsize=6; $fontcolor = imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120)); $fontcontent=rand(0,9);//数字0~9 // 关键的部分 (注意事项:控制好字体大小与分布,避免字体重叠或显示不全) $x=($i*100/4)+rand(5,10); //写在的坐标上 $y=rand(5,10); imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor); }*//* //数字和字母验证码 for($i=0;$i<4;$i++){ $fontsize=6; $fontcolor=imagecolorallocate($image, rand(0,120),rand(0,120), rand(0,120)); $data =‘abcdefghigkmnpqrstuvwxy3456789‘; $fontcontent=substr($data, rand(0,strlen($data)),1); $x=($i*100/4+rand(5,10)); $y=rand(5,10); imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor); }*/ $captch_code=""; //字母验证码 for($i=0;$i<4;$i++){ $fontsize=6; $fontcolor=imagecolorallocate($image, rand(0,120),rand(0,120), rand(0,120)); $data=‘abcdefghigkmnpqrstuvwxy‘; $fontcontent=substr($data, rand(0,strlen($data)),1); $captch_code.=$fontcontent; $x=($i*100/4+rand(5,10)); $y=rand(5,10); imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor); } $_SESSION[‘authcode‘]=$captch_code; // 添加点的干扰素 for($i=0;$i<200;$i++){ $pointcolor= imagecolorallocate($image, rand(50,200), rand(50,200), rand(50,200)); imagesetpixel($image, rand(1,99), rand(1,29), $pointcolor); } // 添加线干扰 for($i=0;$i<3;$i++){ $linecolor=imagecolorallocate($image, rand(80,220), rand(80,220),rand(80,220)); imageline($image, rand(1,99),rand(1,29), rand(1,99),rand(1,29),$linecolor); } header(‘content-type:image/png‘);//输出png的图片 imagepng($image);//生成图片 // 销毁图片 imagedestroy($image);?> |
After the validation code section of php, it is the code of the display page with the front end:
| 12345678910111213141516 |
<!DOCTYPEhtml><htmllang="en"><head> <metacharset="UTF-8"> <title>登录</title></head><body> <formaction="reg.php" method="post"> <imgid="captcha_img" src="verify.php?r=<?php echo rand();?>"> <ahref="javascript:void(0);" onclick="document.getElementById(‘captcha_img‘).src=‘verify.php?+Math.random()‘;" title="换一个?">看不清?</a><br> 验证码:<inputtype="text" name="authcode"> <inputtype="submit" > </form></body></html> |
The above is the number of the verification code, the following part of PHP is the image verification code of the page image verification code display :
| 123456789101112131415161718 |
<?php session_start(); // 图片的验证码 $table=array( "pic0"=>‘狗‘, "pic1"=>‘猫‘, "pic2"=>‘鱼‘, "pic3"=>‘鸟‘ ); $index=rand(0,3); $value=$table[‘pic‘.$index]; $_SESSION[‘authcode‘]=$value; $filename=dirname(__FILE__).‘\\pic‘.$index.‘.jpg‘;//需要自己准备好图片!! |
| 1 |
$contents=file_get_contents($filename); header(‘content-type:image/jpg‘); echo$contents; ?> |
The following is the display code for Chinese characters:
| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
汉字的验证码:<?php session_start(); // 先制作底图 $image= imagecreatetruecolor(200, 60); $bgcolor= imagecolorallocate($image, 255, 255, 255);//生成底片颜色,默认为黑色 imagefill($image, 0, 0, $bgcolor);//x,y轴上的位置 $fontface=‘msyh.ttf‘; $str="刘恒春美女帅哥看这里来好样的我知道了是"; $strdb=str_split($str,3);//每三个长算一个汉字 header("content-type:text/html;charset=‘utf8‘"); $captch_code=""; //中文验证码 for($i=0;$i<4;$i++){ $fontcolor=imagecolorallocate($image, rand(0,120),rand(0,120), rand(0,120)); $index=rand(0,count($strdb)); $cn=$strdb[$index]; $captch_code.=$cn; imagettftext($image, mt_rand(20,24), mt_rand(-60,60), (40*$i+20), mt_rand(30,35),$fontcolor,$fontface,$cn); } $_SESSION[‘authcode‘]=$captch_code; // 添加点的干扰素 for($i=0;$i<200;$i++){ $pointcolor= imagecolorallocate($image, rand(50,200), rand(50,200), rand(50,200)); imagesetpixel($image, rand(1,199), rand(1,59), $pointcolor); } // 添加线干扰 for($i=0;$i<3;$i++){ $linecolor=imagecolorallocate($image, rand(80,220), rand(80,220),rand(80,220)); imageline($image, rand(1,199),rand(1,59), rand(1,199),rand(1,59),$linecolor); } header(‘content-type:image/png‘);//输出png的图片 imagepng($image);//生成图片 // 销毁图片 imagedestroy($image); ?> |
All in all is the verification code code summary, if necessary can be in the Baidu cloud below me to download the corresponding Notes.
Note Link: Http://pan.baidu.com/s/1qYdQdKK password: pvj2
Tutorial on making PHP verification code