管理员
   
论坛积分
分
威望 点
贡献值 个
金币 枚
|
index.php% B# p3 C1 K; G
- <?php
3 [" p; j2 _9 h, w - session_start();8 H6 k4 M! d# d3 J( p/ N
- // 丢弃输出缓冲区的内容 **8 G1 m8 R+ b' y) q* L& b
- ob_clean();- \0 P9 F( r- j) [( ]" |
- header ('Content-Type: image/png');% a$ C: Q8 g5 `& O" [' t) E
- // 1 创建画布
# n: g+ F( w! u; {1 X0 x - $image=imagecreatetruecolor(100, 30);6 N4 t4 i% E( y* U
- // 2 画一个矩形并填充背景颜色为白色! y) s+ ^ u% K5 n U) K
- $color=imagecolorallocate($image, 255, 255, 255);
V) y( X% t3 L% s - imagefill($image, 20, 20, $color);5 }4 ^5 H4 d J" b
- // for($i=0;$i<4;$i++){' q8 u" u0 v% _$ T( b2 }
- // $font=6;8 w& N/ r; y! \6 I; \; x
- // $x=rand(5,10)+$i*100/4;% [. W. Q7 U. h
- // $y=rand(8, 15);
1 |! o3 a- p: P7 F: ]8 Q6 Z5 | - // $string=rand(0, 9);: L# q; P# L9 F6 G/ Z" ?
- // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120)); c3 X1 {& ]% _, D
- // imagestring($image, $font, $x, $y, $string, $color);; t- M+ @- u" l V. s
- // }& Z) `/ T9 `6 N: _% J, u# f4 u" z
- $code='';
/ a' x! y# u# C) ]) M; U" Z1 O% v7 d - for($i=0;$i<4;$i++){- @' Y: ~ C' n
- $fontSize=8;& u( |% J, l$ i2 `
- $x=rand(5,10)+$i*100/4;, v' j/ t% s r: J; _1 H3 k" Y! V
- $y=rand(5, 15);
) O& k# y5 R8 v3 a3 i2 e4 ` - $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
/ n K( O8 l% [; [9 f - $string=substr($data,rand(0, strlen($data)),1);
0 T+ G$ z7 @( W" J/ \ - $code.=$string;
+ N4 t, s8 }5 B, _ - // 3.2 加载自定义字体
" h( k: z7 f& Y - $font_file = '1.ttf';
& R) k/ O6 H, _5 \ - $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));" t& j' b6 J- a
- // 把文字填充到画布/ V; A' \9 w5 s2 v, c& W: o
- //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string); ?' X' e O$ i( z9 \( q, ~- I
- imagestring($image, $fontSize, $x, $y, $string, $color);
% P9 r8 O# b" m4 Z, o& N6 b - }
4 ?: l* L0 a" y5 g) K - $_SESSION['code']=$code;//存储在session里% F! b3 i& L- v
- for($i=0;$i<200;$i++){
$ z8 J6 T* T6 A/ i2 k7 Y' f# P - $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));
M" T; Z. w9 K - imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);
2 X# C0 v, }2 V$ O5 R {' \% L - }
# @6 V% n3 E0 b8 D) F/ W - for($i=0;$i<2;$i++){
0 j# h+ s: X6 z3 K3 r - $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));9 L% ?" u* p9 ^
- imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);
1 o. c0 v0 @% \* I! ?6 [1 r$ K - }
% J u8 G# u7 Z4 `" | - imagepng($image);! d5 O$ M0 l: w; h* F: n
- imagedestroy($image);
6 h/ y( A) p8 j" A/ ]
! y. q4 S) D# O+ W& q/ x- ?>
复制代码 / Z5 W( R# ^8 Z4 z* L# [
form.php
- v! u5 k9 n, C3 R- <?php$ u7 n' G; ?, h, F
- if(isset($_REQUEST['code'])){ @0 q: U& o+ E8 L% w6 \! c4 h0 V
- session_start();
9 U L6 P% f G) j7 l - if($_REQUEST['code']==$_SESSION['code']){
+ u5 U' u6 R2 H - echo "<font color='red'>输入正确</font>";8 c0 A, p% e6 E2 J
- }else{. G+ _1 S; q! [* R$ I
- echo "<font color='red'>输入错误</font>";9 d& |; C) R4 A/ {" f7 d9 ~ |; p5 }
- }
9 c; [9 a6 P; E4 l2 C* J - }( }6 _/ E/ ~: J/ |. A0 r2 Z
- ?>
% i8 |% f$ J/ e' i3 R6 Q4 b - <!DOCTYPE html>
! W: m5 M2 H$ O5 @- ~ - <html>
* t1 W' c( ]& m3 e0 y! P - <head>
: [& m( I- z, ^) v4 h3 C - <meta chartset="UTF-8" />
$ _: c' j8 r- v. k- D8 I$ ] - <style type="text/css" rel="stylesheet">) u1 @' y& x9 w7 W+ j
- a{6 z+ z& k8 m% E% N/ o
- text-decoration:none;; R2 }6 G' O# K; K, y! _
- font-size:30px;
8 n" n; U |0 l. X* G4 h0 B - color:blue;. C, s1 s% d2 ?! b' @' T
- }$ ]2 I+ _5 b4 d
- a:hover{
3 \3 Q: Y. W& J" ~4 o, v$ @ - text-decoration:underline;% s0 \% Z; u% M( ^/ L c8 u" O. q
- }) L0 T" N9 B0 J7 v( c' `: n
- </style>, T/ |4 e' F, |" K7 K2 y
- </head>
5 ^* u/ K' C0 Z; L2 I - <body>
/ x7 M: I# \0 `; l# c, y5 \& Z! m1 d - <form action="form.php" method="get">! |1 t9 n0 N n' B
- 验证码: <img id="img" src="index.php?" onclick="changeCode()"/>
. i/ o9 V" q, L+ F - <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />
) K: A8 ]" f9 R- N r - 请输入验证码: <input name="code" /><br />
7 _- z& M4 s. e7 i1 Z0 P - <input type="submit" value="提交" />7 N \1 j( u, ?4 j/ S, d# e- w- k
- </form>8 c2 I* \7 z: J# Y
- </body>
M; m; [7 M4 Z' g! f( T - <script type="text/javascript">1 b6 |& M D8 v) M( z9 l$ x! Z9 w( [
-
3 X% y: L0 X) P2 m2 I$ ~# T6 S - function changeCode(){
$ V8 ]: v. q; e0 J - var img=document.getElementById('img');0 ]7 M J1 v& T/ I
- //img.src='index.php?r='+Math.random();
( R, C$ E6 ^6 \) ^1 h& A' P - img.setAttribute('src','index.php?r='+Math.random());
: Z" v+ H, b+ g9 Z1 h% f - }
% }9 [2 Y( h6 n! e5 m - </script>
0 v; \! g) ?- `7 q' T - </html>
复制代码 ; M& ~2 P- L6 v/ j7 V
, W* w# }( w8 P6 E, Z |
|