管理员
   
论坛积分
分
威望 点
贡献值 个
金币 枚
|
index.php
. D# ?4 X2 N! @( Z- <?php7 X# f: T6 W5 E! \( c5 [, L( Q4 `
- session_start();
. v, ?" O* z: C+ j. F( ` q% [6 ] - // 丢弃输出缓冲区的内容 **
, a, W/ y( g& i: \6 K) `/ ] - ob_clean();3 T. v x2 E( H1 s* e
- header ('Content-Type: image/png');
# D: }1 u% Y) l - // 1 创建画布
. R4 B) P, }1 B/ v& g; M; P - $image=imagecreatetruecolor(100, 30);
7 t% V, M1 x/ ^: Y Q: u! E - // 2 画一个矩形并填充背景颜色为白色
. Y, S# D/ J0 `- o3 M& u3 b - $color=imagecolorallocate($image, 255, 255, 255);
* V3 }! [$ Y$ u( b- E, [* j - imagefill($image, 20, 20, $color);1 o7 @+ m4 j' K1 W i
- // for($i=0;$i<4;$i++){
1 O8 u6 l% k: {* x - // $font=6;# o) h6 ^2 W" {6 @9 l3 _
- // $x=rand(5,10)+$i*100/4;- X s O1 g+ u1 ~4 y* y: ?2 f
- // $y=rand(8, 15);
2 Y( i. l1 i+ C7 \9 q - // $string=rand(0, 9);2 N, a9 ?) k9 n L% [! k: D
- // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));* ^+ u- M5 B7 u
- // imagestring($image, $font, $x, $y, $string, $color);, ?. Y5 [" J$ ~
- // }
9 Y* M2 R* h. c1 ]% F; z6 ^ - $code='';
( C( K" M$ b. f* m; Y1 {* _2 y* ~- E - for($i=0;$i<4;$i++){. L; j! |+ k9 R( \0 D4 v# X
- $fontSize=8;
* \# I |6 t3 I( s - $x=rand(5,10)+$i*100/4;7 \& m/ t- K( Q8 q5 c! J
- $y=rand(5, 15);
; a- u. E8 [: w q" e: @ - $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';# H7 W' _1 h1 {1 J$ P% e, `
- $string=substr($data,rand(0, strlen($data)),1);1 Z' ?3 j$ |& ?# w2 T. y4 B
- $code.=$string;
: U" g3 D# y& P) M - // 3.2 加载自定义字体
& Z: {- G) \/ j* n7 y4 i - $font_file = '1.ttf';
) R3 ~6 r# Z d' ? - $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));
" z! W4 E( q2 ?3 N7 d& ^ p - // 把文字填充到画布% c O4 {- u; W
- //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);8 h: Z1 h" J. F1 y w& |2 L
- imagestring($image, $fontSize, $x, $y, $string, $color);, q" h) x5 l: y/ t2 d# T
- }
$ _2 _; P" t4 u - $_SESSION['code']=$code;//存储在session里6 j: r" Q- q; f% G$ u
- for($i=0;$i<200;$i++){
+ Y. W5 T7 f" _7 h; e - $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));
% r' H& J- x$ A- Y - imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);
: {! n, g3 I: U$ E0 }) c" u' U' B - }
# K3 S. b6 l$ N* y( o) O2 x C - for($i=0;$i<2;$i++){
) q- z2 L) l9 T/ R. z- k, m - $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));
: ~7 z& l% L+ f - imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);
3 \+ a/ y( [7 ~# x" d4 G, X/ p& Y - }
; S! R! @$ J6 b6 m8 ^ - imagepng($image);+ \8 A* j! r/ ~/ f7 _, G
- imagedestroy($image);
: t; r6 x/ k6 Y0 [+ G2 z& @ - . G6 p1 b+ o* H8 @. d
- ?>
复制代码 . V$ c& R0 T4 S
form.php$ m! u8 S: o2 @0 {: b l& c2 _
- <?php/ e" C: ?: s8 P C' D6 S
- if(isset($_REQUEST['code'])){
/ n* H: w& Q& J5 e - session_start();: d! e( ]+ R: m( c
- if($_REQUEST['code']==$_SESSION['code']){
2 n# _8 _2 h" ?; B8 G* J( Y - echo "<font color='red'>输入正确</font>";
( q: C4 j( ~ v! g8 N5 e* S - }else{
+ u5 s; k2 k% B% t - echo "<font color='red'>输入错误</font>";
( O" e% ?2 U6 ]* ? - }
9 @8 W$ U* W6 W, \: [! P% W - }
2 |: s& F& R5 q7 Q% K - ?>
4 M* @5 D1 f2 `# v. w+ B - <!DOCTYPE html>
# g9 m* f- u p, E - <html>
9 T7 V- U, [; X* Z; }9 B& j0 N - <head>
# u% P5 M; m6 v( e+ | s - <meta chartset="UTF-8" />! N" L' @3 A+ v' A2 ^
- <style type="text/css" rel="stylesheet">
) n4 ^9 d2 n$ N3 a8 A* m# L' F - a{1 b- e- {. |+ b
- text-decoration:none;( v, W8 [2 E, h1 O
- font-size:30px;
/ H. O) g' L* A - color:blue;
# x' s y- H6 z/ W - }3 G5 k2 t: Y$ ^. u
- a:hover{( O, w& O9 R& U6 |; x
- text-decoration:underline;: @/ \9 |: g/ u% b8 o# ^
- }
# h; L, p5 a% H. P. S/ ~5 N, ], A1 P* H - </style>
. r; r3 m2 P2 U( T( p - </head>% Q" Q* j+ c' c' @) [6 T) ]. X5 u# |. Z
- <body>
) U0 e* s- \6 v6 y9 H* s - <form action="form.php" method="get">& w6 o0 q( E6 C
- 验证码: <img id="img" src="index.php?" onclick="changeCode()"/>6 A% U, {; I$ M1 V1 S
- <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />7 p; z5 h5 B N) W, T% f1 ~
- 请输入验证码: <input name="code" /><br />
& n6 O: _( c1 L& T' y - <input type="submit" value="提交" /># n3 E- W: U. b
- </form>" h" R. {3 _! T" T, l* q! d
- </body>
0 U Q/ M; B6 T - <script type="text/javascript">7 [) u8 V$ V6 X; e1 Y7 \
-
/ S6 y! a k7 X# y8 ^: j/ M! d4 \ - function changeCode(){# L1 Z, Y! g9 d. Y
- var img=document.getElementById('img');
6 i; s/ a3 }" E" S - //img.src='index.php?r='+Math.random();* U9 {+ O' l' j8 J4 A
- img.setAttribute('src','index.php?r='+Math.random());
/ M, Q1 f8 O @" A3 V* W1 X* x9 l - }
* r7 z2 U% q: A. z& C - </script>) o) D. v( T4 b% R' p* Z: D
- </html>
复制代码
: L7 b' N) V6 w) X6 t9 n) u" G- L0 Y. T; K& j2 f7 o6 U
|
|