管理员
   
论坛积分
分
威望 点
贡献值 个
金币 枚
|
index.php+ j. U7 O' X2 F2 o
- <?php5 T" D+ k7 b3 V) k) E! J8 w0 H4 x; ~! F
- session_start();& D+ T; t' P. F8 A: P0 `2 S
- // 丢弃输出缓冲区的内容 **
2 z( z2 C3 W Z - ob_clean();0 \: i/ z% e+ T" n1 W9 K2 O
- header ('Content-Type: image/png');
! J+ _7 V: \/ o - // 1 创建画布5 W4 b8 B* G. W) H
- $image=imagecreatetruecolor(100, 30);
3 c& e9 ~6 m# @6 a - // 2 画一个矩形并填充背景颜色为白色; ^+ |7 `9 G8 c- i6 k9 v
- $color=imagecolorallocate($image, 255, 255, 255);
9 A2 N) P% X v" b9 S - imagefill($image, 20, 20, $color);
" E" {6 c' d' O' l( o# N- v - // for($i=0;$i<4;$i++){1 a4 }+ u% B; I
- // $font=6;
1 F: o5 O3 x; z5 T" A5 b' @* K - // $x=rand(5,10)+$i*100/4;
, m n3 a& X/ s$ n - // $y=rand(8, 15);" c1 D4 ?% W1 z: ]2 C4 S0 @9 N
- // $string=rand(0, 9);9 F( ]5 z% G% k" F3 a
- // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));
: I9 R* _- {1 y7 e0 W - // imagestring($image, $font, $x, $y, $string, $color);& i% e- r9 W; x
- // }
) u5 Q5 R$ H$ ]9 ]4 c - $code='';
/ U- u4 v5 E) d9 a i' T/ Q - for($i=0;$i<4;$i++){" T2 k7 f7 W) }8 Y& \" t
- $fontSize=8;
$ K# U1 ^/ i; V: s! |' f9 U8 x& {; `8 y - $x=rand(5,10)+$i*100/4;
! f4 Y" F2 g$ N - $y=rand(5, 15);
: D( E2 ?2 \( U6 ^/ l3 }: p# y - $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
$ `& Y) C0 t3 D( q/ j" J - $string=substr($data,rand(0, strlen($data)),1);
6 n% h) {; I# a+ h/ S4 K - $code.=$string;
/ p% s+ f, {' d4 H - // 3.2 加载自定义字体, s; v) _- ]% p) v5 W
- $font_file = '1.ttf';3 W0 W3 _; t H9 b( x& g" R) K& \
- $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));
3 `6 F0 }7 x) L) d; _1 g! F - // 把文字填充到画布" Y( Y) s5 D% q" W; |4 J0 |- p
- //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);
8 q, @9 }2 S- K3 C - imagestring($image, $fontSize, $x, $y, $string, $color);4 ` u: `8 l- \& Z7 F+ m
- }. e0 [- m; b+ @2 X
- $_SESSION['code']=$code;//存储在session里0 P$ l9 k7 \( |+ s0 c8 a
- for($i=0;$i<200;$i++){
3 t. U; t# S, M" K - $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));
' W+ E! v& j4 X1 i - imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);
0 C0 S3 P. O. U - } }% Q8 b8 V: E6 | G6 r5 `4 p! ~0 j
- for($i=0;$i<2;$i++){
% p6 B; V. J3 ^ - $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));
% U: c5 b' x7 \, f& ? - imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);) [+ R3 g: ^/ ?5 ~5 a
- }
+ g9 a/ `0 n8 H; b9 E - imagepng($image);6 L/ M' c; Y* ~
- imagedestroy($image);0 m" E6 h6 k9 Z# u: p) e
- ' X* l! x* b) q3 e0 e
- ?>
复制代码
* b% {8 d* B5 A; Kform.php* n$ V$ Z% s; O+ H
- <?php5 a5 L, Z1 R: h! N5 z$ {, @ |0 X8 {
- if(isset($_REQUEST['code'])){
* v, G. o6 N8 n( g, Z8 ]/ | - session_start();
8 M4 p0 Z2 R; y6 \- Y - if($_REQUEST['code']==$_SESSION['code']){
0 l/ z- Z# U2 N2 R - echo "<font color='red'>输入正确</font>";
# j* [' h' z, l" A - }else{
* h) N, |" {. _ - echo "<font color='red'>输入错误</font>";7 [5 W/ S; C0 J$ p8 s, J
- }
0 ^0 c) P' Q: g# s - }0 i4 @5 e* K L' M8 g4 M9 U0 m* m5 R. r
- ?>
# J8 p2 l9 G) a6 i' ` - <!DOCTYPE html>$ L" T0 ^) J/ ^9 R# l. A
- <html>
8 y# ^, O2 j2 F - <head>
" O/ y D- \+ Y6 a" D5 r: ~ - <meta chartset="UTF-8" />2 @/ A1 c1 @- g' d" ~
- <style type="text/css" rel="stylesheet">
# U8 U& g' |% J) V9 Q" X - a{ M# Y( Y/ n' D, U: P# T; D; M
- text-decoration:none;
' C6 P. x p9 ? - font-size:30px;
o- l5 T* c, c: F1 M! r - color:blue;
1 }/ h: Y) A6 e$ N+ q0 i& | - }6 r0 W; h3 i; ^& @4 X3 K) x: U, ]
- a:hover{
/ `4 Y. {! }/ n; O - text-decoration:underline;
. C4 [7 a& z$ V" ~ L! K9 c! d - }
; M: t0 g9 ?/ b. ^$ f$ c - </style>
* J% ?; b% o2 h# j+ G - </head>0 c$ |, D2 W( T( i: H
- <body> R- Z ~/ E9 ~, `6 r! h( d
- <form action="form.php" method="get">2 s8 q( \) j2 q# L5 M
- 验证码: <img id="img" src="index.php?" onclick="changeCode()"/>
g$ N4 g9 A9 W6 s - <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />
" m9 C& g7 s' w* }3 W - 请输入验证码: <input name="code" /><br />* C! Q$ f* O1 n; {7 J
- <input type="submit" value="提交" />) W( L5 b0 M( J7 D. _: v( p6 N* l. Z
- </form>+ g- g' C, S4 t# V- n- T5 q. P
- </body>
9 Z2 S7 a, j5 u" {' u2 A' z+ }( z! T - <script type="text/javascript">
8 j/ _0 S5 ~2 H' X: K6 w" z, W - 1 F. A" P, K0 E- T- D
- function changeCode(){. T7 A, v& a2 ]! {* }4 n7 }
- var img=document.getElementById('img');5 v$ P. K6 F; p
- //img.src='index.php?r='+Math.random();$ U i [6 m* }% V+ O- [4 E4 R
- img.setAttribute('src','index.php?r='+Math.random());5 C; N, M B, }: F5 {" y3 D
- }
$ I; T% N) C1 y% d, h - </script>
! n+ t" }9 n6 M' Y8 O& q# @ - </html>
复制代码 K) K4 u) r. B1 n5 y, ^& B
5 v& x5 w% ?+ x" O/ c U) L
|
|