码迷,mamicode.com
首页 > Web开发 > 详细

PHP 生成验证码

时间:2014-11-07 12:54:51      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   io   color   ar   os   sp   for   

code.php:

 1 <?php 
 2     //验证码制作
 3     //文件头... 
 4     header("Content-type: image/png"); 
 5     //创建真彩色白纸 
 6     $im = @imagecreatetruecolor(50, 20) or die("建立图像失败"); 
 7     //获取背景颜色 
 8     $background_color = imagecolorallocate($im, 255, 255, 255); 
 9     //填充背景颜色(这个东西类似油桶) 
10     imagefill($im,0,0,$background_color); 
11     //获取边框颜色 
12     $border_color = imagecolorallocate($im,200,200,200); 
13     //画矩形,边框颜色200,200,200 
14     imagerectangle($im,0,0,49,19,$border_color); 
15 
16     //逐行炫耀背景,全屏用1或0 
17     for($i=2;$i<18;$i++){ 
18         //获取随机淡色         
19         $line_color = imagecolorallocate($im,rand(200,255),rand(200,255),rand(200,255)); 
20         //画线 
21         imageline($im,2,$i,47,$i,$line_color); 
22     } 
23 
24     //设置字体大小 
25     $font_size=12; 
26 
27     //设置印上去的文字 
28     $Str[0] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
29     $Str[1] = "abcdefghijklmnopqrstuvwxyz"; 
30     $Str[2] = "01234567891234567890123456"; 
31 
32     //获取第1个随机文字 
33     $imstr[0]["s"] = $Str[rand(0,2)][rand(0,25)]; 
34     $imstr[0]["x"] = rand(2,5); 
35     $imstr[0]["y"] = rand(1,4); 
36 
37     //获取第2个随机文字 
38     $imstr[1]["s"] = $Str[rand(0,2)][rand(0,25)]; 
39     $imstr[1]["x"] = $imstr[0]["x"]+$font_size-1+rand(0,1); 
40     $imstr[1]["y"] = rand(1,3); 
41 
42     //获取第3个随机文字 
43     $imstr[2]["s"] = $Str[rand(0,2)][rand(0,25)]; 
44     $imstr[2]["x"] = $imstr[1]["x"]+$font_size-1+rand(0,1); 
45     $imstr[2]["y"] = rand(1,4); 
46 
47     //获取第4个随机文字 
48     $imstr[3]["s"] = $Str[rand(0,2)][rand(0,25)]; 
49     $imstr[3]["x"] = $imstr[2]["x"]+$font_size-1+rand(0,1); 
50     $imstr[3]["y"] = rand(1,3); 
51 
52     //写入随机字串 
53     for($i=0;$i<4;$i++){ 
54         //获取随机较深颜色 
55         $text_color = imagecolorallocate($im,rand(50,180),rand(50,180),rand(50,180)); 
56         //画文字 
57         imagechar($im,$font_size,$imstr[$i]["x"],$imstr[$i]["y"],$imstr[$i]["s"],$text_color); 
58     } 
59 
60     //显示图片 
61     imagepng($im); 
62     //销毁图片 
63     imagedestroy($im); 
64 ?> 

调用的html:

 1 <html>
 2 <head>
 3 <title>图片验证实例</title>
 4 <style type="text/css">
 5 </style>
 6 </head> 
 7 <body>
 8 <form id="login" action="" method="post">
 9 <p>此例为验证实例</p>
10 <p>
11 <span>验证码:</span>
12 <input type="text" name="validate" value="" size=10> 
13 <img  title="点击刷新" src="code.php"></img>
14 </p>
15 <p>
16 <input type="submit">
17 </p>
18 </form>

 

PHP 生成验证码

标签:des   style   blog   io   color   ar   os   sp   for   

原文地址:http://www.cnblogs.com/yll-sww/p/4080984.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!