管理员
   
论坛积分
分
威望 点
贡献值 个
金币 枚
|
index.php
- P4 Y# l) r4 g- h1 z- <?php* V" H% \- P9 \/ F& }- h5 F
- session_start();$ }% z6 ~ m, |/ U. W
- // 丢弃输出缓冲区的内容 **
' N" i& R( C% [1 B3 n7 L [% u - ob_clean();5 D: Q5 C5 M6 P, e6 Z4 ?
- header ('Content-Type: image/png');
4 |+ I0 }0 V N - // 1 创建画布7 q6 j ?7 ` u
- $image=imagecreatetruecolor(100, 30);; Y0 h$ c/ w: t- ~& b
- // 2 画一个矩形并填充背景颜色为白色 K& v% S" ~& [! j
- $color=imagecolorallocate($image, 255, 255, 255);7 ^2 n4 O3 K3 M' Q# w3 a' k
- imagefill($image, 20, 20, $color); k/ e9 M& O5 G$ J
- // for($i=0;$i<4;$i++){5 U, u' V! H R$ X! W
- // $font=6;
1 s# P0 m+ R) d2 q* K5 {, O" Z/ J - // $x=rand(5,10)+$i*100/4;$ z: d+ o2 { I& M- B
- // $y=rand(8, 15);5 t1 X E) a: U$ ?) h
- // $string=rand(0, 9);$ C5 t$ J: @; N6 J
- // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));
1 a; o2 a0 T& C: O4 B( C6 }: J' r4 L - // imagestring($image, $font, $x, $y, $string, $color);
! \5 Z$ y3 ?2 p- \1 a - // }) o+ J# q& [4 d- Q* j2 g
- $code='';
0 j/ ^7 d" d1 c% B6 e& v9 [" I - for($i=0;$i<4;$i++){
0 D9 B; ~/ g1 o: [ - $fontSize=8;* d' u' K p3 ~2 {- `6 K
- $x=rand(5,10)+$i*100/4;
& H* ^7 I4 O# ~: |( E5 h5 l - $y=rand(5, 15);
* n% |% ]4 W: X/ S, x- z. X6 w - $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
# s( w: l& T, r% }4 W - $string=substr($data,rand(0, strlen($data)),1);
1 W" s* c+ [' L3 l - $code.=$string;/ u% p6 q! _8 O5 C, c: ~
- // 3.2 加载自定义字体
& m: E% {% Y9 m2 S8 N v - $font_file = '1.ttf';( o; ]( v1 U* ^' y. p* w3 O
- $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));7 r& {2 A) R* i, ?7 i
- // 把文字填充到画布% H1 W/ G% E3 c5 Y, g& F
- //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);3 G0 p8 f# |3 t( |
- imagestring($image, $fontSize, $x, $y, $string, $color); L) y$ U! E7 c& G; E, @8 M* a* V
- }
2 R% \, o3 a3 g: q. ^1 z' p# o - $_SESSION['code']=$code;//存储在session里+ V" T. W7 d5 T6 E7 r
- for($i=0;$i<200;$i++){! N% G$ Z3 N5 {" o- ^! V
- $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));! N0 L3 @ a, j/ e! O
- imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);
/ n% Q$ H% @3 T' f7 Z - }8 K/ q# d/ p4 l- i* }8 L/ R
- for($i=0;$i<2;$i++){
9 D: x# s j0 i% [8 n1 g - $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));
7 ]3 k) U! ]! b- e5 g - imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);# e$ R, t g- m+ e- c
- }9 F: l4 S P6 |8 Y; ^& z
- imagepng($image);
3 q* V! b" d& ` n9 u0 ` - imagedestroy($image);, L* \6 q8 U/ K8 z. q9 J& K0 G
- N1 J5 [' [. b3 g. y/ N& h- ?>
复制代码 $ k$ N5 T9 v9 E* G9 P+ W' T$ X- {
form.php
7 m/ A: @- N: w7 b' }- <?php6 ?9 m7 b" p- M$ Q2 O
- if(isset($_REQUEST['code'])){
" J0 }; y. \. [' h* p) o% {& ^ - session_start();6 F- |& D% U/ S+ Z/ v
- if($_REQUEST['code']==$_SESSION['code']){# N7 P; X' [5 t5 Y) r0 V) p
- echo "<font color='red'>输入正确</font>";
- N( \# W3 {/ G6 b r: o - }else{
: M& C0 l7 d Q. C6 h. w0 E - echo "<font color='red'>输入错误</font>";6 d9 [7 Y3 v/ F F
- }
9 [2 L) I! K! E( k' ~. C/ { - }
* u) j6 s$ I( Y |6 d* s# B - ?>* ?7 C) Z2 I8 w% A% v# t# @2 L
- <!DOCTYPE html>
' f5 p1 r$ g) H; U& I0 t - <html>
- i- ^) w% X1 {5 d! a) g. J H6 P - <head>: e/ _# Q/ E) d' A
- <meta chartset="UTF-8" />* ?( i* T6 G8 l& b) w7 o
- <style type="text/css" rel="stylesheet">
' N) q7 Y5 s6 F2 n$ N( T7 G7 i - a{4 f P$ {: \, M( R5 [( G
- text-decoration:none;# S- O0 @- v4 H) q5 a
- font-size:30px;
$ n1 ], Y% Y4 {1 S0 r; W - color:blue;4 y. u' X1 g9 h; T4 `3 u; v& {; u
- }
$ q( z: }6 ?5 o - a:hover{2 ?- d: [ c/ Q# l
- text-decoration:underline;
' t, p! h4 R: \ - }
! ? S# S- y1 |( ? - </style>( ^ [6 P& O u5 p2 c1 ]6 l
- </head>1 V" k0 D1 \; C) Y
- <body>
$ k! B b* `" W1 {1 U - <form action="form.php" method="get">
, y' z) _6 x- U# T; o7 d - 验证码: <img id="img" src="index.php?" onclick="changeCode()"/>$ b. C6 @6 F" u- w( L& J
- <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />
& ]5 j8 O, \5 D2 R& i5 d: x2 N# F; x - 请输入验证码: <input name="code" /><br />' c/ Q6 Y! m( A; N0 e
- <input type="submit" value="提交" />/ p u. u2 L& e3 i3 ^
- </form>1 j0 R+ o6 i3 j) D* p
- </body> V! H, R3 H$ v/ f; u
- <script type="text/javascript">* X0 L* X! {# j& g9 `
- . y$ I+ z6 O% G; _3 E! g9 E
- function changeCode(){
- z( H! m1 ^8 c - var img=document.getElementById('img');4 `; G5 Q4 t9 H+ G% R- l( |
- //img.src='index.php?r='+Math.random();* D9 q5 T' J) C% L5 c4 R- t
- img.setAttribute('src','index.php?r='+Math.random());) i) e# @+ _+ n F) Z; ?; M1 }9 E
- }
5 s0 [" Q4 g0 I% j& o) g - </script>
! _ Q. s& V6 b: I6 U% l$ d - </html>
复制代码
$ O: e, e- b. |, a$ [0 P+ \: P' X/ ^
|
|