管理员
   
论坛积分
分
威望 点
贡献值 个
金币 枚
|
index.php l+ N( Y- q/ [2 n
- <?php
5 z+ E8 s( ~! R4 _1 I! y. k) A - session_start();
, c1 s! ?4 q& }2 O - // 丢弃输出缓冲区的内容 **
/ H) q9 m% z+ V/ e7 h3 Y+ a# T - ob_clean();
2 ]7 I0 X' `: I {6 t+ v9 _ - header ('Content-Type: image/png');
/ z# ^( B/ P8 h c$ p - // 1 创建画布
4 O6 E' c4 `1 x; X7 d1 {" M: |! C - $image=imagecreatetruecolor(100, 30);
5 k4 W) P) i. ~4 |2 y8 \% b - // 2 画一个矩形并填充背景颜色为白色
: {, Z- m3 a0 l. L - $color=imagecolorallocate($image, 255, 255, 255);6 K* D P ]( }5 v3 U: c. n- R
- imagefill($image, 20, 20, $color);! w8 m) X6 l* {. z0 ^1 x
- // for($i=0;$i<4;$i++){
! w1 G, ]& T( ?1 k8 d1 b% z - // $font=6;
2 {/ ?' C- E) e1 |' J' E - // $x=rand(5,10)+$i*100/4;: E5 L# Y6 D1 T4 e* g6 x
- // $y=rand(8, 15);
e1 e; ?$ a. x3 b$ s) s9 X - // $string=rand(0, 9);/ s5 o# l! H8 w* f
- // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));
3 D6 I' c5 S! Z4 C4 q$ F, m m - // imagestring($image, $font, $x, $y, $string, $color);5 ~3 v5 \/ b5 S: q1 D' [
- // }
; t% d, E, _" l' [, W% s - $code='';8 K/ i! w5 T/ O# X, F
- for($i=0;$i<4;$i++){0 C) e' [- w- { ?" B+ d2 }4 U
- $fontSize=8;0 X4 |8 c: v8 x" \' I
- $x=rand(5,10)+$i*100/4;
& A' J% U8 X2 P" ~8 U7 h2 O - $y=rand(5, 15);
* [ B: E: S* }7 s - $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';9 v8 t6 H" S3 ]! |' x) J- c6 E1 k
- $string=substr($data,rand(0, strlen($data)),1);1 V8 i" S1 n! ]% k8 }
- $code.=$string;
* {+ n! G" H! f+ A9 m: D. c - // 3.2 加载自定义字体/ H3 F& J( ?7 _
- $font_file = '1.ttf';; K; ]0 R9 H6 F. l' g# \
- $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));) H) a+ y$ H g" |. y6 h3 Z
- // 把文字填充到画布
; F% H) Z( N9 g& u7 s( n7 C) ] - //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);. f% I2 P W' |
- imagestring($image, $fontSize, $x, $y, $string, $color);
% J8 w: @ b3 V" K5 m8 q# p! y9 u - }
$ w. C8 C0 x% E. q R' m - $_SESSION['code']=$code;//存储在session里
9 X. M; m$ c/ ? - for($i=0;$i<200;$i++){
$ O; s5 e+ V1 e5 { - $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));
E; [. P$ [' h# j, ^7 x - imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);
0 d+ {1 B4 {* ^% k9 J0 D- u - }
9 S m" Z2 x3 f s1 y. F! @# d - for($i=0;$i<2;$i++){* m3 q L5 g; ]9 j- S# N" C. n* S
- $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));
( U% L1 X$ _! c. b$ X Q& M+ C - imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);
/ ~3 i! R- W9 w, @% z" g: l7 R - }
# K" T: n- O$ V( @) j - imagepng($image);$ t0 ~8 n/ `3 Y4 F& p7 E6 j1 M
- imagedestroy($image);
4 t8 j( X T) O# d7 {, k- |
5 C! U/ v& U9 S- ?>
复制代码
/ X( i/ S7 a7 Aform.php
+ d& r S) }- t) v6 ^- <?php
: h! S3 b7 H* ~* g' u4 x* G - if(isset($_REQUEST['code'])){+ v, p c% M1 P
- session_start();0 ?; ?+ i4 M: |7 v2 l4 [& h
- if($_REQUEST['code']==$_SESSION['code']){
$ |. B; M- ~$ q/ u - echo "<font color='red'>输入正确</font>";) N4 l- Z7 B( B) @6 M
- }else{
9 C/ B7 l4 i& i3 N" c - echo "<font color='red'>输入错误</font>";9 o7 A" B- z, b# S; m+ Z9 f
- }. Y; R, n2 S1 V4 m
- }! [" `. F) m! I
- ?>
9 l: `1 v+ e: g5 e - <!DOCTYPE html>
[5 P) E3 T& T# g# W( F7 P% T - <html>" b: U+ V% s. `5 g6 T5 H
- <head>6 @2 u/ e" d# W
- <meta chartset="UTF-8" />
% v9 X- J' R' U/ W9 D - <style type="text/css" rel="stylesheet">4 Q; {, D2 K$ B" |
- a{
" O1 p+ Z* c# E# H9 A8 z# { - text-decoration:none;
$ W6 w! u/ }9 J& a* ^8 D - font-size:30px;
5 J% R& @, A" z$ b) |3 h6 ]% m - color:blue;% O% d5 [- F( @+ C7 q
- }7 `5 X* F/ R$ H$ e+ }
- a:hover{" b9 m% ~4 F# U& a
- text-decoration:underline;
4 Z; l4 W! _+ Q" X - }1 G; L! \6 Z& z# g, [8 g
- </style>: C, v0 R2 |3 S8 J* _
- </head>4 s9 w8 J* n0 O6 C8 f
- <body>
8 Y& i8 }3 I7 U - <form action="form.php" method="get">
( N, }8 O% z7 E9 W( v/ Y; r9 g$ U - 验证码: <img id="img" src="index.php?" onclick="changeCode()"/>
3 ]5 c' S; {, e$ M. m. E1 T; [4 ~3 P - <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />8 z. `" T- j: |9 C" N
- 请输入验证码: <input name="code" /><br />
+ J _ b8 [% r - <input type="submit" value="提交" /> D3 _5 G4 v$ D ?& M% v
- </form>5 h& X% @2 f7 `4 G
- </body>; S% e! V' }; D7 K( r3 ^
- <script type="text/javascript">
# F" A" V4 V" E2 Y# W8 t% D' P -
4 m7 Q' v) ?/ x - function changeCode(){
$ n, N: _' Q0 }# r* L2 z$ s0 o - var img=document.getElementById('img');
7 e9 f- w9 `! M* f/ | - //img.src='index.php?r='+Math.random();1 Z$ o1 e% }# C
- img.setAttribute('src','index.php?r='+Math.random());
' r9 I+ E! {7 K$ j - }
1 |3 p1 D! u& R3 z+ r% ]& o - </script>
5 {( p! L) k2 c2 U1 I- l2 I - </html>
复制代码
* i* S) t: R4 z0 ^0 t8 T( J7 P4 [, p9 {/ Q' O" c$ g
|
|