管理员
   
论坛积分
分
威望 点
贡献值 个
金币 枚
|
index.php' a: s5 m: Z+ e1 e I
- <?php
* ]7 k- r* z8 j7 v3 S$ j - session_start();
E9 b9 S4 P" V- H" u - // 丢弃输出缓冲区的内容 **7 B, N; |; `+ c& N1 c3 M, s' D
- ob_clean();
5 {% ^' k# T1 C1 E - header ('Content-Type: image/png');4 X+ d: v2 u9 I5 c6 T
- // 1 创建画布
+ c6 l ? C8 L% ?& F - $image=imagecreatetruecolor(100, 30);9 y6 ^# K" s: ? W+ M# j* y
- // 2 画一个矩形并填充背景颜色为白色
5 a+ Y9 o" F+ y7 p2 h3 ? - $color=imagecolorallocate($image, 255, 255, 255);
# }4 w' q6 N {; d - imagefill($image, 20, 20, $color);
) X9 T7 P! [1 ?/ z: R4 y* k/ p - // for($i=0;$i<4;$i++){: v9 a' @2 j8 q& b/ x: H- u6 ]
- // $font=6;
. ?% S6 G, ^5 B& A - // $x=rand(5,10)+$i*100/4;: {. F' k+ ^- b) V; @
- // $y=rand(8, 15);- m" s. j6 ]- e9 O3 i
- // $string=rand(0, 9);
7 F1 p4 Y" j. I, A7 f8 w' L - // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));
# F$ g: ?: m+ L( O e - // imagestring($image, $font, $x, $y, $string, $color);
& \) s9 {; V! B6 {7 ] - // }
. f7 G, Y. t, T6 H - $code='';
0 n: ^, p; h4 e# |# s s# {3 p E - for($i=0;$i<4;$i++){
9 _5 x! B) _: Z$ Y - $fontSize=8;3 [) o' V2 J# L L9 ]/ G
- $x=rand(5,10)+$i*100/4;# i2 d! f! X! ?% y8 l
- $y=rand(5, 15);+ J9 J- {' q* [6 ^# K
- $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
( ?. n. |# P5 k7 c. g0 f5 ?2 g - $string=substr($data,rand(0, strlen($data)),1);' o2 v2 p. k; t
- $code.=$string;
" P2 s% M1 l! i# l6 W - // 3.2 加载自定义字体- ?" f1 P' ^6 N' q8 q
- $font_file = '1.ttf';
: k. h; [ O3 P7 _" u p - $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));- Q6 j3 W- @, ~8 E; ~
- // 把文字填充到画布2 [6 P& E! Y2 X6 B+ V t5 J; ?9 x
- //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);+ z X) S7 j" T/ J6 Q$ v. ?% J
- imagestring($image, $fontSize, $x, $y, $string, $color);6 e' R8 { E. h, r! D$ ^& r
- }% u2 F/ g/ V7 ~) G
- $_SESSION['code']=$code;//存储在session里" q) @) p5 _- ^) @
- for($i=0;$i<200;$i++){
* u# u9 L9 @ F3 a- h' P U - $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));4 U, ?0 }; ?& B p8 A
- imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);
" A0 f0 N% w! A0 Q) Z! J0 v% M - }
. {, h6 K& @9 ^ - for($i=0;$i<2;$i++){& Z* s1 e6 @- Z% g* w+ J( ~
- $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));
1 Y4 {4 x- r' s - imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);
$ _6 I( x& ?+ v1 n, J; k, j( n - }
4 j: A; s+ g1 ` - imagepng($image);
6 Y" Y7 D$ q$ b0 v, @7 K* i8 [& M9 ~( M - imagedestroy($image);
# J. w& a9 E. G8 n c - & A. f3 V; j# {- ?8 j) L0 h
- ?>
复制代码
# u+ w! q+ d! e* q- Xform.php' U2 t/ Y0 }* W$ H
- <?php9 B, M$ w) b1 v3 C; ]
- if(isset($_REQUEST['code'])){
8 J' e& N" s5 K/ V% h - session_start();7 I8 M7 h8 R8 p# w1 I& m
- if($_REQUEST['code']==$_SESSION['code']){( s% ` z5 K) ~
- echo "<font color='red'>输入正确</font>";
+ N7 M/ w0 \3 p" |1 E7 N9 n6 N/ b - }else{9 P: Y! q, e( K2 A8 A: |8 O
- echo "<font color='red'>输入错误</font>";
8 o& Y. @5 @4 K D - }$ h/ E, w& M+ Q; U
- }! z; k! @8 C( {5 }7 i7 q
- ?>( d! J* z2 s5 o' B+ i( N7 n
- <!DOCTYPE html>. l- m1 t# W8 `/ N4 C4 N
- <html>6 P' t& _( i; v
- <head>
& |# I8 Q$ T5 f - <meta chartset="UTF-8" />
2 o; P! j2 P1 Y - <style type="text/css" rel="stylesheet">
+ K; w7 d0 f/ L# M: g2 ?8 M - a{
: a. ~7 N% q6 t8 A - text-decoration:none;
0 l: H* H% S. z' f L% p - font-size:30px;9 g* N, Y( u5 e- \5 z1 R% L$ j
- color:blue;
8 I/ d, b7 X! x! s - }
. ?# j6 `: o6 p* ]0 l9 T- g& m - a:hover{
' m, V4 L6 `0 j$ \3 ] - text-decoration:underline;
, Z- u9 g$ g4 l - }6 l/ h1 B+ m& w) ^4 W
- </style>
' A" o4 e/ P" f$ o: K8 o" ` - </head>
" ?. k. s! \- D6 b/ o. F; n: x - <body>
( h3 g7 U1 D3 q0 h5 G. p& S - <form action="form.php" method="get">: y3 B) C8 ]2 t: b/ V' f
- 验证码: <img id="img" src="index.php?" onclick="changeCode()"/>
: [) v" t* Q3 b' W% q- B - <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />
, T& b0 Y9 x9 y - 请输入验证码: <input name="code" /><br />7 J: e4 [. d+ H' k7 q
- <input type="submit" value="提交" />
! T) ]: z9 m4 [0 Q' T J, a& _ - </form>, G3 V1 [) ^5 W4 N, e# {5 S, X
- </body>
& K! o% T# _' l: T$ P' B4 L - <script type="text/javascript">' p& D/ l+ }0 u8 I+ n0 f0 [
-
8 ~7 S" i1 O* h$ q- I& X - function changeCode(){2 h# w6 i; J/ |5 Q1 A; A6 P
- var img=document.getElementById('img');
1 i9 S" J9 C( V. j% a9 I" q - //img.src='index.php?r='+Math.random();
7 K$ K; a; u0 M! C1 G - img.setAttribute('src','index.php?r='+Math.random());
3 R/ t" K Z3 E% v! F3 _: N: p6 y - }
) G5 K( a# a. }5 r5 C* ^ - </script>6 x+ m3 \. E% v: J3 c
- </html>
复制代码 3 m: t+ Q7 g4 `7 E( `0 g
+ T( C! I# z# J8 w) o3 B+ B$ T+ a# i
|
|