管理员
   
论坛积分
分
威望 点
贡献值 个
金币 枚
|
index.php
# e4 Y- c1 y/ f( N3 A- <?php+ z( Y( E) ^# @$ {: w
- session_start();
; q) H: ]; q- r/ ?7 h' G( \! ~ - // 丢弃输出缓冲区的内容 **
Z z: I& p# f! u - ob_clean();$ ~) h9 \/ Q+ d1 r8 Z/ A# H
- header ('Content-Type: image/png');
7 S" |$ y, e7 e - // 1 创建画布5 j) N. u4 ?# K, E
- $image=imagecreatetruecolor(100, 30);' S7 D. k5 T1 }. E8 j, U
- // 2 画一个矩形并填充背景颜色为白色
1 n* U Q/ _ b M* z+ ] - $color=imagecolorallocate($image, 255, 255, 255);
$ C7 G+ ]* k% J+ K2 g" o - imagefill($image, 20, 20, $color);
1 B9 H1 J Q* D: m7 V - // for($i=0;$i<4;$i++){
' c5 K/ W2 p! O f+ L; t - // $font=6;
1 ^* q+ N5 c0 U9 q H5 V6 ]. Q - // $x=rand(5,10)+$i*100/4;
6 e: k* k0 X7 m% S - // $y=rand(8, 15);8 }9 f5 E( u! {# N' Z% w* p
- // $string=rand(0, 9);
8 w" [* S9 W5 `* G V0 P - // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));. \3 a. Q6 z3 }# d! \& y6 b9 X
- // imagestring($image, $font, $x, $y, $string, $color);
0 h Y1 `+ J: f4 w' |6 \ I, g - // }/ S6 f2 ~1 D$ I6 l
- $code='';
" i2 Z) w7 g# s - for($i=0;$i<4;$i++){
6 m" W# ~' k+ p! [8 x* Z8 W, [2 M - $fontSize=8;( v; w7 x9 s$ e7 n7 P
- $x=rand(5,10)+$i*100/4;
& r8 ?0 Z% T8 `' Q - $y=rand(5, 15);
% K, T5 u% W2 r8 `. l - $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
' N$ H9 m3 V9 Z - $string=substr($data,rand(0, strlen($data)),1);
3 ?( |6 b8 v" r# A+ K - $code.=$string;
3 t7 {$ n8 ?3 N% u - // 3.2 加载自定义字体; @' k$ D' O0 F* f" p2 X, {5 r! R
- $font_file = '1.ttf';
! r0 M: A# o1 V& q( z7 g* }' Q* R/ Q - $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));1 O' H7 U& X4 f
- // 把文字填充到画布
. U- H; i. p* o% v3 O) o' X7 x - //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);
6 T: ~ b- S7 i2 G - imagestring($image, $fontSize, $x, $y, $string, $color);1 a; Z( [# f5 S1 h
- }
5 J1 i, [) I/ e! X. `, P - $_SESSION['code']=$code;//存储在session里
5 P4 H' Z6 ~) ^. I- \ - for($i=0;$i<200;$i++){. C6 B$ Z; p4 Q) K5 C D- y% ^
- $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));. U0 Q- e5 ^" P9 J
- imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);5 X( u5 w f0 f. z$ O) T
- }* ]% U5 U) t5 Y
- for($i=0;$i<2;$i++){7 f% I% Y( p3 G& Y \( s' e
- $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));$ F* E# m* m* W! _8 }! w4 w% ]# R
- imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);6 k) N) c# ]) d* o& v& [: u
- }
; {" p; |' H2 A5 |3 b6 }1 t - imagepng($image);
t/ `' U( u* o( {1 c/ @8 U - imagedestroy($image);# X6 Y* C5 `# J2 @ C9 T# `7 E: y
# X( I0 Y) H2 Z7 H' f- ?>
复制代码 9 U: q! k% O/ V; F7 G4 F
form.php& U$ x$ H& b" f; y% y
- <?php
8 F5 {& R7 {; R0 @! d, C5 `) [/ r - if(isset($_REQUEST['code'])){* N# f2 J B% P5 S* \6 m
- session_start();/ `, J+ \- f0 D" j
- if($_REQUEST['code']==$_SESSION['code']){& z( m2 `* k4 [8 I' [& ?
- echo "<font color='red'>输入正确</font>";
' R1 M0 l, U$ ]7 M1 j - }else{7 Z) ^* q9 H N% H* H2 e
- echo "<font color='red'>输入错误</font>";' ]% W% L6 P+ K7 g7 b: c3 ^
- }) n% Y6 c( q. R, D
- }7 Z8 l3 j9 a* r5 z6 s
- ?>
- F# S$ o. W3 @! B$ r - <!DOCTYPE html>: V2 E8 b* p/ k, T w, c n6 r
- <html>+ i$ d+ t. F% f: |) j
- <head>
- b" S( Q8 u# L8 J - <meta chartset="UTF-8" />
3 ^2 |+ h4 m' L4 }3 {+ k - <style type="text/css" rel="stylesheet"> i# C( z) K" o$ ? ]
- a{9 q. b9 u) R1 Q: M: W7 v+ y. d
- text-decoration:none;
' e& Q6 Y3 X/ C/ r' ~ - font-size:30px;
' r2 m% o4 j0 h# Y - color:blue;
& t& \: E, o `. K% R4 @0 \ - }
8 l8 X# Z! N5 a, Q0 y - a:hover{: J( x3 x+ \3 f/ k/ K5 p
- text-decoration:underline;6 I7 F/ f- p8 e9 B1 x
- }
% I( A4 d% Y5 U" n' m6 f/ [ - </style>
) Z- ? R& w8 }" q# m( N B - </head>
_# R" h, C/ Q - <body>
h' a+ [" r& ^1 Q - <form action="form.php" method="get">
$ Z" C( t1 J. Q# Z - 验证码: <img id="img" src="index.php?" onclick="changeCode()"/>- b! s8 E5 y `7 e' Z
- <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />
$ I0 i# M4 o; a' F; v - 请输入验证码: <input name="code" /><br />
8 D& N) f/ I- Z% V: I7 E. R - <input type="submit" value="提交" />6 c+ ^) t5 V- Z' ]. u9 m
- </form>
2 y5 f% L* y) {$ [ - </body>+ B3 ~! F, c9 `9 K! h
- <script type="text/javascript">
6 W$ r, ~5 c* M1 y1 ], V% [/ L7 { -
+ g9 n/ p2 U0 Y' i6 C - function changeCode(){
- M0 D. ~( I4 R+ t* g - var img=document.getElementById('img');5 H5 X8 h8 u8 Y: v9 W" w/ k. V
- //img.src='index.php?r='+Math.random();/ F3 B( z- j( G0 X( |, M
- img.setAttribute('src','index.php?r='+Math.random());
3 q* }3 @# a/ ~. u7 ^) ~% m$ a# W { - }5 @ s/ E: N! E6 t2 @, z2 h
- </script>
) I' Z8 C2 C- ]/ H - </html>
复制代码
* B9 G5 o$ s3 f& C; z; _9 I# b2 i* m5 ^7 N$ K
|
|