管理员
   
论坛积分
分
威望 点
贡献值 个
金币 枚
|
index.php) S) {. j# [/ m# L
- <?php9 H5 J$ \7 w8 S# u3 U) a+ J
- session_start();
3 h( ~5 O4 b R3 A, X4 |! Z0 y - // 丢弃输出缓冲区的内容 **
, M1 W' ~6 \5 d1 Y- J, _5 q - ob_clean();
% l: O$ u& ]2 n1 f' X - header ('Content-Type: image/png');; [! N0 n/ U9 W! {& a6 C- @
- // 1 创建画布
2 y. @0 v2 l1 T - $image=imagecreatetruecolor(100, 30);
+ @$ o5 E/ {, _* C - // 2 画一个矩形并填充背景颜色为白色
, J. r' g2 U* T- @% g, @ - $color=imagecolorallocate($image, 255, 255, 255);# @. v7 o3 O- P+ s+ u
- imagefill($image, 20, 20, $color);
0 \: M) x: C1 b" Z# w' g - // for($i=0;$i<4;$i++){
5 j0 U7 \0 f: w6 _: ?- W - // $font=6;
; F. R1 Q! ^/ C( J, v+ Y - // $x=rand(5,10)+$i*100/4;: D4 h* }3 T( s# B4 Q6 ?7 `
- // $y=rand(8, 15);
3 v' @* a+ P3 K+ G& m' R- L: q) l - // $string=rand(0, 9);
8 x" ?& U8 J5 _4 q7 o - // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));9 J# V) ~9 Z& s1 B& s5 |' G8 x
- // imagestring($image, $font, $x, $y, $string, $color);
, i9 W b% b) t - // }; J1 d# Z! G, a; t
- $code='';! I a" |: v T4 E% n7 ?' S
- for($i=0;$i<4;$i++){ a# v8 j( g. f! k
- $fontSize=8;# j0 F% f' T2 V; j5 [: ]3 E% v! S
- $x=rand(5,10)+$i*100/4;
6 K: w4 s* E4 Y2 N/ d- z - $y=rand(5, 15);
7 J& \" E5 y4 L0 m: s$ ~/ \4 u0 y - $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
/ P: f8 Y, {0 o# i% O7 d - $string=substr($data,rand(0, strlen($data)),1);- p/ ~* @, k# R+ `2 t
- $code.=$string;
/ i: f* e# u6 y" E - // 3.2 加载自定义字体' l- _1 V! ~8 \0 y6 R6 q7 R# ^& Y0 Y, i
- $font_file = '1.ttf';7 i8 u. \$ S) V
- $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));
, i: {9 w% s) Z+ @ - // 把文字填充到画布) a. X+ ]" v' A1 R
- //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);4 E5 C4 J- L6 P6 C- a8 ^
- imagestring($image, $fontSize, $x, $y, $string, $color);1 `! f5 w, U% z$ R7 B
- }4 m) D* O! Y7 i" ]6 ~1 s
- $_SESSION['code']=$code;//存储在session里: m8 o& v' B! r) A$ n
- for($i=0;$i<200;$i++){# w2 U; l& i8 l# {1 Z a
- $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));" Y, B8 D, x5 D
- imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);
& { e/ l/ K. n - }* L' e8 X% L2 i- H4 x' q
- for($i=0;$i<2;$i++){8 h3 S0 Z7 m& k* l2 M
- $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));
+ ]. i, k' [7 T% f$ t - imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);! z7 e e' R& H/ d: ]( M
- }& `! {& S5 @, Z+ j9 y
- imagepng($image);
0 g+ P, q9 f N) _, q3 D - imagedestroy($image);2 i. q6 \+ z5 Z/ D
, l* @* ^$ V- n* ?- ?>
复制代码
* B% Y$ U" ?3 Mform.php
2 Q& H4 V9 U G. f. ~0 E4 C9 k- <?php# o5 b* r- T+ j3 z
- if(isset($_REQUEST['code'])){. ^8 y4 S0 |6 P3 M) L; d
- session_start();
) {9 ^+ Y9 }& y& N( x3 N8 ] - if($_REQUEST['code']==$_SESSION['code']){
6 u: P; h* X+ V! E: j* @ n5 Q2 { - echo "<font color='red'>输入正确</font>";
1 b' n& m+ J% b! e% i - }else{
! Q9 v$ D: U1 Q' t' F( t) w - echo "<font color='red'>输入错误</font>";% g0 B) b9 Q8 u6 Q9 D7 R
- }7 M, I( ^% t1 E+ K$ m2 o
- }, g1 v) s9 |, |" ^& o4 j" N
- ?>3 A- f+ P* I8 Z
- <!DOCTYPE html>. A5 K: c: `! p# ~" ?5 ^
- <html>
/ ]4 r6 ?! G8 H9 z6 L - <head>
. Z; h# y9 N5 h( a5 V. b' N' v - <meta chartset="UTF-8" />
" t' j# w6 u9 {" W. a! o9 _- v; t - <style type="text/css" rel="stylesheet">3 r' s9 a+ T% F
- a{' F5 n* H" P; X# R9 A" i
- text-decoration:none;+ h, [. O3 J) t% S i) Y
- font-size:30px;( ~; k! J }) U& o/ C
- color:blue;
4 o! P: T) s. e# N8 O1 ]& Z - }3 j6 `' @* }6 o% O
- a:hover{
" g8 I) i G* s" }( i0 \" c# m - text-decoration:underline;( e$ G/ [, F* M' p; l) {" ?7 P
- }0 L" L( P. Q1 G- W$ A3 t
- </style>9 ]; Y% D# z: t$ f
- </head>/ w% X1 G8 e7 ^7 v# A
- <body>! W8 O% |5 I4 ^" f1 f
- <form action="form.php" method="get">5 ?: k' D! ?* r5 Z7 h
- 验证码: <img id="img" src="index.php?" onclick="changeCode()"/>% h6 Q F1 N8 c6 V; T* n
- <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />
! B6 p% O: l6 [ - 请输入验证码: <input name="code" /><br />
, y1 T. l5 f5 _6 y/ s7 H - <input type="submit" value="提交" />7 H7 Z, L" X* F5 ?
- </form>
2 t; G9 p3 U" ~9 ` - </body>
5 e: U6 M4 I/ I! I3 y1 t8 K - <script type="text/javascript">
P6 o, g; I) y7 O; b -
. ^ ]3 Q2 i" G6 x& V - function changeCode(){
& d6 ^4 l& C+ Z# i7 D6 ^ - var img=document.getElementById('img');
5 {9 f3 F) L" p6 t0 f - //img.src='index.php?r='+Math.random();/ V: g& m9 U& B3 m- K$ ^ {
- img.setAttribute('src','index.php?r='+Math.random());! o0 G$ i& G/ w
- }
. I4 \; U f5 ^5 f - </script> e$ j! s% a4 d7 t5 u0 w, ~$ p
- </html>
复制代码
1 m3 {3 J* A; N
2 ]8 m; ?: H3 r4 j7 @ |
|