管理员
   
论坛积分
分
威望 点
贡献值 个
金币 枚
|
index.php
- R( j/ S% E& n+ W. k K8 O3 F- <?php
9 o4 s: t" F9 q2 Y0 S - session_start();6 ~* [: h4 D# J
- // 丢弃输出缓冲区的内容 **
~2 G9 {, M! G - ob_clean();
' a; T1 M; |8 z, C2 a4 p3 E- t - header ('Content-Type: image/png');5 C$ g, q/ L$ @! @: f1 c
- // 1 创建画布
( F/ r# c- x. ^* Y' l4 F9 Y: Y - $image=imagecreatetruecolor(100, 30);% I) }. n R0 i9 H F
- // 2 画一个矩形并填充背景颜色为白色
, R9 K$ G2 |* p+ [7 } - $color=imagecolorallocate($image, 255, 255, 255);$ u0 j5 z4 F0 Z$ M( X
- imagefill($image, 20, 20, $color);9 U. k# ]7 H, v7 F2 ^
- // for($i=0;$i<4;$i++){' C7 H8 I! t6 Z4 R8 S& q* ^
- // $font=6;
1 T$ d4 T* M, m* h* R - // $x=rand(5,10)+$i*100/4;* B# Y) u, P. l
- // $y=rand(8, 15);. T4 P3 \. {0 D$ j: U& _9 F1 i
- // $string=rand(0, 9);7 ?. q5 z6 V. L1 X
- // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));) [% v o9 V" w0 c% k: Z7 h4 U
- // imagestring($image, $font, $x, $y, $string, $color);
+ }$ B4 u& F9 r - // }
6 r: `& S) C. H2 X1 M# H. D3 T - $code='';
) F+ y8 g _- s& g! D) e5 z - for($i=0;$i<4;$i++){. y1 `$ x8 d' c/ ^) V, p: w
- $fontSize=8;, j3 H0 U1 G( ^; K/ u
- $x=rand(5,10)+$i*100/4;! B. \& L5 \* L% M8 P' ]" ]+ g8 r; R: p
- $y=rand(5, 15);
: Q2 b! @' m9 u+ m" r+ i* ? - $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';) _/ G. R) S, e0 H+ t
- $string=substr($data,rand(0, strlen($data)),1);) J/ H( H T; J0 H9 ~- O
- $code.=$string;
) Z* @/ P. P7 k$ \ w - // 3.2 加载自定义字体
$ c& j8 H& w, a - $font_file = '1.ttf';. y4 V+ N/ i1 K( P" ~
- $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));0 A" g, |; r% K2 P" `% ~
- // 把文字填充到画布8 ^* c. ]& ?9 n! W# a* X
- //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);6 }' e4 q9 z6 R6 f1 v# O
- imagestring($image, $fontSize, $x, $y, $string, $color);
, k' `1 X6 ?, j" N4 e - }2 z% F4 P/ p8 A/ ]( o
- $_SESSION['code']=$code;//存储在session里4 F! y6 i& g: [9 @' j3 u4 k* N
- for($i=0;$i<200;$i++){
6 f+ b3 f4 C, P) B - $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));
8 v) O# ^3 t6 c& x+ Y! y: x - imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);
0 o \6 G1 n* k" ^, p$ j - }- J) r4 _9 q' H5 y- F3 p6 O
- for($i=0;$i<2;$i++){
- G& w5 s: y" p; s3 y' A' c5 p - $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));" S9 a! {" u7 S' ~, T/ H( s1 v! W2 c* B$ n
- imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);
" k |4 z: u( \5 Y - }
# I6 A/ O( c4 F0 Q7 i7 I - imagepng($image);
4 M0 h/ R6 g- x5 ^2 M3 p - imagedestroy($image);( z+ Y) B9 `/ E' H) l/ ^
- * `! E# F7 r3 _; S# Y$ Z) `
- ?>
复制代码 ' c: e: N8 }" I; f/ V0 A$ H
form.php* O, o) n9 o/ A5 t ?9 L$ k2 m3 N
- <?php
0 e. S/ G& |, x, E6 v$ H# ^ - if(isset($_REQUEST['code'])){
0 L& l* v4 P W/ u: i3 d- h) W: V - session_start();
+ L! D* c* ^9 N9 R& y3 O$ l - if($_REQUEST['code']==$_SESSION['code']){
2 ^7 K- z( w5 B/ n+ _) C - echo "<font color='red'>输入正确</font>";
1 q( Z" ~; p+ ]) a( H3 M4 x. B - }else{
# n0 E4 x P: Z Y - echo "<font color='red'>输入错误</font>";
+ l+ h ] H7 X: X' H - }
" E6 z1 y) G0 G$ v: {" N8 } - }8 u* m2 l, v+ g% `
- ?>
3 H2 @* l9 C6 ^4 V - <!DOCTYPE html>
2 O1 B& }3 N3 v9 W9 o - <html>' E) q4 F0 O& `
- <head>
7 V3 r/ Q5 Y$ F; L1 B) E - <meta chartset="UTF-8" />- V- q: u0 {# Q a- ], S( D0 t" D
- <style type="text/css" rel="stylesheet"># ~. t) ~7 L& r8 o
- a{, R/ O' j3 q" r1 h, h, `9 J
- text-decoration:none;6 }' j/ i; ~6 U2 \. Y6 D' h4 u' M
- font-size:30px;
; b9 `) w" b$ B# m& Q - color:blue;3 X! l( G l4 c4 E
- }2 w0 G7 Q! }. M; U/ R! H
- a:hover{, y: E4 o _. q( C4 M7 e
- text-decoration:underline;
+ |$ @- e }2 E; ]; n4 g0 [( p - }
9 ?' o9 d" I( f - </style>1 |" Q% X8 e' {: a
- </head>
! f/ Q' |& ^4 H* I4 S7 c0 ] - <body>
* [* B) K- `' M8 |: b( ~1 Y/ J - <form action="form.php" method="get">
- z$ w' C2 W* k! c - 验证码: <img id="img" src="index.php?" onclick="changeCode()"/>- L! E1 e R" {' v: g# p
- <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />9 R: g7 W( Z% D% J7 R) c8 K
- 请输入验证码: <input name="code" /><br />
" c1 h! w9 o( k' b% w% F - <input type="submit" value="提交" />
9 J" C* \! Q; C4 i; Z1 B - </form>
- Z$ O2 S/ n- C - </body>, @; O9 t/ \) M" ~: `- E0 W! _3 o
- <script type="text/javascript">
4 I9 R7 _: J, b: [4 U4 N% m) F. O8 Y - ( G: {) r. ?- I2 |
- function changeCode(){3 L1 ]/ u7 W8 M
- var img=document.getElementById('img');9 {0 w; t" C7 Q3 u" }
- //img.src='index.php?r='+Math.random();7 T. a z9 P, M! j/ F3 L$ E
- img.setAttribute('src','index.php?r='+Math.random());
3 \/ u' m* c3 ^7 j - }
# n0 [/ `; |( l - </script>
% F6 C$ H' a5 F4 w - </html>
复制代码
! z. e2 B" S: p; `, y D% M! |. |) ?4 m4 _& F
|
|