管理员
   
论坛积分
分
威望 点
贡献值 个
金币 枚
|
index.php" V4 Y: r/ E: K: L8 J
- <?php
# I+ f7 v2 a9 L$ j6 K* |5 u }9 C# A - session_start(); {$ F( I: D* B) \0 H2 d
- // 丢弃输出缓冲区的内容 **! y4 n1 U6 z4 f1 h- B {
- ob_clean();, {/ k2 F+ H, V0 {$ E7 {; v
- header ('Content-Type: image/png');
! y; @! a8 n9 |2 g# j% o% w, I9 ? - // 1 创建画布
W7 K' L& s0 A, ~: J - $image=imagecreatetruecolor(100, 30);
) d1 Y9 y* v0 ^" } - // 2 画一个矩形并填充背景颜色为白色
7 R* C) y+ Z# B; x - $color=imagecolorallocate($image, 255, 255, 255);. j7 E3 l& l9 E1 f; G) Q
- imagefill($image, 20, 20, $color);
0 u3 Q& a" f+ f7 b+ _ - // for($i=0;$i<4;$i++){
1 g& F9 _ N& @/ U) J) e( y* _ - // $font=6;
( a3 x4 X. q) ?7 `$ p* p3 L - // $x=rand(5,10)+$i*100/4;
T7 B, C, h* F& D - // $y=rand(8, 15);/ E% g" b) v) B6 h, w, w
- // $string=rand(0, 9);7 i* i! F- Z" W& Z" L& E% u
- // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));& b/ k3 L7 C) u) ~
- // imagestring($image, $font, $x, $y, $string, $color);
- k7 y4 { x& d9 y0 j1 F$ P" g/ f - // }3 X& |& I6 M. j2 D- P" c
- $code='';7 H* v5 o! l, a( L! q" @4 L
- for($i=0;$i<4;$i++){3 F3 S" G7 P. J( ~
- $fontSize=8;; Y5 V [( `+ @( q, r9 B
- $x=rand(5,10)+$i*100/4;
, w a! T e9 S - $y=rand(5, 15);) h. \- [9 D* G% B/ W% A
- $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';" Q+ B; @4 D# t
- $string=substr($data,rand(0, strlen($data)),1);
% v+ a9 ~ M! R" h0 m - $code.=$string;
9 T% r9 q' l7 l* n# X9 U - // 3.2 加载自定义字体
! ?% ^1 \+ j, g! c) u - $font_file = '1.ttf';" {7 O* T" l% s ]3 s
- $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));7 l% B L5 x1 B2 ]$ C, }3 x# j
- // 把文字填充到画布" P- o* E8 V( z3 a- z7 c, A
- //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);( Q' m+ l) k$ H
- imagestring($image, $fontSize, $x, $y, $string, $color);- i2 j" b& V1 Q) O2 D
- }
0 G; c P2 @, [% Q/ ]1 o - $_SESSION['code']=$code;//存储在session里! B1 c7 p4 W* O3 p1 \! X
- for($i=0;$i<200;$i++){5 `% M$ y' p7 _" h/ E
- $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));
- U" T$ m# y# l# o3 E' \ - imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);
1 D8 y5 T4 o/ l# Z9 C) | - }
8 J1 I& [5 c2 u V - for($i=0;$i<2;$i++){
% T) S4 j1 L6 B: v - $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));7 o* v/ J z% O* r ^+ K
- imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);; X, o. k4 c6 D- ?% N: Q
- }5 f- B% X. [) s6 C- ^+ _' W& q; A
- imagepng($image);
1 U; r' P% Q6 k) o( S1 e0 }2 g - imagedestroy($image);4 ?# @4 `$ _* |6 J
' ?7 x1 [8 n; K5 k3 t% [3 [- ?>
复制代码
' m7 W- }% g# J1 rform.php# u! u# p9 y" o/ @1 _- r' P
- <?php
% d* V4 W& z9 T2 Z2 [4 {9 l j" O. M - if(isset($_REQUEST['code'])){
# T( z# t: f2 j1 S1 Z4 T6 U - session_start();
& P$ n5 W& i8 {* v' ]/ t9 U - if($_REQUEST['code']==$_SESSION['code']){
6 T% Z6 a) r$ x4 Y" q) i- w - echo "<font color='red'>输入正确</font>";; Y% h' q; x, P- M/ [1 T1 e
- }else{
+ }- K# d- ~" d) G5 ~9 U: L - echo "<font color='red'>输入错误</font>";
9 }+ r5 j( o7 ?* A - }( N/ c( N$ v5 C, a G
- }. ?1 V' w1 c6 w- o8 j1 a' i8 ]
- ?>
9 S5 V! ?( B+ g/ o, H8 U" k/ ^7 N - <!DOCTYPE html>0 `; d4 Y' [% c) m
- <html>) A K6 M! o. w9 B- q. U
- <head>
7 n, R; C: o( C) D) A - <meta chartset="UTF-8" />, x2 G! W5 |3 V& ]
- <style type="text/css" rel="stylesheet">
' H7 H. m! ]/ ? - a{. w$ H2 b$ I x( U
- text-decoration:none;
) i, Q! y# b9 V0 Z6 a - font-size:30px;
8 o# a2 D- Y/ J& Q! r - color:blue;
% M4 b# R8 m G. z - }; j& f" {+ A* B8 P
- a:hover{
K9 {6 C w' f" J- ^ - text-decoration:underline;
) o3 S: y) N8 u - }
* y; G: z7 N* W0 k" \9 o# C - </style>4 n3 H/ S, G0 b( }0 Q9 H, p
- </head>, c! @! b6 s$ X; [% b4 P, f
- <body>1 `$ ]& |2 K- n( F7 t. {
- <form action="form.php" method="get">0 W3 C# E9 U! C
- 验证码: <img id="img" src="index.php?" onclick="changeCode()"/>
: I7 W3 \5 _# o. w. y - <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />) b. D/ J" }$ T, |
- 请输入验证码: <input name="code" /><br />
3 e7 n& P! [! m1 Y - <input type="submit" value="提交" />) I; e0 J6 L* B3 Y
- </form>' p+ x: h( K& J5 t! Y' A H
- </body>
( p/ g0 S& ]. A& A9 B( F4 L - <script type="text/javascript">
; c3 X" `0 C+ l) R -
4 m4 d4 V; B: e! N- G* r8 h( t - function changeCode(){4 f! L8 m3 t9 j: M
- var img=document.getElementById('img');
- l2 s# l' ]! Q# `' H- d - //img.src='index.php?r='+Math.random();6 t4 k& @( q* M9 M& C% e& J
- img.setAttribute('src','index.php?r='+Math.random());3 `% x" m }' Z. J( }2 z, g9 h
- }: h% M" k8 r- x0 x( Z ^4 q# k
- </script>2 z7 _+ f9 f& I6 J
- </html>
复制代码 $ X$ g9 K! ?# P
& ~6 u0 P" G3 ^) `/ f) p" w |
|