管理员
论坛积分
分
威望 点
贡献值 个
金币 枚
|
index.php3 ^% [% O7 a9 ?3 M
- <?php6 C. N* v( b( Y+ m9 P# Z
- session_start();
( ^9 L, M5 [$ T( L4 V# M. O - // 丢弃输出缓冲区的内容 **
' B! j3 ?; @7 b+ g$ k9 X# D# [ - ob_clean();
* b1 m) B5 S3 T% f$ C' s" z' ? - header ('Content-Type: image/png');
( o$ c* z4 |5 j* A; [* A - // 1 创建画布
: P9 H0 r I3 _1 P& S - $image=imagecreatetruecolor(100, 30);7 @" i% h- c; x' O8 Y
- // 2 画一个矩形并填充背景颜色为白色3 W" K" X& N9 C
- $color=imagecolorallocate($image, 255, 255, 255);
+ s: h) Q: H9 I$ R5 } X- d. Z - imagefill($image, 20, 20, $color);
. K) G2 h4 P6 z1 g; B - // for($i=0;$i<4;$i++){" R3 Z# w- `3 B
- // $font=6;! M/ j5 V. [2 v4 g
- // $x=rand(5,10)+$i*100/4;
+ k2 y& g# ~5 ]- c - // $y=rand(8, 15);
2 E& E8 n4 w/ U1 \7 ` - // $string=rand(0, 9);" o S5 u% W2 y. P0 D
- // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));! `; ^, w" F. d- Y' ?
- // imagestring($image, $font, $x, $y, $string, $color);
$ q7 }5 F w& u3 h; \2 X) G5 C - // }
s& l. @/ t. \( |1 z2 ] - $code='';7 ]4 \9 O( u* G4 z" s- O# j
- for($i=0;$i<4;$i++){+ }4 |) K) v; E: x
- $fontSize=8;
) h# Z9 J6 } T _ - $x=rand(5,10)+$i*100/4;/ A$ J% ]. R. ]' [, r% Y
- $y=rand(5, 15);
. I( q" X" }0 [/ F, ~- ?( O( r% Y - $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
W; Z' s* j- k) P: b2 Q) | - $string=substr($data,rand(0, strlen($data)),1);
9 X" d' o, R. h; E0 j5 M: c - $code.=$string;
) T1 N7 D, N+ t, A1 Y1 U - // 3.2 加载自定义字体1 ^' ?7 @4 i8 u2 Q7 D# [2 z; L B
- $font_file = '1.ttf';
$ D Q* D" k# N" }+ U - $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));
' I3 ~) P" [; ~ - // 把文字填充到画布$ ]0 M9 x9 \$ e4 H
- //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);
$ Z0 p8 U5 F' m+ P1 ?5 s8 p - imagestring($image, $fontSize, $x, $y, $string, $color);, \8 P% S" E6 t! {& z
- }
* b1 [. D/ `$ v - $_SESSION['code']=$code;//存储在session里
$ i% q: S$ }; B" n2 i - for($i=0;$i<200;$i++){, U# f! z+ T; I$ \2 K1 s7 l
- $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));
& A* }' L+ ?' p1 U/ f - imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);; b4 w; V& b% x3 B
- }
7 o% j2 `& E( d/ P( S) u - for($i=0;$i<2;$i++){
4 [+ l; B7 e8 h: b, R. m+ r# U# W - $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));0 b/ b; R- Q% _% j
- imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);* y( q1 D3 k0 B
- }
9 f1 R& G% l1 i' `8 Q: D. ~ - imagepng($image);% i# G2 j3 ?8 v o1 Z5 }: _" I
- imagedestroy($image);7 X# w1 f% {6 I, ^: B
- . g( n( h" {! r( u/ [% \+ e7 C
- ?>
复制代码 " W: @) @; v7 B3 m, }5 h- j
form.php
2 R3 W. J0 J# ?- <?php0 L, N) h- Q* {$ l' ^6 E
- if(isset($_REQUEST['code'])){3 P% V5 \3 a; l X
- session_start();" G2 e4 _* g5 E/ J; b" C3 m
- if($_REQUEST['code']==$_SESSION['code']){' N1 | M6 U( @$ A
- echo "<font color='red'>输入正确</font>";' j/ h [. k+ k% ?$ e
- }else{- s1 [6 v9 Y5 l" I
- echo "<font color='red'>输入错误</font>";9 O- E" G, x2 J" d; B
- }( n8 L3 ]( ?3 e7 z" h4 c5 L+ O1 t7 G
- }
# u8 u5 a4 w0 d& S$ c5 U, I# ^ - ?>! t- a; ~2 A& t7 ~; I
- <!DOCTYPE html>' k* |5 {( b9 ]4 @7 Q/ i
- <html>5 [& O0 _5 G" ^2 U5 \
- <head>
$ a8 } e7 q" p: H$ h - <meta chartset="UTF-8" />
, |6 D% d7 ]) z! Q. | - <style type="text/css" rel="stylesheet">0 N* N U1 D8 X! j
- a{
* N0 X+ E" ~5 f, \4 s7 {$ U - text-decoration:none;
; k/ m9 y0 Q! D) { ^7 u - font-size:30px;
& }( {* D5 Y- g2 ~ - color:blue;
, A+ Y/ I2 B& v* h& L9 V# { `. h - }' L; L' u: f: c4 q) c
- a:hover{' E, [/ R. l' j6 i5 u
- text-decoration:underline;
J7 K8 M x& _ - }" v0 H5 r$ Q {9 w9 Y
- </style>/ G0 C# _, S8 z2 [
- </head> U1 y% X+ i" x5 R
- <body>$ g! b4 M; T5 O. |' ~1 B, c7 z* B& p
- <form action="form.php" method="get">
3 N& P+ s$ N- @9 w3 o, f - 验证码: <img id="img" src="index.php?" onclick="changeCode()"/>
) M; f5 R1 D/ h0 X - <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />
0 p+ E% Q" w6 ~2 d6 ?: ~. k - 请输入验证码: <input name="code" /><br />7 ^8 E/ c! G2 V2 ~% N9 ^5 j
- <input type="submit" value="提交" />2 u( q, B6 v9 N( i3 ]$ k
- </form>0 f2 z5 ~2 J0 W
- </body>; A3 {1 n: M8 y$ q- F8 f% q& c
- <script type="text/javascript">
p4 b: k# e6 W- a3 _ -
9 j5 U4 B, l0 o+ \3 j( a - function changeCode(){
! X9 a |. a( b2 X& g) Z - var img=document.getElementById('img');
" z& h- V9 j2 I8 S) d. G - //img.src='index.php?r='+Math.random();9 `4 {/ W0 o2 i2 L" e
- img.setAttribute('src','index.php?r='+Math.random());8 @- Y) r# L3 P7 i s
- }3 T6 y- f& |8 K+ }
- </script>
$ K0 F2 O0 x7 Q K# ~0 ^. h - </html>
复制代码 4 c7 u) n- i7 z R% [/ R- G% @
8 U( Q9 Y- A4 N |
|