管理员
   
论坛积分
分
威望 点
贡献值 个
金币 枚
|
index.php
. e# n3 @$ K+ z7 h4 m. P+ O- <?php
4 w/ s$ N+ d, n. y; C - session_start();
' U; L* K3 d+ o, J$ w0 ^7 c - // 丢弃输出缓冲区的内容 **
. L: s) B0 j, @ - ob_clean();( n8 D8 P: A! _9 G- R- ]4 T; H% r
- header ('Content-Type: image/png');
( D; M: T( ?$ o4 a1 G6 v - // 1 创建画布; ?# F G$ L: a* t
- $image=imagecreatetruecolor(100, 30);
" I2 v% j; _* \ R - // 2 画一个矩形并填充背景颜色为白色
( w; f" {- l' |: W& v8 ^. _ - $color=imagecolorallocate($image, 255, 255, 255);2 R4 s9 P" r; I8 l
- imagefill($image, 20, 20, $color);
n/ g6 R% g7 @7 x% X - // for($i=0;$i<4;$i++){
# x) y6 W; o3 ?0 G - // $font=6;
" L( P6 _, Z4 f$ B - // $x=rand(5,10)+$i*100/4;
) u1 t; p j" T! P2 P/ t# s - // $y=rand(8, 15);0 E% Y1 w# Y1 b I5 L; P6 H
- // $string=rand(0, 9);
9 p4 {8 X- L/ _ - // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));
D Z2 j# o4 |8 {7 R3 h( ]; } - // imagestring($image, $font, $x, $y, $string, $color);
4 B, r1 s" y. t% v7 `& x- Z - // }6 d1 U: e M: |3 t
- $code='';: x: c. Q9 g, K3 {1 w2 D5 b9 w
- for($i=0;$i<4;$i++){
' W; X/ x8 J, z+ J - $fontSize=8;! f! Z% L3 h, T- w3 \! Y/ T8 N
- $x=rand(5,10)+$i*100/4;. r- j# Y/ M$ b6 x9 }
- $y=rand(5, 15);: d2 B4 e( o' i- r
- $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
! A, [( j1 } e! r8 [- I - $string=substr($data,rand(0, strlen($data)),1);
7 `5 \! w h: Z - $code.=$string;/ c1 r# G0 x' I) D A* t1 G n
- // 3.2 加载自定义字体
! ^2 O; t% v T( Y4 ^& k - $font_file = '1.ttf';
$ K, t) t; H0 C# t; d1 o* Z - $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));
" `+ {. Q4 |; ^: ]$ }6 H - // 把文字填充到画布1 a& q, z- a: F) U" s) E
- //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);
Q7 i1 j/ S A2 I) ~ G - imagestring($image, $fontSize, $x, $y, $string, $color);
q) E ]1 n; x/ \ - }4 i; T) I& F- M! [8 n0 ], V
- $_SESSION['code']=$code;//存储在session里/ j7 G# i; W* r' r
- for($i=0;$i<200;$i++){5 b! p: ]$ r6 I8 q% E
- $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));, ^ v$ U5 L, G' Q$ N$ h" h
- imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);; n# h! r% r4 x+ w
- }
# l1 |4 `+ D' h, q) I3 V - for($i=0;$i<2;$i++){
8 H/ K2 @" {% ]* Q! h( `' G& u - $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));; W, M6 p3 L$ F+ Y
- imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);7 a3 v; P8 l5 N7 E) o, n; G
- }: z7 a* z/ \7 V% q
- imagepng($image);
9 H+ K* x4 O8 T/ }6 x3 f4 Q - imagedestroy($image);3 }& C& }0 v& k; _$ I8 x& M
( _4 v. V0 Z2 a) g) W- ?>
复制代码
0 Z) j9 s# n" p: P, x uform.php
/ @ |5 c2 t6 E0 i) T7 J& x- <?php5 W$ d. ?0 W1 x: {2 k
- if(isset($_REQUEST['code'])){
3 B! p/ V V, }. W - session_start();
) M# H/ z" l1 q8 X - if($_REQUEST['code']==$_SESSION['code']){
' p$ ]5 o, R' ?; j! G* @ - echo "<font color='red'>输入正确</font>";1 p8 L, S2 w6 j) Q+ s2 Z
- }else{
- w9 D$ U, f3 o; L - echo "<font color='red'>输入错误</font>";
8 ]6 d# q/ I/ J) A: ?7 S - }
- R: w9 r6 T9 ~; @ - }- b* i) p+ G1 ^6 |% I( q1 {3 j
- ?>
$ `5 D& l. F' Z$ N9 `% ]. f2 w - <!DOCTYPE html>
% ` P: X: }' N$ i- F9 F5 Z ` - <html>3 g: s! R' A* z9 N& u, Q* u1 y
- <head>7 M; C( R: V# A" ?5 N N/ r, ~1 P
- <meta chartset="UTF-8" />
6 D! ]: Z9 v d% b& U - <style type="text/css" rel="stylesheet">
; Z0 S& J( ^6 d8 d1 h) C% { - a{8 \* Z* u `7 V B' e% { o# s
- text-decoration:none;0 z+ s& @: O: Q" \. |# R# p
- font-size:30px;
8 C2 B1 ]. O7 f# w/ s5 _8 a/ d5 T } - color:blue;+ A/ H; ~ l4 I w
- }
( N$ P" a7 W6 r0 @ - a:hover{
1 s- B! e1 P1 `7 W& h3 Q0 ]7 { - text-decoration:underline;
' p7 ?, f$ n; s- \ - }
& M$ A" ]$ J, G6 V8 A/ \/ B; y - </style>
5 k3 B3 H8 L D7 c: e - </head>
+ X M& D) {$ M, P - <body>
% Z% S" k' Y" I6 _ - <form action="form.php" method="get">
1 e+ Q0 s+ C q/ y0 c - 验证码: <img id="img" src="index.php?" onclick="changeCode()"/>
6 O! e6 ^9 B, O& ]+ p - <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />
! l" Y. s O9 c1 L - 请输入验证码: <input name="code" /><br />
- e2 S' k) }0 @: u0 w' d - <input type="submit" value="提交" />. L( v" m% B* Y" }+ M
- </form>: l( W( g# o9 }" V
- </body>
$ D. Q% i0 }9 O) D+ Q% I" q - <script type="text/javascript">
$ r- D6 R/ c- e -
" G; ~* ?3 u' a! m1 }0 R6 ] - function changeCode(){6 E' z6 F. J' @' o
- var img=document.getElementById('img');
) U7 F0 `. ]! c3 o5 I* q - //img.src='index.php?r='+Math.random();1 C. w' g2 L0 X9 m! Q) g; \
- img.setAttribute('src','index.php?r='+Math.random());6 O' t' S5 g& j
- } v r7 y: s4 I& C
- </script>
3 R- L. F1 W P7 d - </html>
复制代码
" Q- I) _2 q' \9 i
' d/ @" G7 |4 ?9 A/ O6 e% I- E' H |
|