管理员
论坛积分
分
威望 点
贡献值 个
金币 枚
|
index.php6 U B) Y- ]! ]9 v; N
- <?php/ N( p0 n; d$ c) i9 v
- session_start();
( p8 g! |) z& L6 E9 s - // 丢弃输出缓冲区的内容 **1 l2 D' c. }0 l- Q3 }3 b5 f
- ob_clean();( g! i! ]* N0 K: l- t
- header ('Content-Type: image/png');+ A7 g# R9 H2 P6 L
- // 1 创建画布
( s: |; ^3 C: @2 w) m y - $image=imagecreatetruecolor(100, 30);2 e8 x+ \8 Y* A; G- T$ j j
- // 2 画一个矩形并填充背景颜色为白色
( p9 I) S2 v" j7 S- k: \$ D, Y+ K - $color=imagecolorallocate($image, 255, 255, 255);
) t3 S: [$ r$ |2 ^6 D/ t2 p, ~/ j - imagefill($image, 20, 20, $color);
, E& f/ n, ~7 y5 M% H - // for($i=0;$i<4;$i++){& ^; |5 T. o. T, q
- // $font=6;
# }. { x; ?7 i: u& b; k3 M: Z - // $x=rand(5,10)+$i*100/4;
- ^3 q* y1 @, D0 H, O1 w$ j5 O - // $y=rand(8, 15);9 Y9 Y o# H( N2 Z* Z3 ^8 f. M
- // $string=rand(0, 9);# H* f q2 h" C5 T! D
- // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));
; t$ C% j$ i7 I - // imagestring($image, $font, $x, $y, $string, $color);
- D) X! v1 ^! z% w - // }0 ]* ^) J9 r2 r/ q: z* J7 C/ @
- $code='';
! O6 \! z/ K8 t4 J - for($i=0;$i<4;$i++){
3 K4 {6 p+ Z% M" L - $fontSize=8;
' J7 O/ f$ d* N8 f - $x=rand(5,10)+$i*100/4;, t# E& a* r+ _
- $y=rand(5, 15);
, w8 Z! {+ w0 ]- p4 b - $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
+ v; Q- g+ v/ P8 T; X/ `6 u - $string=substr($data,rand(0, strlen($data)),1);
# [, r C% |! h# T N" \0 v - $code.=$string;
! }" I) C1 }( F: I! B! w - // 3.2 加载自定义字体
% W% c7 m; n+ j* \ - $font_file = '1.ttf';+ V, p* b( D& t3 o+ o
- $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));
% H; F O: z5 h% R) h2 g( e - // 把文字填充到画布/ r) v8 J2 e8 }) w+ w& I8 |
- //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);8 g$ o0 r4 ]* G
- imagestring($image, $fontSize, $x, $y, $string, $color);5 N: ^0 G' y& I& P6 H
- }
& b B: M+ E- o, V; _( U - $_SESSION['code']=$code;//存储在session里
" ^; ^9 C: b) y4 F) S - for($i=0;$i<200;$i++){
4 W1 b/ V0 A- Q - $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));9 x+ \5 E) f+ i' C( d2 u
- imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);. G# r: Q1 j0 D# c0 R
- }5 P: ` n- T. z$ {& l
- for($i=0;$i<2;$i++){
! Z$ z# @6 {, O' F9 \ - $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));* T$ l+ a7 m" B% Y3 Q' r5 F
- imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);
/ O" j# }( Q" Y& ] - }+ ~& Y9 Q: o9 F! J6 {8 i
- imagepng($image);
4 c8 @* k" ^' f1 m - imagedestroy($image);- r Q+ ?" d2 ~, S. B
! i9 y) E: V/ d- K- ?>
复制代码 2 m. E, n' l9 e/ l2 Q
form.php
9 O; M8 s( W0 r X& ]- <?php z! u* o$ N1 m! R+ Q6 n6 u$ ?
- if(isset($_REQUEST['code'])){# m: ^: J# ?8 l
- session_start();
' Z/ H/ w+ t, N& u+ Q - if($_REQUEST['code']==$_SESSION['code']){
) p. N; L% _' x5 L2 n9 J" L4 s - echo "<font color='red'>输入正确</font>";/ u( g) a' q2 m
- }else{
% m: u2 [2 |9 J F( q - echo "<font color='red'>输入错误</font>";$ T$ B- w7 ~& ^. |& w5 P
- }" r4 u4 a7 }* m* g
- }6 O( [$ K- h/ E2 v
- ?>
, }: W8 u; t, M - <!DOCTYPE html>
- q; g' r) @2 ?* g - <html>* d1 V4 D4 I( m
- <head>! c p% P& p4 k
- <meta chartset="UTF-8" />- {* Q5 K B7 R! ]4 G7 }
- <style type="text/css" rel="stylesheet">7 }' F; n3 }" }5 o5 a, r
- a{) r8 ~3 \: X6 I) y
- text-decoration:none;1 r1 z, v$ \1 m' j; T' N. V
- font-size:30px;/ X& A$ W' I( e* g* o
- color:blue;
" y% H7 W- ?; M+ e - }% k0 L9 q6 n6 D" m# N$ m
- a:hover{ L; @ n4 A) |& u$ I
- text-decoration:underline;
B) L ^ M8 @, A) \ - }+ z3 g" r/ }, a
- </style>
$ ~; L$ t, a" H J7 x9 D( g - </head>5 R- K$ N( P$ X; @
- <body>3 v W% h# N' R( w% r
- <form action="form.php" method="get">; _3 P; L% u: U2 E
- 验证码: <img id="img" src="index.php?" onclick="changeCode()"/>
9 w. n" q( ]1 T/ p8 D# C2 ` [ - <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />
/ ^) r: {# ]. i7 w8 v7 J - 请输入验证码: <input name="code" /><br />) T0 M$ Y, w. F+ ~: f* ]2 \
- <input type="submit" value="提交" />- [2 m, |: K8 O
- </form>. \4 M% p" g" a: n! a9 R8 _
- </body>2 O! i+ a ]- ]) v, U& N; `$ \( f
- <script type="text/javascript">
' m0 d: B& P% s" r) l -
" e/ h" ]5 B& \* q - function changeCode(){
: y5 M8 O0 |( j( z$ V - var img=document.getElementById('img');& J2 n! ]1 V7 m8 s. O6 I- [
- //img.src='index.php?r='+Math.random();
$ e/ p$ { ~2 a8 m - img.setAttribute('src','index.php?r='+Math.random());
/ Q" ]# z' t2 ] @: y - }. c' o, m6 [( M+ k, b/ t g
- </script>
9 c. l5 k. j" [2 x: k - </html>
复制代码
7 ^/ k2 F; N9 ~ T; Q) b- B6 x" h8 s
|
|