管理员
   
论坛积分
分
威望 点
贡献值 个
金币 枚
|
index.php3 Y- }- c5 d! Z% |
- <?php# P9 P9 v0 h3 S+ d
- session_start();/ y c' V* N$ U* H9 p3 }
- // 丢弃输出缓冲区的内容 **
& B4 n0 H b+ [7 z: e, u( c - ob_clean();
: @/ h$ l9 \# r& X, x4 M3 a - header ('Content-Type: image/png');& Z n _# @$ e: t: ?: A* }
- // 1 创建画布* v' x/ E. E) ?: Z
- $image=imagecreatetruecolor(100, 30);! Y8 N9 n0 q+ S l! i
- // 2 画一个矩形并填充背景颜色为白色
, p% T$ r( u r" `. m l7 O' R - $color=imagecolorallocate($image, 255, 255, 255);% R6 y7 F- o! U t
- imagefill($image, 20, 20, $color);, j/ L' e3 ~8 N( r) g. V& A f
- // for($i=0;$i<4;$i++){8 p' u; ]& @! E* t8 |! p7 D
- // $font=6;
n" Z W- o v - // $x=rand(5,10)+$i*100/4;
' C% C+ M% q8 Y3 r N - // $y=rand(8, 15);. i+ Y& m$ X4 v( `
- // $string=rand(0, 9);% Z* O+ c- q# f1 z) L: B
- // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));/ j1 |3 b; x2 z( P( B" G
- // imagestring($image, $font, $x, $y, $string, $color);$ \) \6 Q! L6 Z9 c9 }* D- s% i
- // }
( F6 m& z" ^9 \* x7 R - $code='';8 P; ]9 j }2 Z( s4 e
- for($i=0;$i<4;$i++){% @& e5 a% o m: `! y7 z! R4 m' t
- $fontSize=8;: Z' U& A+ ^$ I- u% g
- $x=rand(5,10)+$i*100/4;
: m! J3 Q, }$ e - $y=rand(5, 15);* O/ L6 H! |4 j4 c
- $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
0 q3 g9 l# Z* o8 C$ } M - $string=substr($data,rand(0, strlen($data)),1);, W, `5 R0 J& n' p
- $code.=$string;
. U- v: u* o) N! ^! z2 A' C5 W4 R - // 3.2 加载自定义字体
2 B) |( C: V5 t5 n" R - $font_file = '1.ttf';
# R2 c, y8 t5 w1 [0 I& M - $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));
* b) o/ [% r& [! B' X9 o( ? - // 把文字填充到画布
{( D0 y4 M! _ ?; A$ F - //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);
1 f' M! p9 x' ^. ^& z0 ]( c- w - imagestring($image, $fontSize, $x, $y, $string, $color);
$ X4 ]4 ]: w: p* ]* w6 m. b0 I# \ - }
# R, d y/ J2 `9 d0 p; d- m* u - $_SESSION['code']=$code;//存储在session里8 y8 N- T% G1 T4 Y. Q/ D! |# R2 Q
- for($i=0;$i<200;$i++){
% Y! c; G) k6 {" g, G7 F - $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));
" S/ I0 K* ?: n$ M0 } - imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);
k, b/ ]9 ^+ u. H, k% p5 g* G7 N - }
+ I- Y/ M* s4 `# s, P - for($i=0;$i<2;$i++){$ Z+ _' p/ K' E7 C
- $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));
/ J1 i& m( v N1 k - imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);# D7 r2 {- u8 e' Q7 B; }; T
- }
3 F6 k7 a* G4 e5 M - imagepng($image);
* X: V3 _6 L% g3 N6 Q# z - imagedestroy($image);
% P) T/ O) g9 S - 9 L3 o( e( w; X
- ?>
复制代码 / z* I% H0 S5 A6 v) u
form.php
) b3 J6 o$ V b% c2 K0 d( e# C- <?php1 c0 t" w3 B$ K8 F# o% {2 [; q" G- v
- if(isset($_REQUEST['code'])){# x' x/ k6 x+ m/ m2 }' q2 ]
- session_start();
1 O- ]0 d% R; u - if($_REQUEST['code']==$_SESSION['code']){. ^9 g9 H, E" Z2 R! ?
- echo "<font color='red'>输入正确</font>";
5 }9 |6 J8 t2 l. Q9 _/ f - }else{2 q" U5 \" b' r# j( Q
- echo "<font color='red'>输入错误</font>";
! q& I" v' D! `! X1 {) H% l0 | - }
9 U$ r3 O% h* Q2 W4 h1 X. m - }
* g# x+ ]3 ?" F$ Z* z8 z6 a9 k* H9 A$ q - ?>
, m* r6 `5 h$ @7 E- q - <!DOCTYPE html>- B5 {- l9 }5 u0 U
- <html>: k: @" S" R: c! h5 u$ J: [7 A
- <head>
m$ K9 [2 |2 C9 d/ q - <meta chartset="UTF-8" />
r$ K8 O: t& _( t; @# d. ?) c# q - <style type="text/css" rel="stylesheet">1 h! ~; |+ l5 o7 Y7 A
- a{
# _. M: @+ r0 q4 A: ? - text-decoration:none;
# W) I |7 ^& ~ - font-size:30px;
+ X1 l+ y' l6 ~; w. E: N7 k/ H. e- I - color:blue;
* k W Z" a4 Q. Y - }* }/ f+ ?$ u$ K0 R: o4 n% ]
- a:hover{
; r6 W3 o" k8 {: c7 N - text-decoration:underline;, A$ K1 m1 s# w
- }' N, X8 k" x ?$ w0 E; Y" Q/ @' j
- </style>8 }3 C+ d. U" b
- </head>% ]/ W( b; Q; k4 k
- <body>. \9 R+ `) l6 r% u$ I3 R' b- O
- <form action="form.php" method="get">, _+ i+ q3 r5 M: m; P
- 验证码: <img id="img" src="index.php?" onclick="changeCode()"/>* @3 E- e4 D" G- H. W# H
- <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />
! p( [7 Z( b% K S3 U - 请输入验证码: <input name="code" /><br />
! w4 v" H" [$ g! K - <input type="submit" value="提交" />6 Y/ y4 m1 I2 T4 Z
- </form>
0 D. ~+ o1 `1 d! V \2 i3 | - </body>( [ f, C; j1 u8 F2 Z! E
- <script type="text/javascript">! V) F: t2 G. \# [- E" e, r: X
-
, L$ j4 b! b9 C5 V - function changeCode(){
9 M: Z4 B- K) S. X0 w- ~ - var img=document.getElementById('img');
. O# L6 } x) W. V- Q' I - //img.src='index.php?r='+Math.random();& }0 q( b# x! W
- img.setAttribute('src','index.php?r='+Math.random());
9 {* J Y9 F6 _& n - }; L9 Q# v5 |; t& F8 |
- </script>5 M; M/ D0 F/ Q: L* B
- </html>
复制代码 ) A$ J) X' K, D; q/ v g; X
, r+ A- g {4 R( H
|
|