管理员
   
论坛积分
分
威望 点
贡献值 个
金币 枚
|
index.php. }7 J6 N7 t, q& M
- <?php
t1 T, B/ y- V- U - session_start();3 E9 a# H% {# \3 N
- // 丢弃输出缓冲区的内容 **
6 A; |# k; R0 B. F - ob_clean();- X6 W8 n: c/ i3 z, h
- header ('Content-Type: image/png');6 ^+ i) J2 B" B' q0 ?$ f
- // 1 创建画布) ?& r2 T/ S% H3 q" w
- $image=imagecreatetruecolor(100, 30);" V! |; F' A8 f+ _. N8 d$ O4 [% o
- // 2 画一个矩形并填充背景颜色为白色
$ S/ r p+ ~. @1 ?; u Q - $color=imagecolorallocate($image, 255, 255, 255);
3 A" Y# q9 \. E) N# Y5 i: P* T - imagefill($image, 20, 20, $color);
; i Y0 i c0 _" V2 u4 D6 t - // for($i=0;$i<4;$i++){
# I+ v6 w, A+ E5 d: Q8 E" t$ [ - // $font=6;
/ X* Z- `* T3 h8 x! S - // $x=rand(5,10)+$i*100/4;
# j# S$ U* z" k1 P - // $y=rand(8, 15);: y- {2 k4 O3 Q) w% m' _/ i6 l
- // $string=rand(0, 9);
5 x E) K: r- u$ I - // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));
' d- Q% m0 b/ x2 @' H* [ - // imagestring($image, $font, $x, $y, $string, $color);
9 j* ]; Z3 v8 @) M - // }
4 Z5 B+ M4 K$ N% l# n - $code='';
/ N# a* Y1 R: G* \( S - for($i=0;$i<4;$i++){9 G* l: s) O6 x/ b/ Q8 d/ A B
- $fontSize=8;
3 ~: ] k' n+ A3 d) W9 p- a ~ - $x=rand(5,10)+$i*100/4;
8 \7 G8 H" _- R: L* ^ } - $y=rand(5, 15);
; D5 y! q: ^+ S3 {( e - $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
4 |) ~) X9 B7 P$ g - $string=substr($data,rand(0, strlen($data)),1);
6 H! S" j9 h2 j% M$ Z2 Z$ `/ E& b7 B7 I - $code.=$string;( `* K! G$ g9 X. v
- // 3.2 加载自定义字体8 J& K, A" D+ [! A8 R K" ?
- $font_file = '1.ttf';! l* Q1 W+ m6 j3 G/ r8 w" T
- $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));1 @$ h' o$ I }2 z# F1 l
- // 把文字填充到画布+ W8 L" C( \" M" Y8 A0 p: G5 @
- //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);5 s* |& P! o2 t' V/ _0 d2 D% X6 ^
- imagestring($image, $fontSize, $x, $y, $string, $color);
, D# z4 x' m3 c) ] - }7 E5 G5 Y* h; R" y
- $_SESSION['code']=$code;//存储在session里
7 C0 ]8 ]. F- d/ y - for($i=0;$i<200;$i++){ V! F1 ]2 ^. l0 i' F; x
- $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));
" F- k# v) @2 I" p) w - imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);) s |' ~2 D4 H4 b. a" l
- }! Z- _1 Z. H1 C8 D9 B0 T
- for($i=0;$i<2;$i++){) g( u, r$ K; _$ [3 @% j
- $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));
7 C. C' \9 h; E6 H& z - imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);
; P$ W% n3 v" I% t- U - }
; p o' H0 ?8 z - imagepng($image);! n4 S- v- B8 s- X) ~
- imagedestroy($image);
; u9 c& z. ]" `9 A - 2 z1 F, b( r# G6 `+ i8 p; S) Z1 G
- ?>
复制代码
9 [( Z- ]5 R& O+ m& h3 _. hform.php% }3 ~2 v5 R0 X- w& v
- <?php0 \0 L8 n6 o7 H" v
- if(isset($_REQUEST['code'])){
, a/ h" U6 u# n - session_start();0 w) q0 }" T# f
- if($_REQUEST['code']==$_SESSION['code']){# y, v. T, C( f7 G! X' m0 b
- echo "<font color='red'>输入正确</font>";
2 h; z) \0 j) N - }else{* v/ u( T% G, m1 z5 G, E3 m5 m
- echo "<font color='red'>输入错误</font>";8 `* y x4 w8 i6 o; L
- }: G# I2 t; n, R I& n
- }
* l8 e4 ^* N" r% b - ?>
( C7 X# l2 v( A* W2 M - <!DOCTYPE html>
: F+ P: h4 u) q7 b# O) P8 h - <html>' ~: y0 `2 E, }" x3 X& R# T
- <head>/ |' ^% d' R9 q5 ?8 b0 z; X
- <meta chartset="UTF-8" />
7 {( s, q) N" T, h9 V) e* K1 `* b - <style type="text/css" rel="stylesheet">
/ ^8 a1 e7 H$ P - a{
& [! t6 k" C; L4 c7 d' D* c - text-decoration:none;) T1 v, W, k$ [9 E# ]
- font-size:30px;" K6 J6 L# n/ {) ]4 s
- color:blue;1 w) a0 J1 o/ q" k. p
- }
7 U( y @, j3 ^: A - a:hover{
+ X% @- ~/ c* V - text-decoration:underline;' I$ O* K8 ]5 E. D* {/ P0 D
- }$ a O% N! {& L# Y; @
- </style>0 t$ N% P3 w, h6 g6 J
- </head>0 r) X. m# b% ]+ |( M
- <body>
! A ?: Q4 X% ~3 |$ q% u' I& l - <form action="form.php" method="get">7 N* e) c- Q) H* u' E
- 验证码: <img id="img" src="index.php?" onclick="changeCode()"/>
, V( n' `0 W! f: | - <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />$ @; `- P( g% b+ I
- 请输入验证码: <input name="code" /><br />9 t% Y/ D8 }2 }! `3 g& O/ o
- <input type="submit" value="提交" />
) w8 \, @' o$ b. T - </form>
- H6 `8 O8 [. G - </body>
# W q- p" J- m5 o& h - <script type="text/javascript"> N9 R7 t" ?- p: V6 p
- ! g3 P- }2 L+ H% y" G7 ~1 t2 i
- function changeCode(){& n% \: x5 e& [: s1 ^) V; r
- var img=document.getElementById('img');3 }4 p3 F0 L' c# B5 \& K, A
- //img.src='index.php?r='+Math.random();
" D' e4 S. f$ Y9 y' l* s - img.setAttribute('src','index.php?r='+Math.random());- X+ f7 x8 X6 ?( Z% s1 x; K
- }
4 u/ n: N- I8 z1 [! u - </script>+ [ f5 }6 S+ k* z8 h
- </html>
复制代码
1 s, e, {/ L" h' C
) C2 H, F: ?/ s% I0 t% p Q" s3 m |
|