管理员
   
论坛积分
分
威望 点
贡献值 个
金币 枚
|
index.php
# j2 ^* @. j7 j( h8 j- <?php2 F7 y2 c# Y( {+ j
- session_start();
" y1 N: L! J0 O' b - // 丢弃输出缓冲区的内容 **, O0 j$ I+ f( Y# o: a4 Q
- ob_clean();
& t8 ~. R& B" f) k$ S. N - header ('Content-Type: image/png');# B8 s9 z. P, ^7 }, z
- // 1 创建画布; g# H! r; \! S3 G' }; i
- $image=imagecreatetruecolor(100, 30);
/ d( Q$ Z, o( W7 V - // 2 画一个矩形并填充背景颜色为白色
: b/ G" b2 H4 H7 `7 \ - $color=imagecolorallocate($image, 255, 255, 255);; K3 Q' P, L6 ~ H
- imagefill($image, 20, 20, $color);
6 f$ i' a( C6 x9 s0 h# Q& z - // for($i=0;$i<4;$i++){ M- L) |& k) u' Y+ o
- // $font=6;
4 C4 W- g! I, w8 i2 d. h6 r' Q# N - // $x=rand(5,10)+$i*100/4;; ~+ K* b# G A3 L3 t7 F
- // $y=rand(8, 15);3 V% J+ p+ i( P6 R9 Z
- // $string=rand(0, 9);3 }. o, l- i% ^+ f9 l
- // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));
) H1 P* D2 Q7 j: o3 q* J- H j - // imagestring($image, $font, $x, $y, $string, $color);. ?" I$ }6 D# t) A- L
- // }
: K- e0 D a7 Y, a' F - $code='';& C" n/ t% g. { e% [
- for($i=0;$i<4;$i++){
6 y- f9 Y; {/ @8 j6 | - $fontSize=8;
: O7 q0 c0 Z& `+ U# {, X - $x=rand(5,10)+$i*100/4;
2 f9 E [- o. V/ i" S( ~0 i4 g - $y=rand(5, 15);/ G% F: }2 f% G. R$ S8 `
- $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
! }( i- `; U- _# F% Z - $string=substr($data,rand(0, strlen($data)),1);
9 W0 i4 m& ~/ J& n" V, @# \# g1 J - $code.=$string;1 p4 l: ]# p; L9 h
- // 3.2 加载自定义字体
' |& t" R' F; @; [5 n5 S8 d7 F% ^ - $font_file = '1.ttf';
% G' u! K% h1 w - $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));
: _& v: E- s. L; ~) w/ B - // 把文字填充到画布6 B" `" \" T+ X# a% Z6 R; |6 W9 S6 K4 Y% e
- //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);' G' p1 R# d3 S3 O
- imagestring($image, $fontSize, $x, $y, $string, $color);2 @' w+ E2 }% R7 d
- }8 ^8 W; ^" Q. m0 b. W
- $_SESSION['code']=$code;//存储在session里
+ i# R1 F/ F* s9 x7 l+ j5 ~7 q+ h: u - for($i=0;$i<200;$i++){6 V1 Y% c* t3 P# k' g
- $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));6 {& b6 _; \6 [+ j" Q1 s
- imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);
% }: b( w& R6 Q0 B: u - }+ @2 W2 U5 I2 e& \9 ]3 E' K/ L
- for($i=0;$i<2;$i++){4 j4 l4 S" v7 m, T* m7 u+ U: h
- $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));
7 m0 x( Z1 I- V% ^/ ]) B; p0 U - imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);
3 |" Z* y0 N+ v1 k - }; R+ g- X# [/ U+ D. A2 C: c5 ]2 L. f
- imagepng($image);' `/ l7 T) d+ m# ?- k
- imagedestroy($image);
# T; b6 b" @* X - 1 J. N1 D6 u" z: Z$ i2 ^ W: ~
- ?>
复制代码 2 C0 F( r1 E4 r: L
form.php" W5 @( i( @( ^* |4 A
- <?php
! D' O, D- j1 H7 \& ~: x - if(isset($_REQUEST['code'])){
/ q( z" T) q+ `1 o# e - session_start();" m9 C1 d$ V$ l' b2 _; v
- if($_REQUEST['code']==$_SESSION['code']){: z% g$ U4 A$ n4 t& b m
- echo "<font color='red'>输入正确</font>";3 Y* y+ f" z- J9 J% T
- }else{6 F2 O6 I5 O8 R, m5 X
- echo "<font color='red'>输入错误</font>";# y A0 W6 h& a
- }
) D, C0 T3 ^% H1 p. ~& m - }1 l4 p% v% J# F
- ?>
: D- W: v2 D! c/ D! r* e - <!DOCTYPE html>
- [$ ]% D- v: \% Y4 o5 X5 i0 ?/ ~ - <html>/ p7 m$ w) C, |
- <head>
! m$ \- Q3 x2 V! l2 y( h - <meta chartset="UTF-8" />
, ?7 l; I9 ~2 @* w' R4 B" { - <style type="text/css" rel="stylesheet">2 k" r* I, k i: Q$ r
- a{
4 ^! z1 z2 Z, s% J6 f4 F% X - text-decoration:none;7 @; f; `7 s. g( e2 f5 {* \
- font-size:30px;: i9 N; N- T/ H6 i
- color:blue;0 S4 w/ S9 Y: O! K) @/ A
- }. t9 l4 x/ f! C& h5 ?5 ]
- a:hover{
7 b( X6 k4 G- w% `. P3 n! b, j - text-decoration:underline;; p8 _6 A) V, \, q
- }
/ \' ?0 e& z: f5 D5 X$ g - </style>
9 E* r/ ]) }5 j - </head>
* U8 e( @6 o8 B* a' { - <body>
G2 y' F- I. p6 g! x" U - <form action="form.php" method="get">
- R7 N+ D& v) ]/ r7 d6 h' h0 t - 验证码: <img id="img" src="index.php?" onclick="changeCode()"/>
9 \' o, q' [+ e) F' M+ n; w( } - <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />
2 |4 k9 i8 p& a% S5 O5 P+ o - 请输入验证码: <input name="code" /><br />
6 [& h2 A) e7 f- N, f - <input type="submit" value="提交" />; J$ q" K" V7 ]( W7 ?4 q6 t) Z
- </form>* o/ ~9 g. o/ D/ }% G1 Q! i
- </body>
" L2 t! R$ G( J1 i; l - <script type="text/javascript">
& a8 w; E4 L. @2 j/ E6 I6 _ - " T% \8 t! W$ V# C* D0 M
- function changeCode(){4 z5 k5 i6 m: `* e4 L# Y
- var img=document.getElementById('img');/ M: f/ Q9 H! e/ Y, u; _0 w
- //img.src='index.php?r='+Math.random();
( M4 i, V; v% W - img.setAttribute('src','index.php?r='+Math.random());/ ~3 W) g9 H1 A% L
- }5 n, y% }' O8 `: y% j' H# I
- </script>
5 \6 x. R- n* s. c; R3 L% M: q - </html>
复制代码 * z( w. Y% p- {5 b5 Z! k
6 Z3 r i6 B) F+ j: }: s |
|