管理员
论坛积分
分
威望 点
贡献值 个
金币 枚
|
index.php1 J* x& T9 R9 t% Q8 V. L, G' L
- <?php: K( R `! o3 W+ N+ ?+ e! e: z% x
- session_start();
2 a4 \2 ?8 d j1 \" q8 @ - // 丢弃输出缓冲区的内容 **# L( a X$ \3 E
- ob_clean();
: S$ U/ }* |' R - header ('Content-Type: image/png');
, t# L8 R+ V u - // 1 创建画布* j" w* j* e. D0 K" Q: Q
- $image=imagecreatetruecolor(100, 30);% q. l5 N5 m8 a `: J( w
- // 2 画一个矩形并填充背景颜色为白色- y0 F/ M' g& D9 D. h
- $color=imagecolorallocate($image, 255, 255, 255);9 b. v3 l: m7 ~9 V1 p6 A
- imagefill($image, 20, 20, $color);
T2 O* ^ H5 ]: p- y' x7 { - // for($i=0;$i<4;$i++){9 N% R9 ], f* S
- // $font=6;: h) ^3 o& e2 e/ d* {4 @, ^
- // $x=rand(5,10)+$i*100/4;# Q$ J: r9 f+ K2 e8 f
- // $y=rand(8, 15);
1 j3 W1 ~' k& E! C$ i - // $string=rand(0, 9);& t5 s5 O+ i7 ^; K! U
- // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));
$ B( R6 x8 l, @+ ~ - // imagestring($image, $font, $x, $y, $string, $color);- b y# y$ k' I7 n
- // }9 n& M+ Z6 U% m/ Z$ _7 a- j% u a
- $code='';' J" w3 a! A: t9 e' V0 L, ^9 D
- for($i=0;$i<4;$i++){
3 s' ]1 @6 ?3 H( [( k: j# ` - $fontSize=8;: B5 \6 G- p% h
- $x=rand(5,10)+$i*100/4;0 U! d B6 {1 V
- $y=rand(5, 15);
8 C+ Y; y/ k3 ]% n/ @# G' u - $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
- ]2 k) x: r. m1 P3 @9 k. Y - $string=substr($data,rand(0, strlen($data)),1);" w$ v; S: G7 m& c* x5 x
- $code.=$string;7 K' ?& V$ I* L1 j" G
- // 3.2 加载自定义字体
9 M( z Y0 z6 \5 E0 G9 c% g ^2 _1 d2 t$ [ - $font_file = '1.ttf';& {& K5 r* Y3 N+ v* i
- $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));0 N. N4 Y) c- x% r! {
- // 把文字填充到画布
$ g( D9 @( r: E6 r! H& B o - //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);( Y0 z- v9 c3 F$ D
- imagestring($image, $fontSize, $x, $y, $string, $color);
) ` o3 m. [! c: M - }
. D* f1 |& u& h4 N8 P - $_SESSION['code']=$code;//存储在session里
& o( C. B! e* i' E* d - for($i=0;$i<200;$i++){
5 @8 X2 @2 ]8 i8 m6 a! V Z - $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));( u3 s; T; @& y+ s: a
- imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);% a2 r k; R$ [
- }
' g% l8 W( U+ a - for($i=0;$i<2;$i++){" q; @& p y ~& d* H
- $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));8 v3 I( q) C. H( g
- imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);
4 _% o1 E ^. q( W7 j% A - }: c8 C$ H; Z; i3 L$ _& ?
- imagepng($image);4 _3 m, q% m% l8 d$ h
- imagedestroy($image);
+ X/ w/ c6 |* g( H$ Q4 f - 9 S+ i% U! y0 q( ~! B* F
- ?>
复制代码
3 ]' }' M" X+ n4 f5 U% dform.php
- D* S6 E* {, Q- <?php
% j0 y" o9 Z9 Z - if(isset($_REQUEST['code'])){
1 J- s" s1 R1 V3 f. e9 E - session_start();
7 E# m3 G$ k1 J! D, p - if($_REQUEST['code']==$_SESSION['code']){5 Y H* A+ h9 Y6 X/ j. G
- echo "<font color='red'>输入正确</font>";+ w- a. a7 Y6 m4 ?
- }else{
. T& p$ W* S* B - echo "<font color='red'>输入错误</font>";
% a/ B( e! p. p1 m( g - }* H) H( V- O n! R4 b2 F
- }
" f( W: P6 ^0 W1 Q" R( @ - ?>
. S3 X, d* Q% O. Q - <!DOCTYPE html>$ b. r2 U9 E: b3 R- \, w
- <html>
0 @1 V- w0 i* B* f - <head>
2 V0 s5 l9 N" f o; A8 f9 U9 O - <meta chartset="UTF-8" />
+ J+ v: ^0 v! B4 ^! D - <style type="text/css" rel="stylesheet">: M# V5 B' g! k2 e7 g
- a{3 h, L7 F' W' t) x
- text-decoration:none;
# U! V- ~7 \, ` - font-size:30px;
( R1 b7 _3 A% Z# X4 W - color:blue;
6 }' r# A; t: h - }+ r5 J& L6 s0 b" z: y: t' R
- a:hover{
; h( Y' H3 n& R: j& f% S - text-decoration:underline;
$ d! u$ E" [1 }7 r/ ^; a - }
6 a6 c& z# |! k5 ?* V: x - </style>
/ n& e3 ~" Q2 a) i8 Q! S - </head>& |) l: X5 T7 c' l- p- J0 z5 w/ p- m
- <body>- k' T' f0 m) ~" w7 z
- <form action="form.php" method="get"> Z. i q L6 |# ~# `
- 验证码: <img id="img" src="index.php?" onclick="changeCode()"/>
K9 d" X- E/ u1 u - <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />9 h( N w8 s5 u0 _( ~
- 请输入验证码: <input name="code" /><br />
$ \5 N2 A8 W3 x - <input type="submit" value="提交" />0 f+ T* V9 G) i
- </form>: ?6 ~+ X2 m; h5 g
- </body>: ~) A4 G8 h* K8 P
- <script type="text/javascript">
% \5 P. z: s3 u7 H/ X, f: ]+ p - 8 K. t) w; a- y
- function changeCode(){0 I2 S+ `5 w9 N! [( p: Y5 `
- var img=document.getElementById('img');0 }1 O3 t- H2 e" q
- //img.src='index.php?r='+Math.random();+ v6 f3 P( p( w/ W1 B" }
- img.setAttribute('src','index.php?r='+Math.random());
' v4 J7 _- Z( F* {2 c* Y - }) {8 ^ Q( Z3 V! T% R; \( [* i
- </script>0 p, o- x. n/ m- N
- </html>
复制代码
* |6 A7 b6 w- W0 J7 w2 c8 w
" {5 p# L) x/ D F) T% }! [5 Y# u |
|