管理员
   
论坛积分
分
威望 点
贡献值 个
金币 枚
|
index.php. j1 d2 [1 w/ W1 s$ i: J
- <?php
; b4 F6 R" \: l- y - session_start();. Y1 p. r. d Q, m
- // 丢弃输出缓冲区的内容 ** D5 j" [: j% v$ G! _4 b: l& Q
- ob_clean();
. g; X3 C1 f: ]- N- Z9 @" P5 @( Q - header ('Content-Type: image/png');% ?2 r* F; g9 p2 u* q
- // 1 创建画布
/ `. F# F" e( ?* k) D. h - $image=imagecreatetruecolor(100, 30);
* K3 @* u( |1 @7 w1 B% ~2 a - // 2 画一个矩形并填充背景颜色为白色
- S8 m2 E. s' }/ v2 p - $color=imagecolorallocate($image, 255, 255, 255);
1 k, ?+ {$ U! F$ V) n( [ - imagefill($image, 20, 20, $color);+ Z3 a- F1 f7 x" f% b
- // for($i=0;$i<4;$i++){
0 Q6 a7 f& }/ }2 t. I# Z- m0 e - // $font=6;
1 p; {- ]' g/ B% G1 L0 K; n8 m1 ?- L - // $x=rand(5,10)+$i*100/4;
' U9 k) Q) c2 ?( w) `" r4 m3 ?3 z - // $y=rand(8, 15);
, a! U2 ]4 h- U% _ - // $string=rand(0, 9);; B# L9 N- I8 I$ u8 a. `
- // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));1 L0 a% S, i0 W( S
- // imagestring($image, $font, $x, $y, $string, $color);! X$ S: p# K; Q
- // }
( F1 _* z v- c2 J% X - $code='';; c& M& |+ P% O4 y
- for($i=0;$i<4;$i++){
D% ~6 C4 M! T( Q; f# c - $fontSize=8;4 o. m C/ P' K
- $x=rand(5,10)+$i*100/4;
8 a, x9 S3 ?* z6 `. M - $y=rand(5, 15);
# w2 U8 p& e/ t& x) A - $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';' \' M+ l4 T; f* g5 j9 I j z
- $string=substr($data,rand(0, strlen($data)),1);
; N. E4 `/ \0 \4 {2 X: R" p- e8 G - $code.=$string;
0 o: l7 z& N/ j6 l2 Q+ S, n9 h - // 3.2 加载自定义字体
! G( V+ n1 M$ N/ g0 z% E2 } - $font_file = '1.ttf';
; ~ w" A. E5 o( W' k0 P - $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));
% m/ s( F) M! h3 y5 [ - // 把文字填充到画布
/ G# o5 t; S, y; e' R5 i) _" h. q - //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);
# f' k9 o9 O% S) s6 Z - imagestring($image, $fontSize, $x, $y, $string, $color);
5 ?$ {* f$ Y4 E; l9 [: G - }
9 a% K% @" c! D) ] - $_SESSION['code']=$code;//存储在session里9 J/ g4 T. n% z8 E, L, q
- for($i=0;$i<200;$i++){5 B# S; {7 j' `) o
- $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));
) l, w! K+ a* F _8 g; U, R - imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);/ c: ?5 V8 g+ u1 a- Q: }
- }
^2 C$ I( i3 d+ S0 e6 H - for($i=0;$i<2;$i++){( V) i. M, X, n$ [1 z
- $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));' ^: c9 o2 d' Z# _
- imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);/ N+ w% @7 v" u; I, n3 I
- }
1 l& V4 i$ l* K6 w j% U7 x* X - imagepng($image);
" M9 V+ A- B! b3 ]6 u! o( | - imagedestroy($image);
; v/ d, g) x- j! s
; s' ?! X1 w) o2 ]7 p- [- ?>
复制代码 , {& j5 ]& S! i1 ]
form.php
% t( W8 g' h5 p. P* U: _- <?php
; ^9 ^4 i6 K# p, z. i& Q - if(isset($_REQUEST['code'])){0 r; T5 d+ W4 d4 [3 o
- session_start();" B: O7 M, S, `( B7 S9 F; b+ M; g) H0 l
- if($_REQUEST['code']==$_SESSION['code']){, }1 ~6 x8 q+ }2 D" ^% ^
- echo "<font color='red'>输入正确</font>";8 d* ?$ Y) Z2 J0 q2 X0 C
- }else{& t: W# G' i; ~
- echo "<font color='red'>输入错误</font>";
" ?1 z9 }/ P9 F& V7 B! Q0 w - }" w8 m5 W4 b) o7 i
- }
5 O# E0 j9 q; Y/ I4 C% j$ M - ?>
6 ~5 F4 a( U# |9 x& S4 t1 ?0 a( v2 m1 R - <!DOCTYPE html>
4 E: U+ u/ h( }+ [8 { - <html>0 v" U7 G, p$ S/ T3 q; g# q' w
- <head>
4 s j' b% T: W- {. ~ - <meta chartset="UTF-8" />2 X0 v; e. O. z6 y
- <style type="text/css" rel="stylesheet">
! f* |' F: I; v, B - a{4 p: G8 \% n" d; W0 Z
- text-decoration:none;
" \/ D2 O9 b7 D# ], s - font-size:30px;/ l8 N3 n U9 x) `
- color:blue;
9 |6 R8 S( X; Q8 M - }5 }) D% ~4 p5 k4 R8 J
- a:hover{
- e+ Y: @- D" O/ a2 ], ]4 q - text-decoration:underline;
z' U" I0 z- M& V9 w; [' L - }
! D8 E) Q% P+ X7 c$ v - </style>( R0 i" x( \. s
- </head>: }! I3 D# }4 M6 Q
- <body>- K* A& d3 d; n S: {! Y8 f5 R$ J3 s) Q
- <form action="form.php" method="get">
" G3 }/ s4 s) g( {5 l7 Y) | - 验证码: <img id="img" src="index.php?" onclick="changeCode()"/>
# R/ b( s* s' ]5 z3 p4 K - <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />
. c# s& T3 T5 U$ b5 ` - 请输入验证码: <input name="code" /><br />
* f8 ]1 N/ L& N7 ^* Y5 e; b7 w+ h - <input type="submit" value="提交" />
2 c$ ?; ^- b0 c3 X1 p - </form>3 u0 x% i/ d! |" h, v- S; x
- </body>2 L5 G) k: w+ }" A* l2 K
- <script type="text/javascript">" z6 i9 n% m" Z2 y6 ?
-
5 A- Y" ]" O: N% ~% H - function changeCode(){1 B$ O" f: o6 R2 w5 R+ ]4 D8 V8 Y2 E2 {
- var img=document.getElementById('img');# y/ I7 |' g& v4 r6 h" {, T/ g: |. y
- //img.src='index.php?r='+Math.random();8 f a; S$ e$ f% j+ j. X% n, ^
- img.setAttribute('src','index.php?r='+Math.random());
# ]8 R, s" o5 [, y - }# e8 c2 w0 U" G$ h. ^! `3 \: \8 I
- </script>- X9 k7 w/ w, o4 p
- </html>
复制代码 : T) {% \5 e) \8 H
- D$ j7 i- ?8 f( m! O |
|