管理员
   
论坛积分
分
威望 点
贡献值 个
金币 枚
|
index.php
5 S, J f9 n; Z7 j. b- <?php: o5 f, @* g4 n- l! v. K8 C
- session_start();
* `5 J1 Q. J- \( z' D - // 丢弃输出缓冲区的内容 **
! ?$ M" N8 m! v* ~ - ob_clean();
1 }2 @+ ]5 }5 M4 e! P - header ('Content-Type: image/png');
3 Q: z2 g9 `0 z8 d7 R - // 1 创建画布; N; E8 h/ W: R* i9 h6 K y' C* q' @
- $image=imagecreatetruecolor(100, 30);: U7 l) {/ S) |9 a4 ^
- // 2 画一个矩形并填充背景颜色为白色
2 }$ I" A) Z1 u - $color=imagecolorallocate($image, 255, 255, 255);" ^4 J4 q t. D9 N0 b# g
- imagefill($image, 20, 20, $color);& F0 @0 c$ G5 J3 K, l) m# N1 y
- // for($i=0;$i<4;$i++){
3 U$ `, h) e/ W/ }- E* D' f - // $font=6;
0 X6 d/ f# T. g& l& ]5 K - // $x=rand(5,10)+$i*100/4;
- X& K( k1 \9 J/ h; u4 G# ` - // $y=rand(8, 15);( Y; n- B3 I# s8 g
- // $string=rand(0, 9); h2 R7 x! a/ l" _8 x
- // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));
4 C% ^3 M. W7 Y - // imagestring($image, $font, $x, $y, $string, $color);% L4 V" K- d4 f/ M8 i- ]
- // }
% C% X. Z$ t1 {4 c; o - $code='';7 Q4 ^" B. P6 a4 x+ R$ ?
- for($i=0;$i<4;$i++){
7 b$ m2 l1 L* C7 Y - $fontSize=8;
7 g4 D) |4 ]1 D0 E9 T9 U - $x=rand(5,10)+$i*100/4;
; K( U6 ~5 M6 w8 _0 A8 \! P - $y=rand(5, 15);
5 N% v" y$ B! d+ P - $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
) y3 d5 N- n# a! ~2 H' i7 | - $string=substr($data,rand(0, strlen($data)),1);
( ?, I* p, f8 \2 q( \ - $code.=$string;$ Y- d! w* k7 i& w4 j
- // 3.2 加载自定义字体' O) o) q/ e$ C( x
- $font_file = '1.ttf';
$ n" l: `6 I5 X8 x - $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));
, p4 z( Z6 C3 ~* D' ^ - // 把文字填充到画布8 E; K5 S. X+ A+ P( K9 P" i/ l2 b4 V
- //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);) j+ l4 H3 m$ ~3 S8 q9 i, E* ]: K; ^3 q
- imagestring($image, $fontSize, $x, $y, $string, $color);1 H0 W3 j3 c9 r Z" a. k
- }
" A$ j: a2 v9 } - $_SESSION['code']=$code;//存储在session里
" v/ F7 `% e- a - for($i=0;$i<200;$i++){! `4 u3 ]4 w. f$ j7 C0 {
- $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));: m/ z! D( f8 b
- imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);
1 W9 {/ x( w% e) P2 H5 k - }
5 ~1 R. T3 E1 @! e* w - for($i=0;$i<2;$i++){
: Y2 `7 I" \$ u0 {3 H( i, d - $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));
. F2 x" T, f2 J) S - imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);
( n2 U1 }5 g) [1 W4 R+ A0 u - }6 {* c# U3 \3 @
- imagepng($image);
* ^6 @( Z1 `4 V& t z6 f - imagedestroy($image);+ [, A7 u; s1 t$ b& g& q: k
- & B8 i0 q0 H. r$ y6 m) N
- ?>
复制代码 5 |0 m( c& ^ u
form.php
' I8 p7 [7 \7 N/ @0 v5 ^, n- <?php
/ o$ Y' C5 `9 Q4 b - if(isset($_REQUEST['code'])){2 G) {3 q6 S$ o
- session_start();6 ?' s1 L9 D; R- y
- if($_REQUEST['code']==$_SESSION['code']){- ^9 M4 L* Z; i- ]/ Y
- echo "<font color='red'>输入正确</font>";
$ M$ v3 H$ n2 j2 s/ H! ?4 x - }else{' _, R+ k( |; K" B: q# ?
- echo "<font color='red'>输入错误</font>";
3 i v9 u0 A3 J) {. _" K: Z - }
9 {: s6 t/ N2 v; F/ P4 Q$ {2 z - }1 `9 T; S; \6 [7 _: d& u
- ?>2 Y0 |. h2 |" G- m; ]
- <!DOCTYPE html>
5 P4 M a; l5 z* E* c - <html>
: ~2 S' B2 t: |, o+ {& \; S: H) ] - <head>- |7 H- U* ^2 [2 N$ m& U
- <meta chartset="UTF-8" />
" h( Z7 F; S- J* `* O" B/ g - <style type="text/css" rel="stylesheet">
3 ]. u5 ?' D, b [ - a{
! T/ Z- S* y! [' D2 K% n3 { - text-decoration:none;& |) r7 j& u8 A3 b$ A2 ^2 _
- font-size:30px;
' U' c% u8 I6 ` - color:blue;+ ?. a% J0 ], E; v3 ?) V) t7 X
- }4 x" i# ] n3 ?* f
- a:hover{4 U6 I4 U! h. a' F$ E
- text-decoration:underline;3 r" C# R# f) q1 W
- }+ A1 Q& P# G% u3 u1 ^% c# x, }
- </style>; B1 a3 T( B# @- t
- </head>
0 }% T# I+ v) y( m9 Q5 B9 R6 H - <body>+ X3 `6 f }5 f
- <form action="form.php" method="get">
; D0 Y r) [9 W - 验证码: <img id="img" src="index.php?" onclick="changeCode()"/>. i% A5 |4 L0 b( ^6 a
- <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />
- M" ~, | _, R0 L - 请输入验证码: <input name="code" /><br />3 x7 ?& }7 ]0 C
- <input type="submit" value="提交" />" @5 _6 L5 K4 K( i7 \; C* B+ e+ Z
- </form>& }8 |* B. j) n8 m* m. j
- </body>/ g+ T# x% w; y e$ }/ _5 v0 e
- <script type="text/javascript">1 @. o) ]8 E7 T4 k. P6 o
-
z L3 x! y" Y; ^+ n/ p r - function changeCode(){! W3 j+ V9 K6 F2 w, G8 J9 Y: j7 x, B
- var img=document.getElementById('img');
! v7 J% Z' z/ Q7 g& l - //img.src='index.php?r='+Math.random();( a- E/ _6 T. B1 w2 F e
- img.setAttribute('src','index.php?r='+Math.random());" z' e9 [1 i1 N
- }. [) N1 l. ]: m6 v% D r
- </script>1 u$ i1 M1 w. g
- </html>
复制代码
/ o! K& A0 Q6 t( _+ w5 K+ w0 e
: h. R- I9 {" G1 {+ S4 @ |
|