管理员
   
论坛积分
分
威望 点
贡献值 个
金币 枚
|
index.php
# `- {; h% r3 U& ]2 q3 i- <?php1 T A: I- o* K7 A6 s
- session_start();9 D" x0 u/ T- a+ c6 x* f, G
- // 丢弃输出缓冲区的内容 **
1 L, s Z+ n- r) t% S: N) a - ob_clean();2 p) K/ w* W. O& {* O
- header ('Content-Type: image/png');" X G! Q) H! a! y3 O5 s+ x
- // 1 创建画布, c" a8 v$ s8 N
- $image=imagecreatetruecolor(100, 30);
3 [4 Q3 _, ^. y$ F3 V - // 2 画一个矩形并填充背景颜色为白色: u3 @6 n, `5 ]* P( e1 h
- $color=imagecolorallocate($image, 255, 255, 255);
; u; p7 |, f) `) M - imagefill($image, 20, 20, $color);6 O( Z O. d1 g# S. L3 T+ y
- // for($i=0;$i<4;$i++){
. T2 n! h1 K2 g! Z - // $font=6;. @1 Z& ^6 r& T
- // $x=rand(5,10)+$i*100/4; p6 a- A& N, r) G1 S
- // $y=rand(8, 15);! e; e9 z+ e) Y4 C- ^, B |
- // $string=rand(0, 9);- v1 v4 Q. X! A* q* l, i" p9 ]5 s
- // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));$ O3 r: U' m8 p3 U! X5 z
- // imagestring($image, $font, $x, $y, $string, $color);6 E; o: l4 |, s( N
- // }
0 Z# w5 r2 s. e3 @" k P& F3 Q - $code='';4 A3 u; z# C9 N/ v K
- for($i=0;$i<4;$i++){1 B2 ]2 v, @3 h+ Z5 y3 \
- $fontSize=8;' |$ }$ a" m$ C" M) z0 q8 J
- $x=rand(5,10)+$i*100/4;+ k1 D6 q5 u- \
- $y=rand(5, 15);9 j# `, P0 V7 F- E0 m( l; J/ ]9 N7 Q+ _
- $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
6 x( p Q7 w: { - $string=substr($data,rand(0, strlen($data)),1);
( c, k& ?' e$ q' k; _ - $code.=$string;" m9 |- K+ S$ ^
- // 3.2 加载自定义字体
0 O$ ^( Q8 m$ |2 O; j - $font_file = '1.ttf';7 b7 v" A8 e2 I
- $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));. }0 k5 k- d0 l
- // 把文字填充到画布
3 b; S' O% ^5 u, { - //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);
0 T4 B |1 p$ o# k; |7 N - imagestring($image, $fontSize, $x, $y, $string, $color); \% A; N4 e6 I! k. q
- }) a: ?1 | P$ v8 c
- $_SESSION['code']=$code;//存储在session里
; I! V' L U4 y - for($i=0;$i<200;$i++){; g1 n, H/ T& A" e( P+ J$ Z
- $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));
& i6 Q0 w: \6 N - imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);* Z! n" s- N4 c( V' K
- }
% N( `9 S2 O! E5 C; i- \7 N - for($i=0;$i<2;$i++){: @( F! Q2 o/ P6 H
- $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));6 `' q) s9 ?4 j4 a+ n5 X
- imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);
8 s ~% h. {0 U7 d& ]2 i - }
4 b- ^# G' p0 ~% ?' h - imagepng($image);) H' K0 {& j: m, y
- imagedestroy($image);" U" z+ [, P c4 f/ T- _+ N( Z
- ) F0 B6 K$ h0 y+ d5 z* T1 l4 |2 o
- ?>
复制代码 5 E, c9 V3 f4 B, o) j1 u1 H
form.php
) E% Y6 w8 S, p4 F5 {9 c$ b1 P% n- <?php* `- m# d3 n& \- r" Y- r/ W" m+ N
- if(isset($_REQUEST['code'])){) Z: c" ?- a$ f" h
- session_start();
8 `) }! {- N3 o8 B) o - if($_REQUEST['code']==$_SESSION['code']){# }1 J9 g2 y: h2 }
- echo "<font color='red'>输入正确</font>";
* f7 d0 U( z# l! I2 H7 O! D - }else{2 @5 s2 B" X: E" K( U$ T% l
- echo "<font color='red'>输入错误</font>";
6 Q; g* Z6 r0 c7 }8 b - }
n Q/ C5 `7 J) H - }
1 \; q: D! i+ v! w/ _ - ?>
3 N% |8 x* J5 F8 s) Q" l - <!DOCTYPE html>6 ?0 a; q+ N4 y* P2 z- h* a
- <html>
+ C. F% n6 W" K6 h8 {, ~' M - <head>! g/ z% y- y6 I4 a' E
- <meta chartset="UTF-8" />5 E& s; C/ h" G
- <style type="text/css" rel="stylesheet">
) s2 j- Q! r4 M& W7 @* p/ C - a{
( B" j0 V* D# j7 C5 { } a - text-decoration:none;
) {: i6 }1 B; L: z" N5 F, r- B3 m - font-size:30px;
: n p) d3 {8 G' v% S - color:blue;$ ^( ~$ V/ Q, z V1 Z1 v5 W7 _
- }0 A2 r% I! \' S6 M! k
- a:hover{- B; z1 N7 ]# S
- text-decoration:underline;
* `1 ^' f6 W. A' q - }
: q( D7 Z. g% V' x4 u$ s - </style>- i8 R. M" h9 ~2 B- v
- </head>6 o9 J6 u$ u& [+ L) R( I
- <body>. m$ Y2 P/ c# ?6 I: O, V
- <form action="form.php" method="get">
4 B. X8 B( d2 [3 N, E* \ - 验证码: <img id="img" src="index.php?" onclick="changeCode()"/>/ B3 t. ^: x7 _! ?9 W
- <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />) h) J& V; g0 R& w1 x
- 请输入验证码: <input name="code" /><br />
& f' x. x) x8 R2 _& `4 w - <input type="submit" value="提交" />
* U2 h; a3 w1 z& w - </form> c# S9 }1 _! t1 ~+ W2 q
- </body>
" J( n" L- ~) ^* q I - <script type="text/javascript">
3 ^: Z# k& g' H; k& Z4 s' P - ' h6 ^. m4 f4 }( p& L9 i+ g2 d+ M
- function changeCode(){
$ J9 ]( A" y+ J6 H - var img=document.getElementById('img');
0 @. k7 p( V, ^0 W2 W - //img.src='index.php?r='+Math.random();
; W% u4 |2 c$ @" N - img.setAttribute('src','index.php?r='+Math.random());
! w) D) N2 q, ^% }: w" P, T. m - }; |2 c) ^- k4 \3 y" R+ B$ {
- </script>1 v( [! H! W1 F; G
- </html>
复制代码
2 K' o7 [# |4 I% c% B8 S |5 Z0 P- ?4 G; ^& r
|
|