管理员
   
论坛积分
分
威望 点
贡献值 个
金币 枚
|
index.php# m2 Q F9 z5 j) j0 ]5 _' q- \" k( O
- <?php
- O; N5 Q6 \2 d) g - session_start();
# }# }& P) s2 p5 S+ p5 t* ^ - // 丢弃输出缓冲区的内容 **" }! h( U+ e5 N7 X) C" G) B0 j
- ob_clean();, y/ Q" c2 k# `' b# E4 i7 Q
- header ('Content-Type: image/png');
1 F5 I* m! n e' D/ M' x/ @ - // 1 创建画布! l; N1 y4 U, ~# c
- $image=imagecreatetruecolor(100, 30);
3 t/ u1 w# {& R8 _7 f; A5 p) |) _ - // 2 画一个矩形并填充背景颜色为白色
( P# A$ H/ G1 I - $color=imagecolorallocate($image, 255, 255, 255);
$ J9 _/ l D% W9 f+ r3 U+ k - imagefill($image, 20, 20, $color);
$ v% |, I2 ~9 s2 ? - // for($i=0;$i<4;$i++){
8 }" ^6 f, ^; M - // $font=6;
' ^2 _9 i0 C$ r& i+ {% L. a) t - // $x=rand(5,10)+$i*100/4; i0 A) ]6 r# r6 H9 g4 o: ?( G
- // $y=rand(8, 15);& r! H& f/ t2 s: R7 u2 O2 Z
- // $string=rand(0, 9);3 W# B) D2 W/ K: h+ ]0 n/ S
- // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));4 G- T; ]' q9 c( \
- // imagestring($image, $font, $x, $y, $string, $color);
7 y6 B4 m5 p6 e - // }
2 ?" c- s$ o9 ?! W# L - $code='';) E! b5 S/ O- A/ i# D
- for($i=0;$i<4;$i++){' E1 G& K, x0 u/ x) U6 w- }( G
- $fontSize=8;" K) y. w. {1 D6 R6 \/ @
- $x=rand(5,10)+$i*100/4;. Y/ x5 b+ i" a( E
- $y=rand(5, 15);, N5 v) g- `$ K% B# M
- $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';) D6 D) |" A* n( s
- $string=substr($data,rand(0, strlen($data)),1); i7 o3 C1 _& L$ Y+ o \1 O" T+ O
- $code.=$string;! _; N% ~* d( Z2 T
- // 3.2 加载自定义字体$ c# a1 n L4 \4 P# `( h
- $font_file = '1.ttf';8 Y1 K$ s; e% {! Q+ f
- $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));
2 i2 z* }! N+ a7 f8 J; U Y* X& ^ - // 把文字填充到画布
, ]# l, O3 j% b' e( p0 ^, V - //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);# F: R" T$ G3 Y2 i5 }* c. K
- imagestring($image, $fontSize, $x, $y, $string, $color);
, F$ x/ I5 Z m6 u' D5 t - }
' @' `" _7 ~" Y& m9 V! c; X6 j3 v - $_SESSION['code']=$code;//存储在session里6 t, C% l: E( U4 ?2 _ R+ d/ O
- for($i=0;$i<200;$i++){
# h0 l$ ?; {3 Y - $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));
8 n- q) s( l4 q1 G6 u - imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);3 x4 c/ H1 D! J
- }7 h" V- \1 |/ _" Z5 f
- for($i=0;$i<2;$i++){
/ ?0 ]: p( Y% ]! q - $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));
! {2 U7 I' D$ x5 ~ - imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);0 N* G/ v2 j: b7 ~5 h
- }
P1 N* r% X- ~2 C/ n - imagepng($image);6 \ ~+ K- g! C5 a9 w
- imagedestroy($image);
' N% v8 L1 z, A3 k9 ]! ? - 6 p7 _. A3 i% r% v; N8 T( J& Y
- ?>
复制代码 $ g+ }8 @% E; F+ _$ b
form.php& O0 q# z9 _( h5 N
- <?php2 N# P- z; b& n6 i" m! R6 ]" d* j
- if(isset($_REQUEST['code'])){ L* j" b' G: @( B
- session_start();
" B; U0 I; g0 b - if($_REQUEST['code']==$_SESSION['code']){
! a- C- l5 ~7 a/ k! W/ t - echo "<font color='red'>输入正确</font>";+ T; C7 p. W* i+ v7 {0 I8 V
- }else{, c: M* a: P; f3 C& }$ C' c7 G
- echo "<font color='red'>输入错误</font>";0 e2 _$ [$ ?5 u# \1 W
- }, a/ `( e1 z6 Q( W; W6 M- U0 z* u
- }
; F, `4 Q1 @# y1 K/ q! e - ?>% {, r6 f9 y8 J6 y# p6 Y* W3 Y+ c
- <!DOCTYPE html>1 z; a6 m n" Y: X' p7 {5 e
- <html>
) s9 }$ Z$ ~* f+ i$ r' Y - <head>
: B4 Q9 w) `" \" R" [3 | - <meta chartset="UTF-8" />
, L% d6 y1 r' Y - <style type="text/css" rel="stylesheet">
, d/ V F/ l; ?& e: ? - a{
6 l! \( F5 j0 {0 f - text-decoration:none;1 u5 C' j8 a) H0 N/ A) w
- font-size:30px;
' z4 N# f! w. ?9 f; K; Z - color:blue;
" Q* N( o0 Q; @) c1 r! y" w7 T6 y - }
6 I5 c1 {) j+ L9 ^9 b, r1 s# \ - a:hover{% b* v m" m; h: w. y
- text-decoration:underline;
4 u7 p' x* L0 g1 _ - }! h2 u7 U6 `- Y
- </style>' A* d/ [8 M9 D
- </head>
, @5 _: Y# ?* N$ j' ` - <body>- \+ a V Q7 X3 r4 k2 h" Q
- <form action="form.php" method="get">
5 g; `0 j9 @$ n1 |0 {0 K9 h$ d& A - 验证码: <img id="img" src="index.php?" onclick="changeCode()"/>9 A) F/ e# v! T4 E
- <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />
4 s9 @/ a4 b# i! R; n - 请输入验证码: <input name="code" /><br />2 w7 C3 Q& ^% }
- <input type="submit" value="提交" />) Y& W$ S' W* R$ b: O
- </form>
9 e, B O+ k) v8 h; G - </body>0 [* x U5 h6 X5 i1 ]7 D& I3 ^3 V" v
- <script type="text/javascript">
+ e! V9 [, e* f) c( s, Y - W' s8 r5 S7 g6 K. j n, f
- function changeCode(){
! ~* G- G" w6 d+ U- X3 q: I2 u - var img=document.getElementById('img');7 u: ]9 I4 g4 W% c7 c
- //img.src='index.php?r='+Math.random();
$ t4 ?$ t# P' | - img.setAttribute('src','index.php?r='+Math.random());* J- n) B1 R3 K, ~* c
- }
3 }. I5 W# {' A) L3 [) ~5 O/ r - </script>6 Z7 T- ]7 @4 H2 W6 Y
- </html>
复制代码
7 M' r* X" O B H. v/ Q$ _4 B C2 P ~
|
|