管理员
   
论坛积分
分
威望 点
贡献值 个
金币 枚
|
index.php% Q- z1 r7 o. u3 u/ |
- <?php
9 s. k, |$ d/ r6 a/ B - session_start();% I" _: e: ]1 H7 e1 P
- // 丢弃输出缓冲区的内容 **9 u' i( I6 k, O5 g6 q
- ob_clean();0 x& Q. C Y0 W* q
- header ('Content-Type: image/png');7 ]- F% A6 ?( ?* O) F
- // 1 创建画布8 S. o* _1 I! s3 R4 y! q# Y) K
- $image=imagecreatetruecolor(100, 30);$ z4 B/ M( w' q6 P. x3 w% H/ U
- // 2 画一个矩形并填充背景颜色为白色
- @0 d& B5 _5 i) d - $color=imagecolorallocate($image, 255, 255, 255);
$ e+ I K" ?" I6 C - imagefill($image, 20, 20, $color);( x9 E; }' l+ _& z C4 N: d
- // for($i=0;$i<4;$i++){4 F5 O; f4 R* E9 _/ q$ L* }5 H
- // $font=6;3 @. x2 l+ ] h. k& ?
- // $x=rand(5,10)+$i*100/4;4 \- W; U, G$ ^: e, E/ f: \
- // $y=rand(8, 15);6 H& K+ {9 b# r
- // $string=rand(0, 9);
! E5 J. [2 V) q7 a - // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));8 c) {# s5 Q2 i
- // imagestring($image, $font, $x, $y, $string, $color);
5 E; N) @* c1 x: Z - // }
2 @1 F8 A8 z8 ]. @6 C. K2 A( r - $code='';
; c$ w" A, P8 [+ c. [, Q* | - for($i=0;$i<4;$i++){2 Z1 J& Y) L& V, v) F% {) j
- $fontSize=8;0 W4 y/ ^% y; T T# q# N
- $x=rand(5,10)+$i*100/4;
8 j: M; ~7 n, L - $y=rand(5, 15);
# e, M0 ` _ b. q4 G - $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
+ I9 o7 O% H- @ - $string=substr($data,rand(0, strlen($data)),1);
) c2 X& g7 i' @- G" x* o9 o/ B+ g - $code.=$string;0 Y* ]5 _3 u/ d/ J, x
- // 3.2 加载自定义字体
2 h* _# ]7 j$ v" H% H - $font_file = '1.ttf';0 S' u0 H+ X0 z
- $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));1 o9 D- T* s8 L3 J3 F* m* T& O2 {
- // 把文字填充到画布, E6 G+ h' s3 }- ~8 _
- //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);
6 s. M6 X+ P( f" _9 V6 ]8 M8 Z - imagestring($image, $fontSize, $x, $y, $string, $color);6 r+ F1 w4 R9 D8 |& q6 N
- }. n" Z2 @8 p! T8 f) F3 i. N8 c, B
- $_SESSION['code']=$code;//存储在session里: ~' z' i v" x2 G
- for($i=0;$i<200;$i++){
6 V$ e. `# L- E4 x) ~* I - $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));
( F8 q0 ]) B9 K% Y$ e6 e% K8 ]$ n- w - imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);. M: ~2 m0 k/ u
- }
2 ]/ C& t( r5 x- A7 T, Y - for($i=0;$i<2;$i++){
" ~* I, @2 C. T/ e4 e- i - $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));
7 R6 f b4 R G/ A- o3 q& e - imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);# K" D/ O( U2 K' Y
- }5 }: L1 O/ w2 |
- imagepng($image);
: z2 X @$ \+ m9 j4 h - imagedestroy($image);
! S& }) I/ I8 D$ }4 q" C1 n
& ` k- G$ Z; s1 l0 z5 Q- ?>
复制代码 7 @; ~4 q2 ~& \: U q- S, r
form.php& S3 E s; l, g3 H* h" Z
- <?php) I# [% S8 r$ W
- if(isset($_REQUEST['code'])){
& R8 q0 A$ R% K* X8 L8 t - session_start();1 L! R, n& ~. `5 G7 ~
- if($_REQUEST['code']==$_SESSION['code']){. L* a0 S% ^ h& d K% _/ m
- echo "<font color='red'>输入正确</font>";' f+ {% s# y' D- e/ B0 Q. s
- }else{2 B1 j) [. J2 s$ o
- echo "<font color='red'>输入错误</font>";
* V" x% ~/ w7 W* r% E* y3 a% M - }
1 o4 @( J7 I- W) j+ R% s - }
0 C1 Z# m3 y# X( _! [- I' H, f8 w# A" z; P, h - ?>
& W/ ]5 u2 g/ q/ E: B% d4 X - <!DOCTYPE html>
% x9 q" I2 r% c& B! U% `2 p - <html>* ^: |( l O& P
- <head>
4 ` A7 o3 ~! }4 J! k; F - <meta chartset="UTF-8" />
7 u. ^' E0 o( ]3 e - <style type="text/css" rel="stylesheet">2 _0 ~2 ^& i( h' k4 K
- a{/ Q4 G0 e! @. K+ v; z
- text-decoration:none;
$ `! V2 ?1 z/ P9 Z6 k2 L - font-size:30px;4 x! B9 |$ |3 J. y7 y( V; v9 z& C
- color:blue;
. S% a! X! v( e4 d$ g - } G4 Z, t3 R1 w5 w) K4 r! ^& _
- a:hover{
9 v7 _! Z( O8 A! c4 x - text-decoration:underline;
$ [2 ~- U/ F4 e! K- ` - }
' L4 p: M, E2 w7 e' P l9 d - </style>, {5 w) W( f% y5 C! q
- </head>
6 e" v0 B3 k' }/ Q* t/ X8 p% ~ - <body>9 P- }* r( l$ w/ z/ \/ U" K# [/ t8 O( v
- <form action="form.php" method="get">
$ u5 J) ?; C- ]5 b - 验证码: <img id="img" src="index.php?" onclick="changeCode()"/>
5 [. p9 X' C0 I, J3 L3 C* `5 } - <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />9 i$ |! G; T6 C- k8 ~
- 请输入验证码: <input name="code" /><br />1 i# Y$ t1 Q9 ?* L
- <input type="submit" value="提交" />
) N" v2 l$ O" b* J - </form>
$ @$ l' `" A: s - </body>
1 B. j2 E" b- L) ]8 f - <script type="text/javascript">
4 }+ E, v& K# I% x -
, y# Y$ Q: t. O! f4 Y4 U, j - function changeCode(){
" T! K$ k: l- |5 d7 m* }9 V - var img=document.getElementById('img');7 z' a! X1 R3 x4 p1 k0 d
- //img.src='index.php?r='+Math.random();
4 U( R& x6 A9 o0 o5 h - img.setAttribute('src','index.php?r='+Math.random());/ f0 t, {- z# U
- }
3 a' z7 I! D9 ]1 e - </script>
. W( o& d. c) }! Y1 K, L5 o- r - </html>
复制代码
1 ]" q$ H. B& a V/ i; D# C
" `2 i. h- E, k/ \ |
|