管理员
   
论坛积分
分
威望 点
贡献值 个
金币 枚
|
index.php
{- D; F! B7 M3 k. U; [- <?php; ?/ l; E N. Q: s# i
- session_start();
" `7 P7 W8 T% u _: ~ - // 丢弃输出缓冲区的内容 **
2 Z9 ]; v `9 R - ob_clean();; t* P6 D7 N7 t. o& C5 N: \! w
- header ('Content-Type: image/png');
/ g) r* Q& u4 N! |5 ^7 n; e7 j - // 1 创建画布
, ?8 R- p4 P8 |6 L - $image=imagecreatetruecolor(100, 30);
2 Y/ [1 F+ [) M3 ` - // 2 画一个矩形并填充背景颜色为白色
; C6 y% ~& t5 i% q, r, p4 f2 K$ C - $color=imagecolorallocate($image, 255, 255, 255);% v$ B6 }. \! j+ Z- c- t
- imagefill($image, 20, 20, $color);) h' B! e8 s( e3 \6 _: p# y
- // for($i=0;$i<4;$i++){
1 n8 Q; s4 y/ j. \( i4 ? - // $font=6;0 P" I% v! ?0 I; A7 M
- // $x=rand(5,10)+$i*100/4;
1 F4 y- P3 i0 H - // $y=rand(8, 15);* y6 A4 d q, ?4 t
- // $string=rand(0, 9);4 ~* {5 h, [9 h/ g0 m
- // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));
4 t, C' q$ a8 q# \ - // imagestring($image, $font, $x, $y, $string, $color);
5 F7 `% n9 i/ W6 {* Y7 ` - // }
M) K; e4 F4 N# K - $code='';# z" }' J/ a7 H) j
- for($i=0;$i<4;$i++){
/ C8 {) b4 p, m - $fontSize=8;; ]; \4 g* `" h
- $x=rand(5,10)+$i*100/4;* T3 N, `2 f5 k: \2 e
- $y=rand(5, 15);* L S2 @% @ C' R+ L
- $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
# |$ f) R) d, I! _ g/ @+ Q - $string=substr($data,rand(0, strlen($data)),1);
' E4 r, `9 l5 t C% Y& J4 ]- | - $code.=$string;
1 v% B# c4 N/ [, f - // 3.2 加载自定义字体9 A8 @; Z5 }0 p* V) V
- $font_file = '1.ttf';
( F3 r& Y/ ] U V - $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));
4 ~4 P0 C. Y' g+ { C- l0 H5 ?6 Z - // 把文字填充到画布
- {3 W, z9 l( f! e: y - //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);+ V1 ?" h# K9 J, J* {8 r+ W5 j& a! ^
- imagestring($image, $fontSize, $x, $y, $string, $color);& T% F& Q0 V7 n8 E- d
- }2 r' P- F+ b9 \: @! N7 C: z1 @
- $_SESSION['code']=$code;//存储在session里
- n2 C* j, R9 M9 B5 o% g- b/ j - for($i=0;$i<200;$i++){4 b% @ ?- T+ ]/ j6 _
- $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));1 _( N- q3 `* s9 ^" S
- imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);' H- u5 U6 v7 D( T* f
- }! ~5 Q3 |; ]) o" r; s; H
- for($i=0;$i<2;$i++){
: i$ P) f% @5 z: n, v" d- g8 O2 a - $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));: Q+ G% ^1 K, j6 ~7 u+ ?5 N5 w
- imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);
$ ^0 N: B- {6 Y$ _, J) } - }& C& G: X8 J: Z/ r" J3 U
- imagepng($image);/ b2 z) r7 Q) L& k* `) U/ p- ] _
- imagedestroy($image);
& q/ g* j6 X! P$ ^8 J1 a
5 W! f1 K, P0 h# F* I+ d- ?>
复制代码 0 J5 v# A/ r& a7 K
form.php. A. _$ M: S8 b" a2 e; T
- <?php9 m# _8 z1 z2 n+ e; T
- if(isset($_REQUEST['code'])){' w! N* x/ N4 l `* B' d
- session_start();% O* n* O" e1 k% [% w
- if($_REQUEST['code']==$_SESSION['code']){
3 ~7 v5 l4 s9 _9 D; d9 h - echo "<font color='red'>输入正确</font>";
8 L! }: V& d0 E) c - }else{) _4 X0 T4 u% o# @8 T% [0 d( s
- echo "<font color='red'>输入错误</font>";4 {' t* p7 C" W' W/ G' O+ r* K
- }
0 S* O# h. b. G* J8 j# ]4 `0 _ - }2 ]% y- b8 F( N4 V% m& _% f
- ?>
2 ~* O" y5 b0 [, e+ i - <!DOCTYPE html>$ t* P8 O+ i2 q1 F$ ^
- <html>
9 G) a0 P, S3 w# O - <head>9 G& A' C* `" x$ |
- <meta chartset="UTF-8" />2 N7 p2 n$ ~' k; f
- <style type="text/css" rel="stylesheet">0 r5 K& X. K+ @4 q. O
- a{
5 Q! r3 J4 L4 s h - text-decoration:none; [$ o6 F" }# ~& g: `0 x4 L% I- o
- font-size:30px;# ^7 T6 y; j/ i5 I
- color:blue;
3 Y: Z; |* w% q3 T; O - }
. m1 M! k3 r' w9 x/ \! Y' f - a:hover{* X( v1 e1 S0 _& |4 b
- text-decoration:underline;1 R2 _) p. d8 ^4 Y/ Z e' [
- }
5 o" r2 v7 L. ?' ?1 D7 ] - </style>
* E4 K4 [' ^( C& Q2 }/ @( e5 Y3 V - </head>
* i, @7 Z5 ]3 `! M9 K& U: z - <body>
5 M; u6 h3 \5 Q/ a - <form action="form.php" method="get">
3 g" o! c& `3 n6 h& t2 v) v$ a - 验证码: <img id="img" src="index.php?" onclick="changeCode()"/>
2 n! }. ~. A# Q ~! R8 D3 X - <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />
/ M, B- g. w/ I, k' ? - 请输入验证码: <input name="code" /><br />- Y# B* P$ }' a; H2 F
- <input type="submit" value="提交" />
* t1 d1 y) K. ~% u - </form>
6 w ^1 D) B. @0 @8 {) k; c - </body>
) R' D% B3 W! n! f+ S) z" ^ - <script type="text/javascript">9 f2 I- V' o1 L' L( W7 R4 D+ j
-
' ]( G. @& F. z; G7 ]( Y - function changeCode(){
; i2 B4 r& n/ M! ~9 U, X - var img=document.getElementById('img');
% O5 `/ S: w# u" y- c - //img.src='index.php?r='+Math.random();# n, U( R7 }) C/ |
- img.setAttribute('src','index.php?r='+Math.random());" h4 r% S" \. Q1 `6 ], o0 Y. Q
- }, p: o7 y0 O4 ~, \. K
- </script>
# z" V; V1 V i% h; f a - </html>
复制代码
' x; l7 ]$ ?) f" O1 x9 J: C+ `9 o- C2 ^+ e
|
|