管理员
论坛积分
分
威望 点
贡献值 个
金币 枚
|
index.php* D2 u3 u1 i9 h1 F2 R4 J
- <?php
+ I2 K" Z; V' |! H - session_start();
9 o; A8 I* O+ n% w - // 丢弃输出缓冲区的内容 **) r- e: f6 \, H; J# H; G9 P$ z
- ob_clean();
* H( f6 F2 O6 Y! Y1 s9 W. @ - header ('Content-Type: image/png');! m6 t6 ^, |# {/ F; G! Y
- // 1 创建画布4 a) }# H" L# u# ~2 ~1 O
- $image=imagecreatetruecolor(100, 30);
( |2 v5 Z5 Q7 P6 ]8 ~+ o8 K - // 2 画一个矩形并填充背景颜色为白色5 y% a% i$ w8 D1 w
- $color=imagecolorallocate($image, 255, 255, 255);
0 n$ Z6 z1 w8 R* \5 x8 m - imagefill($image, 20, 20, $color);
3 \# ?3 B, B j$ W7 A# K4 y - // for($i=0;$i<4;$i++){" V, @! |5 Q* k0 L
- // $font=6;" h/ A' z8 l$ Z# q: e |5 f/ D
- // $x=rand(5,10)+$i*100/4;$ f0 W# x- p' r' o) C. @( ~
- // $y=rand(8, 15);3 i1 @$ U7 H! Q8 O' e0 B' u s3 h
- // $string=rand(0, 9);
5 e' j$ l' w. s - // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));- \7 p- Y" n- A& `* T( j& v8 u
- // imagestring($image, $font, $x, $y, $string, $color);* A" B4 U/ O# w5 ]3 V5 B$ a& V) V
- // }3 d1 ], P( n: E) E: G* d' J
- $code='';. Y' F$ k6 f6 p/ q. J$ [+ R
- for($i=0;$i<4;$i++){6 R, }/ m7 s) {1 x3 ] B' e
- $fontSize=8;, d5 X. t [ T" X1 ^( `
- $x=rand(5,10)+$i*100/4;9 s$ c/ Q# R& ^8 w5 t' i& X2 e
- $y=rand(5, 15);5 C7 K, u5 |, V9 y) _2 D) G) t
- $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';8 F: g( y- a) n; ?$ R
- $string=substr($data,rand(0, strlen($data)),1);9 J& B @* t( a; s! r7 ~" O
- $code.=$string;
# r. y7 ~) e; ]0 M- H5 m - // 3.2 加载自定义字体
5 C* o3 U ]$ X) F' f7 o - $font_file = '1.ttf';
6 B6 D- M) G% l# m - $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));# ]: p& `6 G o) Z0 R3 r
- // 把文字填充到画布9 P( j! p' x) ^* K; {
- //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);3 m& D! @: p$ {0 P& [4 i7 `' D! E- D0 S
- imagestring($image, $fontSize, $x, $y, $string, $color);; Z, w/ O4 ]& r' A
- }( M8 q5 M" ^8 b" ~6 {/ z
- $_SESSION['code']=$code;//存储在session里. z/ B1 T+ t0 P) X. ?
- for($i=0;$i<200;$i++){
0 N8 ?/ V, ]: {! D - $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));
w. C# o4 I8 t" v5 C0 [( |+ ` - imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);7 n9 U# Y/ o i- I. c# c! H
- }
: W2 Z" D. q! ?$ J, e) k5 a - for($i=0;$i<2;$i++){& L/ t% O- b+ |
- $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));6 C9 Q& R1 s* V- d b* R
- imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);- f: q2 v; Z/ r- N0 G& ?
- }4 k2 g/ m) `; v
- imagepng($image);+ O- n1 E/ J$ z ^
- imagedestroy($image);" h" Y: x/ O5 Z& C- \+ ]
* Y; I9 d; [) V9 v- ?>
复制代码
( q i( f' h$ a* x% jform.php
; B' _0 l; t1 l, O( ]8 s z0 h- <?php
/ p# A/ \$ z% B! u - if(isset($_REQUEST['code'])){
6 ^: T Q1 t; v6 ? - session_start();
: `& l, ^6 g8 R - if($_REQUEST['code']==$_SESSION['code']){+ w% m1 R t9 W- w. ]
- echo "<font color='red'>输入正确</font>";3 g, O# t1 d! ?
- }else{: N# O/ c' l( E
- echo "<font color='red'>输入错误</font>";
# I& k: C# Z/ D7 `; D - }
; [; Y3 P8 t2 D. W/ P8 G' P- W - }% n/ ~$ x# _. E0 z- S3 z; ?
- ?>, U j+ m$ c; o. J$ B
- <!DOCTYPE html>3 h; j4 Y) Q7 e
- <html>" e4 q* c: v; Z% \7 S, ]/ q
- <head>
9 O- }5 Z1 o W( s; B' S9 A1 x - <meta chartset="UTF-8" />$ I6 N3 ~, u# ^: _8 @2 K
- <style type="text/css" rel="stylesheet">
( y& n6 a" I6 j. a- v - a{6 n* Z$ I. g# r3 F1 o
- text-decoration:none;
0 l* S+ p5 z/ H: l3 R - font-size:30px;8 ?6 ?7 S3 W: _1 G6 i
- color:blue;
/ L+ s. D% R1 U: \% i1 G - }
* s0 K# g& T: E - a:hover{
8 A, w3 a/ y' Y" e* s - text-decoration:underline;
7 ?; q7 H g8 [7 @; o' ?2 l2 i - }2 u. c9 \( ^ }& v
- </style>4 D0 y8 o8 f! e. ~& x% m) n5 g
- </head>
# H+ k' _7 U' j - <body>
! `2 N3 W! k q. h6 T6 ` - <form action="form.php" method="get">. W" G6 R8 i( }) c$ B4 y {
- 验证码: <img id="img" src="index.php?" onclick="changeCode()"/>
* {% a4 f# G( T6 s - <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />
( U# t P( W/ P. d2 j1 f8 A - 请输入验证码: <input name="code" /><br />0 j' e: F8 l, y+ y$ N2 K
- <input type="submit" value="提交" />
; i) ?+ g/ Q! X% G# c - </form>
0 Y) U% K. d/ F! T% h# A: _6 |9 Y4 G - </body>
- K4 k9 q; Q3 [( L - <script type="text/javascript">& ~! o2 d! U6 Z' N/ b; z, S9 Z! D
-
, [0 n* r4 E$ j1 L8 O2 }' O) c) \ - function changeCode(){* u# U: J9 E$ j6 r5 l
- var img=document.getElementById('img');" M# |3 n2 i- J/ N
- //img.src='index.php?r='+Math.random();
7 b3 Z+ o) j) e0 @' X0 h8 ~( w - img.setAttribute('src','index.php?r='+Math.random());
: L) e5 \, N/ I$ c9 W - }
$ e! |& J" ~& Y4 K- h - </script>/ O5 q& V- y! E4 A( S5 x
- </html>
复制代码 " [. {3 X3 F+ E. p8 f
# L2 _, y8 d( q7 q X: U. o
|
|