管理员
   
论坛积分
分
威望 点
贡献值 个
金币 枚
|
index.php# K0 D h# \/ g4 R+ ^. J$ j- {9 z
- <?php1 v$ x* |$ c# W
- session_start();: C) S+ U3 U' D' V
- // 丢弃输出缓冲区的内容 **" G8 e7 a% x* P: S0 }
- ob_clean();
! `. \% G4 c4 u - header ('Content-Type: image/png');
( q6 a3 U' ]! C- Y; X7 c1 r# i - // 1 创建画布% N. \/ j; _; l
- $image=imagecreatetruecolor(100, 30);
1 T) j \* l! n+ j3 r7 f" A, ^ - // 2 画一个矩形并填充背景颜色为白色
/ j! Y* _+ M4 \ - $color=imagecolorallocate($image, 255, 255, 255);" e( m- z8 _8 z' u
- imagefill($image, 20, 20, $color);
$ @) C8 p; q8 j! |4 x. \ - // for($i=0;$i<4;$i++){
" F2 I( q: U* X% o+ g# \ - // $font=6;
( F; ?# P( X2 h6 U3 v3 c - // $x=rand(5,10)+$i*100/4;& S9 t; G( U# ~$ R5 h& ^- j) K( j
- // $y=rand(8, 15);
* Y) `( I A) ?: M5 {" s8 r - // $string=rand(0, 9);
, j. f9 C4 M5 \$ S1 i - // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));
" t- m$ U B- y) ~. X9 E - // imagestring($image, $font, $x, $y, $string, $color);
' F" ~! X0 d8 W+ t; z6 [9 d6 K - // }% ~! A+ A( u: f6 N8 V6 k
- $code='';$ j; \8 P" g8 W/ @2 H2 h
- for($i=0;$i<4;$i++){8 @# D; E, E# h: c
- $fontSize=8;
% a8 A0 u( Y# x% G( h3 K6 ^5 e. R - $x=rand(5,10)+$i*100/4;
, U* x# w. E+ V( J8 h1 |. ~ - $y=rand(5, 15);
1 x" S, h T% n2 N/ l5 ? - $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
. n. `0 N9 E- T( E; p' i# Q - $string=substr($data,rand(0, strlen($data)),1);
+ S* x4 }$ d% S5 I3 ~3 |% o - $code.=$string;" z) T5 a+ f2 }; N* g s
- // 3.2 加载自定义字体
9 z4 j+ {! k. ^( ] - $font_file = '1.ttf';
: B( @8 E. f* N5 |; F0 q - $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));9 y8 ^) X6 Y1 d* |3 f$ I
- // 把文字填充到画布0 h8 D; M) E& E# \' J. J
- //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);- Q) |- ^1 ^% s" z, n4 Y- }
- imagestring($image, $fontSize, $x, $y, $string, $color);# _5 @' ]+ {" w# O! B
- }
8 `- n- |$ H% ]. T# s! ` - $_SESSION['code']=$code;//存储在session里
) I2 U' P; w* v* O3 M2 _& U - for($i=0;$i<200;$i++){3 i7 E ]0 X/ n+ @4 |" N2 ]
- $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));3 d2 m# y( c7 c6 m
- imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);9 R) G( {' N) h% }
- }' T- Y1 t5 L, O. r: r
- for($i=0;$i<2;$i++){
: w% _8 W4 l6 U. c I" N8 _ - $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255)); V% L4 T' |0 h* ~9 z$ m3 @; l
- imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);
. |5 s T7 U) W+ U- O: ?6 W - }
3 n- @, ?3 B ^. y+ Q - imagepng($image);- B( x+ v1 s* O
- imagedestroy($image);! a% S/ h: T/ N1 E- e$ t
9 i* E0 k8 ^2 ^( }6 e- r- ?>
复制代码
" N. [8 ]' z! A) z, o# J+ _form.php
9 G) C% Y4 K& q# _, w/ n! D- <?php
5 q+ Z) n. } R" T, c. K7 q: a - if(isset($_REQUEST['code'])){( f" |2 w( H; N8 m3 Y6 i
- session_start();
5 k6 W2 l3 A9 y# E/ ~4 j* u - if($_REQUEST['code']==$_SESSION['code']){" K8 z" x$ @: W/ a C" a, _
- echo "<font color='red'>输入正确</font>";
H2 d. a1 g( X" k - }else{
! F; n! [) W9 J7 P# S, T0 N - echo "<font color='red'>输入错误</font>";
5 {) D$ i5 j P3 U - }8 F- s- B/ B. ^0 d9 ~" P5 i5 B
- }3 q' C! _$ s, d0 U4 g6 ^' B4 P
- ?>
/ J, L% k+ W: {6 V: U3 ` - <!DOCTYPE html>0 b! d6 Q0 I+ r, g9 ^' M
- <html>
" p" q: s5 g7 K7 W! L9 c) K+ i - <head>5 T0 z1 _% u, _; J
- <meta chartset="UTF-8" />( g8 w8 P0 M2 h5 i9 L
- <style type="text/css" rel="stylesheet">
8 O0 Z$ y- W+ N9 Q% S - a{
; S8 c* C9 f' c5 C' O8 }! D - text-decoration:none;8 ?% k3 m: Y9 C- \
- font-size:30px;
/ K$ C- Q4 L0 c" i - color:blue;
) S2 y2 Z! w- X, s" T7 a - }7 _( U' T8 g$ |. j w1 \
- a:hover{1 C- a! p$ G3 u1 j! k
- text-decoration:underline;- e9 ?& j5 S7 u& w2 B f
- }
' @' ?- {. y8 l0 a) Z& x - </style>7 m% S! E9 [& M0 g7 O0 o3 _1 l
- </head>
, X! z1 a! l7 c$ s - <body>8 \: o4 ]( _! l6 Q2 ?
- <form action="form.php" method="get">
2 w6 O8 B! M! \5 w5 S( q6 a+ U - 验证码: <img id="img" src="index.php?" onclick="changeCode()"/>
; U, {4 ]( `3 k% r' x - <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />$ O6 Q) R6 a- L
- 请输入验证码: <input name="code" /><br />' ~, I! I8 L' t/ @- G n
- <input type="submit" value="提交" />( V6 u# W$ l& A" K# T- v g3 [
- </form>4 N" _" Z" b! S6 T! J) J3 A
- </body>6 H$ h1 }# M, L* u8 A3 E# b
- <script type="text/javascript">
2 i- Q9 ~& }: A% \ -
+ b' S0 L1 A! \5 F. i - function changeCode(){
, j6 T. h7 r$ D" [, N2 o' c - var img=document.getElementById('img');9 B3 U* @6 C4 ?5 W- H; W$ g: d' I
- //img.src='index.php?r='+Math.random();
n" \, x1 P0 p5 {. A* e - img.setAttribute('src','index.php?r='+Math.random());
^% I) [# Y( s. u6 w% f0 u - }
! Q: L$ l4 o0 w2 i - </script>/ X1 X/ k- {+ t5 k% F8 g4 W
- </html>
复制代码
3 m% N4 z/ g+ Y9 H4 o! a/ ?# j4 y5 \" y1 i1 B! Q0 G/ b" g9 U9 V8 N
|
|