管理员
   
论坛积分
分
威望 点
贡献值 个
金币 枚
|
index.php
) T* ]# \/ D4 y7 _+ W! D0 S) F# m" W- <?php
* c# d3 ]1 o/ q( @% F V3 D- J: |4 P6 t! ? - session_start();
3 J6 L; X! t8 P7 L G. I9 w% w - // 丢弃输出缓冲区的内容 **
4 q: Y+ w9 I* T$ J* z4 D - ob_clean();' ] x9 @$ v3 {7 r% o, n
- header ('Content-Type: image/png');
! E* s' I5 _4 H1 I4 a* Q - // 1 创建画布
. J' y+ K, \1 F- ^& ]% J - $image=imagecreatetruecolor(100, 30);
7 H( [3 N+ w2 `7 J+ r3 n* L2 }, S- S - // 2 画一个矩形并填充背景颜色为白色
( J# C' j/ R' O1 a; `5 c2 t* J - $color=imagecolorallocate($image, 255, 255, 255);- j( q, b/ p8 Y4 }0 r4 D7 `" H% e
- imagefill($image, 20, 20, $color);6 z: \3 M7 `" a; R! H& L9 m" k: a
- // for($i=0;$i<4;$i++){
* [. y+ H% Z: t& g' K7 j! V& U - // $font=6;
* f, b8 X- V+ `+ X+ R - // $x=rand(5,10)+$i*100/4;
, @6 ]* l# @; P: t# w4 m& _9 v - // $y=rand(8, 15);, O& P. p; \6 t& [+ t
- // $string=rand(0, 9);
% V1 |8 s* j% w+ H$ g# Q - // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));
6 s: T- O4 s* x! T - // imagestring($image, $font, $x, $y, $string, $color);: M8 {/ e. I! H/ m& x
- // }8 `! }. o" V9 J& [
- $code='';
$ W: |5 \, T5 Y- V - for($i=0;$i<4;$i++){8 G9 G2 E' P/ ?: _
- $fontSize=8;% U: k# r6 G. n; ^) o0 ]& c E
- $x=rand(5,10)+$i*100/4;
d6 R" u m$ O/ t% J - $y=rand(5, 15);
3 z; R) U% ]0 R- a - $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';: M/ `- J. N5 P
- $string=substr($data,rand(0, strlen($data)),1);
0 t: j* Z: t+ B' E9 D - $code.=$string;5 B' o; |/ d6 S4 A+ g1 F5 x: p
- // 3.2 加载自定义字体
- H/ m( B8 Y, l% ~( I0 w) @5 `& r6 x - $font_file = '1.ttf';
5 U, g! |8 V3 q/ X, ^* m, x - $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));, i2 t3 c0 e. r" |- z, Q8 E
- // 把文字填充到画布4 H+ T- ]& ^8 {- x: I
- //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);2 U# K. x9 ^2 J( K6 d h% o
- imagestring($image, $fontSize, $x, $y, $string, $color);
7 c: ?! r" j6 m% i6 S8 F+ G - }3 C. [# P0 z* ~% Q' E# X8 ]( f8 Q$ W
- $_SESSION['code']=$code;//存储在session里
6 g7 E" q# [2 u, d- H5 I# i - for($i=0;$i<200;$i++){
. e- z" r: R/ H5 V9 ?8 [* I - $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));
& o- y% K% s& o- a4 y$ D& i" ? - imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);
3 o0 x" R/ ` o7 I8 Q9 ~- k - }
; {+ \2 l1 r/ k - for($i=0;$i<2;$i++){
& o& _( J/ o! A/ w - $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));
) J7 T' a. {" o% T1 w2 W5 _ - imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);
2 O. i+ i' w: D3 U* H$ s - }
5 t' p- ?0 c0 k" U5 Z - imagepng($image);
7 D8 P4 Q6 D, H. a4 h0 ]7 w* V6 h) e - imagedestroy($image);7 F' v/ D& A" e" J
! ^5 g/ w0 r4 V9 n+ s) g- ?>
复制代码 9 B6 m8 G2 E0 _3 f
form.php
# E1 u' |8 w. [( z2 \- <?php
/ V# c$ n/ A: z$ t) F - if(isset($_REQUEST['code'])){
1 S5 {) R8 S u/ C5 X* f( W% P, c - session_start();: q( }0 K. p+ `
- if($_REQUEST['code']==$_SESSION['code']){+ d1 K& ?& Y1 Z/ m& S
- echo "<font color='red'>输入正确</font>";) P& s9 \% W* W2 o1 \+ y
- }else{5 k/ J* ~! B% C' T7 P+ L; @" E; Z
- echo "<font color='red'>输入错误</font>";
! P& ]! |5 H. p7 g! f - }
. r; w, S, X4 ^: O3 s7 ?; ? - }" G) _ a+ \1 m9 w
- ?>+ K$ f0 J% f5 x* I0 W) z
- <!DOCTYPE html>
4 o$ u F( W5 `2 ~$ w - <html>& U3 _ Y8 i2 k3 o; u
- <head>
+ O- T8 q, j% Q - <meta chartset="UTF-8" />
5 R4 C8 G5 p9 }/ j. ]1 W$ @& J. g+ t - <style type="text/css" rel="stylesheet">( k9 h) E4 P( y3 o) _, a; A6 b6 N" H
- a{
) j$ f& h$ Q: l7 q, X: a - text-decoration:none;- K. L) _; q, d) T7 d' q
- font-size:30px;
$ ^9 \7 [2 G, t8 a$ R$ J: h - color:blue;
0 o7 A& _" o7 Q1 p8 M5 w - }9 c+ }+ N+ B1 }. o% ]$ E' |
- a:hover{, U% n2 `8 P+ L
- text-decoration:underline;
$ l2 q" O9 u8 z! b - }
& x: @1 X! u& M! u- x* L7 h6 r2 w0 @( y - </style>& r/ v4 D, E: S, W7 z+ X
- </head>5 S% @, }) y5 O0 }1 S. n+ o
- <body>
9 }% U# E4 M; u! v( _ - <form action="form.php" method="get">2 ^0 l! K# D( u, h) U G" X! f: ]
- 验证码: <img id="img" src="index.php?" onclick="changeCode()"/># m( ?5 j% b2 r( _ U
- <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />3 M! P0 O+ n+ a2 J( F0 Z
- 请输入验证码: <input name="code" /><br />9 L/ ]7 t# Z3 [7 w7 M' L
- <input type="submit" value="提交" />
4 E% X; c/ W4 `: }* V - </form>
" s) ^" K/ ?8 h - </body>
5 A7 z* a- f1 K) \. U% L - <script type="text/javascript">
& N4 U& w1 p& k -
3 i/ [& x3 V4 p: f - function changeCode(){8 t, m$ ~( M) E# F1 } L4 i
- var img=document.getElementById('img');& D# @6 Z' q- T$ c8 g
- //img.src='index.php?r='+Math.random();1 | {0 A4 ]. E5 S U# Z, W# _5 D, p
- img.setAttribute('src','index.php?r='+Math.random());
$ U, H/ K3 C& I d8 b* T - }
T8 N9 u! V( S' J1 k% R! N p - </script>
/ V' f" d0 C8 f9 F: h - </html>
复制代码
9 `+ {0 W: F) R7 w( A
8 u1 B2 O ^8 F; I |
|