管理员
   
论坛积分
分
威望 点
贡献值 个
金币 枚
|
index.php
/ t4 `- {* o4 g2 F8 u/ t1 C9 d- <?php9 O1 _" Y1 w; U r& @
- session_start();& \) B' l9 \( B5 s
- // 丢弃输出缓冲区的内容 **
' T5 O6 Q @5 y( s- j - ob_clean(); ^4 f e9 D M5 u# s- k; ?
- header ('Content-Type: image/png');
* Y& n* R, P a( u - // 1 创建画布
2 ~9 } ~6 W& x+ [/ G2 H - $image=imagecreatetruecolor(100, 30);
- I. C6 p* `* }( z$ w - // 2 画一个矩形并填充背景颜色为白色7 P! l5 U. \: y- i
- $color=imagecolorallocate($image, 255, 255, 255);8 ^; }; G: L, ?+ c2 Y! l7 ^
- imagefill($image, 20, 20, $color);
- S7 T+ G+ O% d( D - // for($i=0;$i<4;$i++){8 n. M! r: J. t& G! s' [+ f+ P
- // $font=6;
* l1 W. p" A: X8 s - // $x=rand(5,10)+$i*100/4;
3 V7 ~% W8 R" w5 [' C' L; A - // $y=rand(8, 15);+ l( x v7 @0 s3 r. i' z
- // $string=rand(0, 9);
- D, e" |% _4 i$ [% P7 s4 J6 N/ r( ` - // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));1 G0 F7 o3 o1 O2 H( d; N$ E
- // imagestring($image, $font, $x, $y, $string, $color);
) d$ }/ l: h+ c/ |! I# u - // }
9 n7 O+ i5 f' b3 a - $code='';
7 r' O2 j" z# B8 Q3 a. H- j% @ - for($i=0;$i<4;$i++){
% y0 `2 s9 t/ w8 U H. J; _' M - $fontSize=8;
, C; p$ }" x* T7 w) Y7 |, P - $x=rand(5,10)+$i*100/4;
5 n+ m( i6 H5 f" R* U" I - $y=rand(5, 15);
! ]/ g- U; E1 C/ L" e. r1 _9 H3 F9 {$ S - $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
, L& X0 G/ G- _+ A! l - $string=substr($data,rand(0, strlen($data)),1);
) j6 K( `( c" [$ @$ }& G - $code.=$string;1 {8 ]& `2 }, A8 v4 w
- // 3.2 加载自定义字体9 H1 X) \2 j K- _' E- Z
- $font_file = '1.ttf';+ X. [& Q$ A1 X5 a- n8 j+ k N# M/ n
- $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));$ l* }. R1 H( `; V
- // 把文字填充到画布8 A' d& ^7 r, r- j9 x7 S
- //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);$ I9 t0 @! p) Y7 N) w
- imagestring($image, $fontSize, $x, $y, $string, $color);) J5 K3 C, m* E5 Y. O6 J6 z
- }8 t1 }1 S; W, V
- $_SESSION['code']=$code;//存储在session里
# K2 I' j6 {# N( G' L- p' ?& T - for($i=0;$i<200;$i++){' W' p- _+ @' {4 @; E0 v! t2 b/ A
- $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));! F9 S. R' ^! H4 \
- imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);" j/ U. J9 K3 t1 a% T% j
- }1 |# r* s9 Y; [/ G: w4 H8 ~
- for($i=0;$i<2;$i++){
$ M; A9 E- D7 [6 r - $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));
- v. y& X0 C9 y3 K5 R - imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);. \ ~( A. w8 O. y7 `$ h- b
- }
5 g1 V, \0 X/ v- c B9 a: {4 V; s - imagepng($image);/ N1 I3 {+ s$ }
- imagedestroy($image);
* J7 E K% U& g; M8 B9 D. x- D0 t
. l) V4 _+ L3 z% |4 h8 i- ?>
复制代码
D% a5 S; m2 r% f: I5 jform.php; y: Y7 i2 w4 L& h
- <?php
" f. \! s2 T1 H4 t - if(isset($_REQUEST['code'])){! I& f% s% ]# O6 F& ~ h
- session_start();
2 ]6 Z& v3 O8 q: r - if($_REQUEST['code']==$_SESSION['code']){* `0 e% ^+ \$ Q i
- echo "<font color='red'>输入正确</font>";7 J8 z! x" y$ ^
- }else{
% r9 ^) d$ Z- O$ F - echo "<font color='red'>输入错误</font>";
( e6 \; r1 _; ]3 \: a0 n - }
( K1 b' z# y7 z) j: o8 Q* e - }
; x- U N9 R, _. j) z5 K# t - ?>- N6 a8 B0 h3 [1 F5 M4 P# m4 }
- <!DOCTYPE html>
0 A2 d6 K' H7 \$ x: e, } - <html>
! [# [" n$ X- {9 \$ y4 ` O - <head>$ B5 O: L: ?+ e7 g" N% ^0 f
- <meta chartset="UTF-8" />
. I3 y, ^3 j H: L - <style type="text/css" rel="stylesheet">
7 q" N" E( N. R' u0 Q" D - a{
+ C4 W3 _) c1 S" P8 v - text-decoration:none;+ x5 A2 v8 ]1 P3 s4 S: F: L2 W v1 G
- font-size:30px;6 P* ^& G9 G: F
- color:blue;9 O3 q5 r O b% G" r9 r
- }
/ B9 j' V* }: S# J. \ - a:hover{
& a; Q3 c" ~4 S+ P - text-decoration:underline;9 u1 a$ ` O2 X8 c( z
- }9 E, s" C7 y* G1 q% t' I% K: A1 I, t/ c
- </style>8 J/ j+ u) Z# B
- </head>
/ c c+ q) |. n) `4 \! @4 f- A - <body>
" n. u5 H& _$ E2 ^$ O. E - <form action="form.php" method="get">
\. O' S. J9 K. J - 验证码: <img id="img" src="index.php?" onclick="changeCode()"/>( `# d$ J; H+ b+ e
- <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br /># j. J2 W) y) S# L+ B+ P, p; C
- 请输入验证码: <input name="code" /><br />
7 J, P) j8 a: h7 q% G - <input type="submit" value="提交" />
8 F5 y( H# z* l. q& o, r/ E/ f* Q - </form>6 [! f8 k9 I1 ? J1 L
- </body>
h) j: {9 g2 O0 O6 K4 y - <script type="text/javascript">! V) H& C- u4 p- K. Z
- . @: R3 b5 l% t
- function changeCode(){5 b' s- K9 g& [( K7 A0 l# p, z
- var img=document.getElementById('img');
0 Y$ W3 j1 A; ?: L o - //img.src='index.php?r='+Math.random();
: \1 o' C/ o4 ?% l# w5 n - img.setAttribute('src','index.php?r='+Math.random());
; d, v" u- o- Q* g - }
& p0 j+ ?% J9 B, }" ^ - </script>+ E% ?+ F2 e! q5 |4 L
- </html>
复制代码 2 d6 a0 ]/ L$ r4 l0 D* o
' G. V1 |6 k; P4 j( |7 t |
|