管理员
   
论坛积分
分
威望 点
贡献值 个
金币 枚
|
index.php% ?+ i9 G9 O: n
- <?php' n& I9 I4 o4 z6 D* G1 \2 v6 t
- session_start();6 Y& m8 Z, m+ A7 c3 g
- // 丢弃输出缓冲区的内容 **
) w3 q/ U |# q3 s - ob_clean();
# u/ ?- C# f9 K v: q9 W - header ('Content-Type: image/png');) x3 B( D& Q( r3 [
- // 1 创建画布# b( G. _' \8 D
- $image=imagecreatetruecolor(100, 30);
6 o" @! D& j( w0 S$ @ - // 2 画一个矩形并填充背景颜色为白色; M# n) D+ {! D0 n
- $color=imagecolorallocate($image, 255, 255, 255);
* C$ ~1 u, b) f. J. ^5 j - imagefill($image, 20, 20, $color); k# B# T6 v3 M3 r; M8 F
- // for($i=0;$i<4;$i++){
) z. T8 \( `3 X% H( B - // $font=6;% W% I) U e' `, q! h) o1 c
- // $x=rand(5,10)+$i*100/4;
" J! I: w5 w. w - // $y=rand(8, 15);
+ g+ I* }" b* y/ U - // $string=rand(0, 9);
[: `, \5 K0 D2 O8 d* p' _/ M - // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));+ W& }; Z# Y) g/ C
- // imagestring($image, $font, $x, $y, $string, $color);
4 J$ r. G# M4 G/ \* a+ o4 L/ t1 r9 j - // }
- p9 R3 Z& A3 a: h( S. L5 _ - $code='';+ t) m2 [$ a' M A! s
- for($i=0;$i<4;$i++){
8 E, ` X Y# ]" d. b+ k - $fontSize=8;
5 C% I" M/ I8 j2 w% ` - $x=rand(5,10)+$i*100/4;7 k. q x. ]6 C7 R4 T k- [/ a
- $y=rand(5, 15);
1 Q1 m) g d' r4 G8 y - $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
; @6 X: @% u4 e; p# ?7 N - $string=substr($data,rand(0, strlen($data)),1);5 b6 h. v v# s4 n
- $code.=$string;
1 a% D! d# r w) c - // 3.2 加载自定义字体
" a2 x0 u' B0 u; X8 L! U - $font_file = '1.ttf';
% q/ x( B4 v! K% B - $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));
: J" c2 E5 l4 o; J - // 把文字填充到画布2 K3 _2 i% A6 C: p
- //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);( O- J! D( X! P
- imagestring($image, $fontSize, $x, $y, $string, $color);. e* `( ~, ~3 H" U5 _/ O3 K
- }
1 h5 B& k) @4 `& h2 i - $_SESSION['code']=$code;//存储在session里& ]8 F- ]/ W. U; M4 i
- for($i=0;$i<200;$i++){
; a' ^! Q% N. L& n4 p - $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));3 J+ e- t$ N, U9 k3 W. A
- imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);; `9 H3 S4 X0 p' X
- }
' C) [# ]( T- u | - for($i=0;$i<2;$i++){2 R! s: B& e( F2 j* n# e9 H
- $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));
( T @# R5 q: y8 ]: E' [ - imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);
8 G; y8 k; s9 @& h% y6 o/ s - }
4 w4 G. W. i* k# c$ _# c" t) Q - imagepng($image);2 U6 B4 f8 x9 [/ }, d' h7 Y2 i
- imagedestroy($image);
: N( }, I$ n% y S, k - * d" M4 x$ e+ q1 c: u
- ?>
复制代码 # V W: j, ?$ ]) b& i& Q# u
form.php0 w7 p9 n+ q+ c* W4 U3 ]! J5 z
- <?php
8 z9 j& n" d2 Z4 k: F p - if(isset($_REQUEST['code'])){
' X" o5 o- M2 N& S& K' H8 X - session_start();. @, d* U4 P* G* i |0 D, e M z
- if($_REQUEST['code']==$_SESSION['code']){
2 v. f" H( t9 M* u9 E - echo "<font color='red'>输入正确</font>";/ Y0 J) [. }* T# R- p% [: a, \
- }else{7 E* S1 c; s5 J: L- \0 [
- echo "<font color='red'>输入错误</font>";* V4 n. e# G9 s# c
- }. Q0 ]4 D. ]9 G; S& Y5 F. |) N
- }# N* E D! Q e- c( Y- [5 _
- ?>2 V/ ^* z- d/ g# |
- <!DOCTYPE html>2 k0 _% A8 }- d1 E; Z: Z0 h0 f9 [4 c
- <html>
! c% l* X( j7 t6 e) A% g - <head>1 B8 s" F' I! s! x3 W
- <meta chartset="UTF-8" />
' V$ ?4 x- F4 s# o* w8 g$ | - <style type="text/css" rel="stylesheet">' w+ d! m7 e, N3 M2 x" [
- a{) h+ ?4 y# }* z1 A r
- text-decoration:none;
8 Q! ~: ]$ q7 D - font-size:30px;/ O7 Z7 z% D( `( L
- color:blue;# R! z! G4 S" }: _; B( M1 |, C
- }
6 q/ k0 I% U5 Y0 J) f+ O' } - a:hover{
1 z6 s4 @; v; a* \- j - text-decoration:underline;6 W* m; j q% }7 v
- }. K& ]8 J7 ]; t8 U' w% \( k) D
- </style>
* ] C& O8 T5 { - </head>
, l, h8 t+ t/ l' j( }; m: Q - <body>
/ S1 b; w8 k2 f0 x3 a, ~ - <form action="form.php" method="get">
q! p' ~' z( k8 W2 g8 a - 验证码: <img id="img" src="index.php?" onclick="changeCode()"/>: A) {. ] x, |' F3 v
- <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />
3 E# _4 E* _ P4 D$ @* V, R& z - 请输入验证码: <input name="code" /><br />
, y% o" P# Y) e5 E. z1 F K$ J - <input type="submit" value="提交" />; b* I) J3 z9 H5 j
- </form>
9 r3 t3 \$ F4 H* [/ } - </body>
5 n& m/ d; ?/ J9 c - <script type="text/javascript">
" D# ?9 N" y+ \ - $ ` t6 k5 i9 P5 q
- function changeCode(){
; a0 w& p+ }+ k9 }9 x0 ^0 q' ^ - var img=document.getElementById('img');# Q2 q3 C5 z# v& J
- //img.src='index.php?r='+Math.random();
& A" f- Z6 {- r6 j8 s, D5 S - img.setAttribute('src','index.php?r='+Math.random());
% K! B- H! a: p* W" w - }
# n+ H1 ^+ o# u$ q$ x3 [ - </script> u: ~0 T8 |* R3 P
- </html>
复制代码 ' _8 x& B! l% d5 {
3 U/ j5 S; t; q! q; X |
|