管理员
   
论坛积分
分
威望 点
贡献值 个
金币 枚
|
index.php
* }# [3 y' _& E0 }- H6 s- <?php9 S' n6 x& \) o8 ^8 I
- session_start();/ Z4 i& X: z3 \0 \
- // 丢弃输出缓冲区的内容 **
8 \3 ~* h$ @$ `" J6 H% W - ob_clean();2 M" \3 F K7 g4 {2 I. I$ v
- header ('Content-Type: image/png');
4 F# V' D' E$ A% [' k - // 1 创建画布) ?4 d8 C/ {9 r2 A1 V7 Z N
- $image=imagecreatetruecolor(100, 30);
/ g& L5 ]$ X0 z8 Y, X. c& v - // 2 画一个矩形并填充背景颜色为白色1 \1 U, p4 _& Z! e' \& S
- $color=imagecolorallocate($image, 255, 255, 255);, C1 m) C& R8 Z
- imagefill($image, 20, 20, $color);& k5 X5 C& s) e3 J3 c3 y; n
- // for($i=0;$i<4;$i++){! r0 @7 Z1 K/ U" ^- n
- // $font=6;" F7 j* B2 K$ F1 E
- // $x=rand(5,10)+$i*100/4;8 q; S, ^ x6 Q
- // $y=rand(8, 15);
9 ^; q+ h4 w; [2 ~5 A - // $string=rand(0, 9);
) t6 E1 E. b |8 ? - // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));+ M3 V1 c& P+ i6 h- P) u
- // imagestring($image, $font, $x, $y, $string, $color);" W! A, T/ s: J9 g0 v7 G. l
- // }- W/ h$ g, M2 `" n8 V
- $code='';2 [, F+ W; ]# k, C; {9 t
- for($i=0;$i<4;$i++){# k9 x, v; j( [3 E& ~9 m c0 a
- $fontSize=8;
/ S/ u) L" y. m* a% E* f - $x=rand(5,10)+$i*100/4;' V( F3 w+ B h- A1 h/ d
- $y=rand(5, 15);* G1 ^' l: W G1 `
- $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
& I1 ]4 c* e$ @4 l8 v0 ?" m! T - $string=substr($data,rand(0, strlen($data)),1);/ h, g; b( F/ t+ l0 |; K
- $code.=$string;9 }9 Y/ }2 Q" O U2 p
- // 3.2 加载自定义字体
2 M4 u W3 [7 E& C9 _0 }/ M - $font_file = '1.ttf';
, b9 O, O" L$ m - $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));( u" p' M% S: |4 E1 }) P7 N) ^
- // 把文字填充到画布! ]) H0 J& h; {: x8 q/ G. ^
- //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);- w7 k! B4 K5 v; v: a; ]3 z) E
- imagestring($image, $fontSize, $x, $y, $string, $color);3 o, G* S4 x/ H
- }
1 r0 `8 u, |) ?# @7 p - $_SESSION['code']=$code;//存储在session里: Y8 H+ K* n5 D8 P0 S* h
- for($i=0;$i<200;$i++){
- I: ?+ {7 @" x9 p - $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));6 R# d7 g7 {5 C q# j* o. X$ o
- imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);9 ]* I; J. x7 m# I# O x$ D5 _# Z
- }4 l6 e9 t) {. S5 q% N4 K7 Z
- for($i=0;$i<2;$i++){) w7 s1 l; P3 W7 o4 W6 \
- $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));+ s- m1 Q1 C W! A% U+ e
- imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint); S( J- l! V1 S, B+ }# p/ V
- }
3 V8 z. Y0 R7 g - imagepng($image);
& ~0 p3 s5 b) i z; d" T - imagedestroy($image);! i* [) @" k$ W
- ! _7 B, ? [4 o7 p! U
- ?>
复制代码 - d% Y0 U( P, k0 v
form.php
+ n6 X5 C& e- R' \- <?php6 `! w: S) b; q, r" m4 b6 J L
- if(isset($_REQUEST['code'])){1 F) M- l. E& O/ B
- session_start();2 ^9 S8 o% D/ [1 |+ T ^' K
- if($_REQUEST['code']==$_SESSION['code']){6 t2 S# N& y6 X! s
- echo "<font color='red'>输入正确</font>";% w# z+ q( I& f7 ^
- }else{
* E2 ^) |) z9 |7 u0 } - echo "<font color='red'>输入错误</font>";0 O. W. G h9 u! e, ^ _
- }
$ k6 Y6 o- n- J0 n - }9 L+ W$ O7 b- @- N. \+ u- z4 h5 {
- ?>
( k. O+ m6 D' g6 T% x+ d! [ - <!DOCTYPE html>
' h) W! p0 J" B/ D" Z& Z5 `7 r1 E6 q - <html>
s7 ^: ]- t, H1 t) j* C - <head>. B4 L1 G1 ~4 n
- <meta chartset="UTF-8" />
6 {( R3 K8 k: V7 W4 W - <style type="text/css" rel="stylesheet">* d! b0 j/ O1 w/ W
- a{
2 Z1 x% x6 o" P- P - text-decoration:none;
% v1 O/ w# D R - font-size:30px;' |7 d. _; {9 W& Q( W5 [& y X
- color:blue;
* m. \' l9 @9 y8 G8 Y1 F; D \ - }# j1 M+ x) |' U7 l( f
- a:hover{
8 O9 M+ G- A& D6 Q- H1 T: l5 v' z - text-decoration:underline;
2 F" S! P/ @! C6 {! _4 A& i! u - }
]0 Z6 r3 S$ L) ^5 y. _ - </style>
! m) m' q6 g0 U: h( o - </head>
( Y# A2 K1 }' o2 o8 d1 | - <body> p6 u1 l0 d6 L) S5 ?
- <form action="form.php" method="get">- e9 ]" S2 g8 c1 ^4 E1 s* ]
- 验证码: <img id="img" src="index.php?" onclick="changeCode()"/>* [* U4 l/ _1 ]) v% k/ F
- <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />1 N/ M! o! O |" l/ L
- 请输入验证码: <input name="code" /><br /> d6 l5 Y3 o' C4 o7 P) S
- <input type="submit" value="提交" />
% f: \& x$ @& u7 S - </form>
( E8 X# {5 ]4 z# R z: E - </body>8 n" D) u: N c( M( n( ^' h
- <script type="text/javascript">
- q- G% c& I& Q1 R" k! p# w -
$ Z8 X- b! ]( F; ~; g6 A; K - function changeCode(){
5 k$ U. W% D! Y' U, Z3 y2 b7 U2 B# ^ - var img=document.getElementById('img');
" F% X1 v: i' A. C - //img.src='index.php?r='+Math.random();0 S" F' ` @8 B0 z
- img.setAttribute('src','index.php?r='+Math.random());) R% I+ m1 B* C5 q' Y
- }
4 M/ U5 G/ P4 f$ H/ B" `0 _ - </script>' @1 Y, }" ]# v8 ^/ V7 o
- </html>
复制代码
5 |. {# n4 U2 R. N( p3 \
1 k% W% E$ I. C( i5 y* p3 ^8 m |
|