管理员
   
论坛积分
分
威望 点
贡献值 个
金币 枚
|
index.php2 ^% F$ l! V! n/ S5 k
- <?php
" z# Z0 n: y- J. I( H - session_start();9 M2 @1 u4 b1 B' e1 j5 o% S; w
- // 丢弃输出缓冲区的内容 **
\7 b& ~4 q1 b - ob_clean();9 }" Y5 I ]1 L
- header ('Content-Type: image/png');+ G7 ]& {. Y/ `" D0 n
- // 1 创建画布
3 G4 O0 M8 A, o: c2 ` - $image=imagecreatetruecolor(100, 30);
/ w. ^4 y4 v1 H$ p6 @& \ - // 2 画一个矩形并填充背景颜色为白色3 y$ k' \/ x8 |- N
- $color=imagecolorallocate($image, 255, 255, 255);
. t% Y1 s# _, W, @3 i - imagefill($image, 20, 20, $color);
& {6 I8 X/ F, M V - // for($i=0;$i<4;$i++){
8 f$ D5 S: F& G - // $font=6;$ @& Q. E1 ~8 ]; n
- // $x=rand(5,10)+$i*100/4;5 A! P6 r' W, ^0 m- D
- // $y=rand(8, 15);
5 Y0 \2 V1 _& o: h2 d. y - // $string=rand(0, 9);
s9 p" z+ e2 Y- `: d% o9 I - // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));* }& j/ P$ x0 V+ v8 c- _
- // imagestring($image, $font, $x, $y, $string, $color);; t& c) N( U) a) }. S3 y5 x$ h
- // }
( x [' a& T) @5 p: Y3 G1 J0 P z$ g - $code='';* @0 a) w* B7 ~0 C2 ~
- for($i=0;$i<4;$i++){- m1 d+ y @) Z* c1 ^$ s# B
- $fontSize=8;
7 A! b4 R; v$ d/ p( e8 s( w# K - $x=rand(5,10)+$i*100/4;
# I+ C& R6 r( [7 S: {! K - $y=rand(5, 15);
" V; T; \% N3 g; q$ |+ L2 f - $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';4 C5 v# Q% b% X# m- z2 v
- $string=substr($data,rand(0, strlen($data)),1);9 L3 x) [; x5 v7 E
- $code.=$string;% h! u" L6 Q3 Y! U2 Z% }, `
- // 3.2 加载自定义字体
, Z o- V" A5 F5 g, |0 c - $font_file = '1.ttf';
: E! ~6 p- \0 W" q8 H9 \- f - $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));8 z8 h7 f& `* [, `' L2 y, g+ r
- // 把文字填充到画布
8 x$ m: l, ?' ]3 @ - //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);
/ M" {7 F% E$ o - imagestring($image, $fontSize, $x, $y, $string, $color);1 Q8 A" q) Y% g$ u
- }
% O( f3 n- k+ a: K1 |4 a - $_SESSION['code']=$code;//存储在session里! D) `! z. d4 T
- for($i=0;$i<200;$i++){1 E* S3 P* {$ d' u
- $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));+ k. R, U8 t* J; M! m; f
- imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);: @! F @! F" a% C+ V( e7 H
- }
6 H+ l L7 A$ e) G - for($i=0;$i<2;$i++){
* |5 ~% U9 ~* h - $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));
1 K- ^: [2 O3 V: G4 n" q/ d - imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);
3 A9 S* L) n9 s0 u# t - }
: K" P3 F& v% x6 ~ z( Y. X - imagepng($image);* [6 q7 X% |" S0 r. B+ e. c5 z" w
- imagedestroy($image);
% Z2 w0 y( }% t
' B- S: Z. ?9 ?7 i- ?>
复制代码 6 l) I' H! P3 @! |2 \$ N- Q
form.php
4 r) T7 m! X. R/ c' L- <?php- e+ B4 m. j: w w% E- J
- if(isset($_REQUEST['code'])){4 V7 e6 K' b; `$ @% C
- session_start();$ R8 {6 y& N# P. {
- if($_REQUEST['code']==$_SESSION['code']){$ @3 X0 @ B8 }% ]
- echo "<font color='red'>输入正确</font>";. Z; a. g+ f' H/ D" w) Z
- }else{ k! B3 Z8 Q w
- echo "<font color='red'>输入错误</font>";0 R8 D: W/ Q9 s! f2 A `# x
- }6 I& S" ^, i b$ u: X" `
- } N+ o4 F- ?8 ]6 N3 m
- ?>
) M. l4 h6 S5 X% L( z! l# q - <!DOCTYPE html>0 V4 c2 J2 ]0 I5 q
- <html>
* c& v- d- I! i- q- G' {8 }- G" I - <head>
- f, i% Z3 p! E; W - <meta chartset="UTF-8" />+ t: y( O8 l) i, I$ |5 A7 X5 ^
- <style type="text/css" rel="stylesheet">" b" s% H! J+ Y; q8 }, f
- a{& v5 a3 w* v$ p. y+ d0 j# h
- text-decoration:none;3 o& i' t3 A' R- y7 s. c3 ^- \
- font-size:30px;
1 G) W$ O% `1 o8 e( i - color:blue;
5 p3 _8 D; q2 e1 c3 E - }
+ m/ j/ P& V6 ]) Y( h - a:hover{
* Q- d, O2 |4 T1 [, a - text-decoration:underline;9 }- X* @! j( o$ K" _' C; r, e/ K
- }% H+ _+ ?: u4 D. H
- </style>; c+ n" s3 c$ c$ I% e
- </head>9 T9 e' j$ q/ ], n( b
- <body>! l3 m" U6 _1 U4 \ M- W
- <form action="form.php" method="get">
# L7 s; I l S! i, S! s - 验证码: <img id="img" src="index.php?" onclick="changeCode()"/>2 }. L% p3 H4 N
- <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />9 }6 W7 h) F% v$ x6 Y
- 请输入验证码: <input name="code" /><br />" c2 W) }% _% e7 c- e) J3 |! Y
- <input type="submit" value="提交" />+ n1 F! y' o1 w) l6 [6 N
- </form>
" y8 G; P1 F, e! s9 l; {9 ` - </body>. R: i k9 h q5 Q& k r7 t+ U
- <script type="text/javascript"> W+ ~* a9 D1 \& Y' n1 X" V
- - x/ ]( {3 q( y j( j+ Q1 }
- function changeCode(){
$ ~1 K5 `3 S- w* e - var img=document.getElementById('img');7 [3 r5 y. @4 G6 P! |$ ?( t
- //img.src='index.php?r='+Math.random();2 C- q1 V* b5 s' n( }$ q9 K, t z& W
- img.setAttribute('src','index.php?r='+Math.random());3 p0 G0 a+ K2 p- w
- }
4 ~1 G; q$ }: d - </script>
. b& q3 R& J* h1 T5 k0 W) ?, ` - </html>
复制代码
3 {: z+ U8 P0 A
/ h a/ \1 }0 K- I9 \ I' T3 K' }6 } |
|