管理员
   
论坛积分
分
威望 点
贡献值 个
金币 枚
|
index.php
- u( I Y' p& _0 P# x& K- <?php" g3 ^8 o. W, {/ E' ~7 I2 c9 q
- session_start();
. q( Z3 d0 M1 p/ ^, s - // 丢弃输出缓冲区的内容 **
+ _. c* s/ n1 v, j# ^( I - ob_clean();
4 W7 _9 S' K- Y' e, ~1 B+ K - header ('Content-Type: image/png');
6 j' n& k+ P+ r1 _: b0 F - // 1 创建画布: o& C/ h9 b) m) H/ N9 o) x0 X
- $image=imagecreatetruecolor(100, 30);+ [+ E# S+ ?1 y
- // 2 画一个矩形并填充背景颜色为白色
- }, n; g+ P3 T7 I6 C; t; ? - $color=imagecolorallocate($image, 255, 255, 255);( Q9 {( s/ F& i
- imagefill($image, 20, 20, $color);; J+ \& d; [+ ~/ u! l1 m8 ^, O
- // for($i=0;$i<4;$i++){- D5 C- P& ?2 T( @7 G5 b
- // $font=6;& ]9 r* v) E% Y4 T0 r! F& [- O
- // $x=rand(5,10)+$i*100/4;
0 N9 ]) s& L6 h B) p; C& l - // $y=rand(8, 15);
: y0 d( T* ^- b1 ^# F - // $string=rand(0, 9);
6 k) h% R. U0 l6 K' y, K) c6 o - // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));, K4 E1 v2 e% w7 O, I
- // imagestring($image, $font, $x, $y, $string, $color);5 ?0 U8 M$ ]- O/ a; K
- // }
' q% }; f6 k. n8 ] - $code='';# K# |2 z& e# r+ _
- for($i=0;$i<4;$i++){7 ~5 V$ ], E8 h3 Y5 Z }8 g7 v% }
- $fontSize=8;
' A7 G/ i+ R. b9 `7 M: V8 ` - $x=rand(5,10)+$i*100/4;
7 f2 y% y% [4 D, B0 W$ Q" C - $y=rand(5, 15);8 B. n+ s* U+ z/ r! {: ?
- $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';! h% [* Z# k" F; _
- $string=substr($data,rand(0, strlen($data)),1);3 L3 Q9 R a4 X4 B& C7 ~6 Q% o+ w) \( v
- $code.=$string;* H% S' i7 w* ~% N+ {
- // 3.2 加载自定义字体; h4 |# _8 q7 U1 H
- $font_file = '1.ttf';
9 V9 d' c2 Y6 R/ k- ]$ W - $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));# y4 L3 G/ e- i
- // 把文字填充到画布
" ?. f) I2 j8 s0 j. e7 \1 M - //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);, v0 h- u; e! b X
- imagestring($image, $fontSize, $x, $y, $string, $color);
' G# ?' u, \5 _0 I Q - }! G" R0 W/ [ W8 u8 ]8 p
- $_SESSION['code']=$code;//存储在session里2 I# i) W1 m( H) O8 ~, N
- for($i=0;$i<200;$i++){
0 E, T1 F* W2 J5 [ - $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));
" l# `% O, n+ _$ ?( K) u" u - imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);: L" w& e- n* U
- }
+ t' v6 F. v, Z; r" |$ b - for($i=0;$i<2;$i++){8 g* ?4 o, S' L i
- $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));2 i I4 ?9 W4 T7 V$ `& |% W( ^
- imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);
2 p+ Y" q2 W; X- W - }
" p0 O" }* h' d# l: A - imagepng($image);
: M# ]2 M8 w1 q9 S0 u - imagedestroy($image);2 i, f2 m. w" x9 J- u+ X8 V# R
6 g u0 w1 y4 C7 v- {- ?>
复制代码 5 v9 ^+ h; q6 p5 D% N( S1 N
form.php: p4 T- q/ j8 o' J @/ M: q
- <?php1 o, U; `4 d! P6 ~
- if(isset($_REQUEST['code'])){
8 H2 Y6 T- l& H$ o/ L. ^ [4 \9 u - session_start();* z5 U. C ]. n' w, x- b7 m9 L
- if($_REQUEST['code']==$_SESSION['code']){
) |+ V6 q) \1 s, y; [0 P - echo "<font color='red'>输入正确</font>";; m& \& F4 s" M" R& \/ A x
- }else{
: j0 h. d5 C) y5 ~& J - echo "<font color='red'>输入错误</font>";4 {! H# P, D" v- h9 I6 Z
- }
6 _ _( X; ~* Y4 A. ~$ m9 e - }
/ ^4 O, H5 `0 H* r; q* E8 t - ?>
: E5 ], d% T+ z% ]' ^ - <!DOCTYPE html>
! J9 B4 x6 }. n0 j) z( n* y- a - <html>5 g0 f2 J4 G1 R& A( `. Z* g
- <head>
4 H' Q" R. Z) ~. e - <meta chartset="UTF-8" />" g9 r' o- f$ y
- <style type="text/css" rel="stylesheet">) L/ v2 ~' t+ U' b+ c
- a{
7 e3 ?& O5 `' v5 o" d: [+ Y - text-decoration:none;( s# m6 d# L: Y. L( G6 A
- font-size:30px;4 f% z% |6 T) d3 C5 f1 V
- color:blue;
) D/ {3 k' S! z; o' M6 {* A - }% o; E3 Z$ c% F# w
- a:hover{
0 b3 v/ O2 F- D4 a. ~ - text-decoration:underline;
, v6 w$ K8 R# v" g3 V1 \ - }: q2 j" V9 }: x: R( t- E5 R/ g
- </style>
5 c4 c- u" Z- X" _: b t - </head>9 w. J6 d7 k& z0 _7 a! m- X P% U
- <body>
& R$ m$ R0 \/ l! ~ - <form action="form.php" method="get">) G1 t7 P0 I1 a p
- 验证码: <img id="img" src="index.php?" onclick="changeCode()"/># O6 D5 O1 _8 t" C2 h
- <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />
: y' _; I7 }! Z }& s3 c - 请输入验证码: <input name="code" /><br />
' J2 _; i/ T8 E/ c+ w" }7 f - <input type="submit" value="提交" />
. q; c2 O- O. p/ Z* j - </form>. Z. a$ Q* s8 M
- </body>" z5 Q& ]: q* {5 v
- <script type="text/javascript">. d$ x! V) _; T5 ?2 t
-
9 ~3 u$ n- W7 M; `6 R4 h - function changeCode(){2 u4 `" _2 ]- }) u) q& P' M
- var img=document.getElementById('img');3 z) k. i& v( F- P
- //img.src='index.php?r='+Math.random();6 k* b7 P! u1 d; n1 F& ~
- img.setAttribute('src','index.php?r='+Math.random());
! b+ Q# p5 M8 Q+ F o3 u. [ - }
( S' \# l. ?0 r( u" T- | - </script>1 L! A; V, T$ i9 T
- </html>
复制代码 $ f3 V. X$ h9 [, z0 R9 G8 d8 }
3 l& K2 ^7 S+ ^* r; C; k* {; f+ B |
|