管理员
论坛积分
分
威望 点
贡献值 个
金币 枚
|
index.php* Y8 L" Y3 b& @4 |
- <?php( Y% @( W, @# N% j7 M
- session_start();
4 k4 E$ O/ H6 @ - // 丢弃输出缓冲区的内容 **
" ~7 w1 O& h/ R) e( I - ob_clean();
) k5 \- t1 f) p% F! i - header ('Content-Type: image/png');
+ T, }8 S$ p* \$ X6 p - // 1 创建画布+ h L8 {3 p8 P! r1 U
- $image=imagecreatetruecolor(100, 30);
+ y! o; h' N7 |" T) ?3 S - // 2 画一个矩形并填充背景颜色为白色
% \" l' K. d( x9 o7 }2 H$ _ - $color=imagecolorallocate($image, 255, 255, 255);
0 X8 k) y( d5 ~* h9 J - imagefill($image, 20, 20, $color);* a% G) j1 l6 X$ f
- // for($i=0;$i<4;$i++){
2 f; J, v V3 I- `- L# C- Q - // $font=6;
1 O+ S+ a+ m# B2 c) x2 J H - // $x=rand(5,10)+$i*100/4;0 Q( g6 s% J2 ^ z- ^: E5 h3 U
- // $y=rand(8, 15);
g; u. V0 y7 T3 Z/ f4 B% t! S - // $string=rand(0, 9);
# I0 B# D3 N5 [$ K7 p% @! O - // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));
- b. l W, B4 |0 N- d. n - // imagestring($image, $font, $x, $y, $string, $color);# J( V* n2 j0 j
- // }5 W: e7 O# A& t: S* C8 K
- $code='';+ v( {! r$ Q! d9 a9 T5 ?4 O, u
- for($i=0;$i<4;$i++){
. I& \7 O- Y/ m9 T( z/ u6 ^ - $fontSize=8;
* X) h/ G* q4 |. ?: D) d - $x=rand(5,10)+$i*100/4; G3 R, X. [6 A5 y! m& B& d4 {
- $y=rand(5, 15);) S, z$ f4 q, K0 X) \4 N
- $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
7 f/ _: K( {) K - $string=substr($data,rand(0, strlen($data)),1);. r: v: }( m7 f( a
- $code.=$string;
$ `; \" ^0 K" B8 ] - // 3.2 加载自定义字体
$ z5 t. Q* h' ]6 P5 c - $font_file = '1.ttf';
/ s2 }/ t5 `- C0 f: z( K( d" I' m - $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));2 b( Y; _9 l' C& R# d7 T
- // 把文字填充到画布' U3 m$ h" A9 J4 z$ ~! C) \% |
- //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);
+ R1 [5 _" [2 G" S2 l - imagestring($image, $fontSize, $x, $y, $string, $color);
% L! |- ^- q) m; Y5 r5 r" O - }
- f. G9 M$ J/ M; N9 b5 a6 Q - $_SESSION['code']=$code;//存储在session里1 |/ Q, x; o. u8 b; D
- for($i=0;$i<200;$i++){
. z* P. b; j6 N - $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));( e& n( ~+ t- ~1 @& E6 y
- imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);3 \( x' M' F6 b( y: M: N
- }
D$ V* K/ |; m7 K* O" S- y - for($i=0;$i<2;$i++){( F a: [: I) T4 ]* b
- $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));
2 @0 R& t/ V# P7 w - imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);' e( H/ v' i2 z \* ^5 y
- }8 h" n8 u+ P8 K n/ b% c9 d' Q0 U
- imagepng($image);2 z& M' i$ O+ b5 B# Y3 ~
- imagedestroy($image);
$ R( f4 h5 ]9 P0 q - 5 n2 ~6 N& x4 m4 z9 c$ j
- ?>
复制代码 ( L, G. D8 i* ]8 n5 A4 X
form.php( K0 V/ z* u# f# B" }7 B0 ]( Y
- <?php
. w+ B* {9 B& A - if(isset($_REQUEST['code'])){7 g+ X( M. c' `( J, g
- session_start();
. |- E- j2 v7 `3 F0 N6 v: ~% y - if($_REQUEST['code']==$_SESSION['code']){
8 s: U9 I7 C* o. E4 n6 i - echo "<font color='red'>输入正确</font>";- j9 O. y. B: f8 `
- }else{1 j( K$ r% ]: o0 Q7 Y
- echo "<font color='red'>输入错误</font>";% e4 o3 R @: q$ H
- }1 @/ B: b. I& l& T, P* m
- }- H9 a+ U; D$ [$ @& j
- ?>) F% a* f3 M! Y c5 N; w
- <!DOCTYPE html>3 y, f2 j" J8 K+ h l
- <html>6 h3 R' R' d4 w! ]( `
- <head> M# X# N, C. s- {2 x, q
- <meta chartset="UTF-8" />" e8 O: U- i+ B. q4 [# V7 l0 C% r
- <style type="text/css" rel="stylesheet">+ j8 k+ i" _4 Q- Y4 V, |% x
- a{6 `6 [. ?5 C9 d1 X9 D& g# Q
- text-decoration:none;
' }) ^, R$ [' ~& o - font-size:30px;
5 S$ y% H1 w3 v - color:blue;
) K( A) i& {( Q" H2 }0 m* Y - }
$ N! Y( ]' @) G- A1 K4 S - a:hover{
2 X; Q7 u2 O% Q4 | - text-decoration:underline;
% T5 N! ^# F5 q - }
9 `, `! H. D! a - </style>
/ w% Z d1 P7 K8 U) k2 A+ \ - </head>! ^4 _1 B8 [; j# `
- <body>+ o2 c' s/ @% }$ i: G y
- <form action="form.php" method="get">; j8 f$ Q+ ^8 N- l+ `) K
- 验证码: <img id="img" src="index.php?" onclick="changeCode()"/>
5 {1 K( Q Y$ ~+ o* o8 K6 ? - <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />" i# t& j) h N! V$ M( s
- 请输入验证码: <input name="code" /><br />
+ k! v# U, s% f - <input type="submit" value="提交" />- \9 b( w/ j6 E) b" j( n+ I) c
- </form>& e8 |! p L8 ?
- </body>: B( O! S B8 _, T
- <script type="text/javascript">' }+ X# m; M. h/ Y8 A1 X
- ( q# t7 d6 K: o0 m$ z0 {3 u
- function changeCode(){
3 F/ E4 i: q; u& i- Q& m - var img=document.getElementById('img');
# Z: J( _% g. C- B6 G& ~ - //img.src='index.php?r='+Math.random();
G l0 @0 t4 O! t - img.setAttribute('src','index.php?r='+Math.random());0 V. @& N I' ^! a0 r/ [/ I8 ~/ x
- }5 O5 S4 _0 J$ {1 z. W$ N( ]$ F
- </script>
. |* W# Y7 `2 H$ {. I - </html>
复制代码
( Y4 c2 a. w- b6 P( X
! j1 u' ^" T( _6 P |
|