管理员
   
论坛积分
分
威望 点
贡献值 个
金币 枚
|
index.php# F" f/ Y, X/ T
- <?php
+ c$ u- Z* q0 s' o$ z+ Z; c: t - session_start();
. h* ]0 s6 y# d# Y+ W - // 丢弃输出缓冲区的内容 **
( e' R( }& O) `( f- e- c1 C, C" N - ob_clean();
) C* M y# [9 S$ _( G0 y - header ('Content-Type: image/png');) E. V1 H5 A& p n& M; `
- // 1 创建画布
( Y# \" g( f% }% s" e. I - $image=imagecreatetruecolor(100, 30);
( O' N2 j2 e" C2 Q# z - // 2 画一个矩形并填充背景颜色为白色
# `+ K. e! P& c# { - $color=imagecolorallocate($image, 255, 255, 255);
8 P2 u- }8 V9 e# Z; e5 U/ B- ~- j: ~ - imagefill($image, 20, 20, $color);
, `+ P+ v! }0 C( m1 A* O - // for($i=0;$i<4;$i++){
) M' a9 ]$ Q% c% z6 _. L8 w6 d - // $font=6;& ^& O; i$ f8 I7 F2 l- ~4 q& D
- // $x=rand(5,10)+$i*100/4;
3 t2 h, x; A/ S x4 ?: A: T; B, b - // $y=rand(8, 15);" V/ ~0 u/ i! c1 s
- // $string=rand(0, 9);% o; H {) {9 n
- // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));
- E; A. L8 M1 `, T) t+ D - // imagestring($image, $font, $x, $y, $string, $color);
5 ^" ?9 b- x8 n- _. b8 c - // }
) X( q: Z; v' j - $code='';
2 e5 m; M" ~$ n5 X8 l - for($i=0;$i<4;$i++){6 B: @+ Q6 v% C7 M+ D0 O# H# h
- $fontSize=8;
) m* O8 p( Z( O* m5 R. ]' f - $x=rand(5,10)+$i*100/4;
) A9 Q2 ~: M f2 f - $y=rand(5, 15);
$ f- l% V2 E; O2 Y- Q' J+ q3 q - $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
2 m8 R, ?( l; h/ e4 R) n3 q - $string=substr($data,rand(0, strlen($data)),1);; u) p, e$ g1 h. l! ]* P" ]
- $code.=$string;
6 L% m1 G2 E% t, [$ h" ^2 v8 y* Y - // 3.2 加载自定义字体" x7 ^% j8 b2 g' j3 L* R& k
- $font_file = '1.ttf';
1 S# p1 m# n8 r - $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));# ]5 r& o( Q2 b6 h
- // 把文字填充到画布' ~ g' V' H: y$ _" F# g4 Z2 R" Y
- //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);
; z3 t8 q+ S( b! ^2 y6 g5 ] - imagestring($image, $fontSize, $x, $y, $string, $color);9 k, A; J# u6 M/ f% Q
- }+ f: j6 Z5 ^$ X8 p3 F
- $_SESSION['code']=$code;//存储在session里
|* k) s6 @- E - for($i=0;$i<200;$i++){
1 ?' O' ~5 b; n5 _, p2 U" |: | - $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));
, w9 H9 z9 [3 w- u& H- E- z( w0 Z - imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);/ C0 ?1 C U8 R& w/ C k2 f, M
- }
+ \) K4 S0 } I9 o& o - for($i=0;$i<2;$i++){
1 Y6 d; R( c. `8 X# U - $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));3 x- m& U; k3 o5 z6 E9 R. z
- imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);( I/ t1 F, U- J t+ z& @
- }! i N5 J* d( H& B
- imagepng($image);% [" K% i9 i; |; M) o( K8 ~; Q) {
- imagedestroy($image);
7 f/ W( ~/ ?5 E- K - % v5 o9 Y4 _' E3 d# t9 j5 U% m
- ?>
复制代码
# m; t7 o) {. R, ?% Hform.php0 T4 i/ O% ^* ]
- <?php+ ?. l# T: Z7 P; |7 b* S
- if(isset($_REQUEST['code'])){0 \. f5 b( T! `' N9 ^
- session_start();
8 W6 \2 x* g7 D d - if($_REQUEST['code']==$_SESSION['code']){
3 q3 X5 N& C0 H& s& j - echo "<font color='red'>输入正确</font>";
, f! l" z' _' B$ I5 K9 s2 j- A - }else{% d4 M2 ?2 U* Z& g3 o
- echo "<font color='red'>输入错误</font>";* L5 m; u1 x0 i% b5 k
- }
) M( N' {1 \, u5 B6 g - }, h$ i) @0 q! u/ i
- ?> j: w( y! Z- `5 w8 l3 O
- <!DOCTYPE html>
* K% b4 u8 C, f& J$ K \ - <html>
! x: y2 _ d9 `' r3 u" r" H2 \ - <head>0 U% q- X' w7 C4 Q; e7 a
- <meta chartset="UTF-8" />
0 D, g% x2 n6 L - <style type="text/css" rel="stylesheet">: u* q9 h/ k1 s4 z( J" e
- a{ K8 p6 L- E! D4 L
- text-decoration:none;% m9 g) ~7 ^* v* J& C$ v! O+ K
- font-size:30px;
; S3 ]9 T# g" M- O' Z: Q9 l. l - color:blue;9 h6 p- D9 N0 L
- }- l% K" a! y) @, b
- a:hover{# g; N4 k2 v2 R, g; A! |
- text-decoration:underline;
% D( t+ ^1 B6 V - }
0 b9 D, \: \' ~+ s B - </style>
* h, d. {4 @* D3 M6 p - </head>
) L1 K* ~- I+ K; X' e0 F - <body>
$ d$ T% B z q5 u% t* G - <form action="form.php" method="get">
& m2 o1 L+ c* t* S' E9 ?6 a - 验证码: <img id="img" src="index.php?" onclick="changeCode()"/>
9 L: ~! w" e) ^ - <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br /># H K, F l4 F' b: t7 j
- 请输入验证码: <input name="code" /><br />
u% }) x6 \% ]' N; w) r* ~ - <input type="submit" value="提交" />/ l: S B" U+ f2 V0 \
- </form>( t4 K3 n0 L1 s3 O
- </body>2 S( i, Q0 ^) |* o: u! D( ?. a3 P: B1 l
- <script type="text/javascript">) ?/ O$ ?# y5 v8 `
- 0 X( A1 _/ a( B" G
- function changeCode(){
* ^3 r+ D% W4 a - var img=document.getElementById('img');- i$ g, X# X, t# r, ^0 k) q; E
- //img.src='index.php?r='+Math.random();, y. N5 X1 e2 u5 |8 G
- img.setAttribute('src','index.php?r='+Math.random());
$ z8 V/ y" T5 Q# o8 ^" l( v {2 K; K- [ - }
" S% v% W1 H) ]4 a7 R8 w8 T - </script>
Z0 [8 a8 b7 i8 Z - </html>
复制代码
) S! c/ t0 A* w e" m1 t# ~+ X4 H5 q- F0 H- ?( t5 Q: O& R9 Y! k# b
|
|