您尚未登录,请登录后浏览更多内容! 登录 | 立即注册

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 16074|回复: 0
打印 上一主题 下一主题

[php学习资料] 验证码

[复制链接]
跳转到指定楼层
楼主
发表于 2018-12-16 01:00:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
index.php
+ N6 n% X0 O3 o
  1. <?php; `9 Q: G4 |- {8 u4 X
  2. session_start();
    ! P! Q: n) G( M3 i8 ?; N
  3. // 丢弃输出缓冲区的内容 **
    ! Y3 f% B$ V2 j% B/ T8 d- q
  4. ob_clean();
    7 E! j) w$ @8 X# X
  5. header ('Content-Type: image/png');: N$ Q" q7 Q7 ]( n+ ^5 I  Q
  6. // 1 创建画布% c1 t3 B" H% Y. F7 O9 C( ?) J
  7. $image=imagecreatetruecolor(100, 30);
    / r, w2 o. m0 |0 S3 s+ C3 R' l
  8. // 2 画一个矩形并填充背景颜色为白色7 i& B; F( X; P! L- [& z8 D
  9. $color=imagecolorallocate($image, 255, 255, 255);2 T6 ~& [) X8 y& w, W6 @% z4 J) E2 N
  10. imagefill($image, 20, 20, $color);
    9 `6 y2 N* t- c. e8 X
  11. // for($i=0;$i<4;$i++){% n0 z; S# t1 p# ~+ j7 c
  12.     // $font=6;9 G, @  @# H, n% J) Y
  13.     // $x=rand(5,10)+$i*100/4;) w- k& z9 V( i) L9 ^( a
  14.     // $y=rand(8, 15);
    + `& C% v& b% `" V. I" N
  15.     // $string=rand(0, 9);5 e1 p: D) c! ]5 x& g
  16.     // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));$ J0 A/ H/ W- f' w$ S) g
  17.     // imagestring($image, $font, $x, $y, $string, $color);: l0 f6 ~; C) M: q& @5 |* e* p
  18. // }
    ' _$ }5 ?- C8 b( g
  19. $code='';* i, ]2 b! s; {, A5 c
  20. for($i=0;$i<4;$i++){6 W7 q8 V$ c! n
  21.     $fontSize=8;  w: g( ~/ Z& |; h9 N) y9 d
  22.     $x=rand(5,10)+$i*100/4;
    , b1 C" E" Z3 Z* Z6 k4 u2 O; M
  23.     $y=rand(5, 15);
    3 B" h; U/ g2 D4 I% U0 V. V4 d
  24.     $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
    : ]6 u. d5 a7 \4 ^& i) s
  25.     $string=substr($data,rand(0, strlen($data)),1);
    # F' {% I8 ]) ?( l+ I
  26.     $code.=$string;9 o; v8 G! u% B4 h$ V7 ^$ C7 Z
  27.         // 3.2 加载自定义字体1 I3 ^9 z9 ^/ `+ r
  28.         $font_file = '1.ttf';' u: W4 t  o; t6 P/ C" P; V
  29.     $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));
    $ t5 k. J. s. Q) m: m( e( A$ b! {
  30.         // 把文字填充到画布
    5 f$ F. v& S' D3 V- j
  31.         //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);
    2 m/ `5 @/ \5 M
  32.     imagestring($image, $fontSize, $x, $y, $string, $color);) b2 J% V! L$ R$ v3 w
  33. }. w% G+ t3 i) D& @; j. m& e
  34. $_SESSION['code']=$code;//存储在session里$ U" B* C9 C. _9 n
  35. for($i=0;$i<200;$i++){- m$ \, m, e6 m- X1 _. L0 |$ Y) [: B
  36.     $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));
    7 _' c8 w  J1 h* v! f- l( z) T
  37.     imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);
      g  m& a9 K* G' s, Y( ?" b" O6 q! h* J
  38. }
    ) _! J5 E% }+ u% I$ ^& C  I
  39. for($i=0;$i<2;$i++){- {% }$ _" z: u( T
  40.     $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));
    / g5 M5 V, g0 M& K$ ~. ~1 ?
  41.     imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);
    , }6 t, \$ J( A% ?9 I9 I
  42. }, q! _4 o$ n2 C
  43. imagepng($image);6 _2 K1 a5 Z) l8 K5 u* ^' R
  44. imagedestroy($image);
    1 i$ ^6 y' d* j' G2 I

  45. ( A; U( [, i1 W. K8 V! K) R
  46. ?>
复制代码
9 h6 B/ d4 e) x
form.php: k. D/ f6 @, y
  1. <?php
    / \9 @) O7 G% g' `, x
  2.     if(isset($_REQUEST['code'])){7 e3 t3 o* \% {. Q' A6 Q: P
  3.          session_start();
    # r2 C1 _, f* u1 t
  4.         if($_REQUEST['code']==$_SESSION['code']){& }# V/ `& J5 P0 K& M
  5.             echo "<font color='red'>输入正确</font>";$ E- t$ U4 T& ]
  6.         }else{0 K/ m. |3 f9 }2 T
  7.             echo "<font color='red'>输入错误</font>";+ e, a1 y8 i9 A
  8.         }% [3 r: |2 f6 S( d
  9.     }& b0 \) [6 w* \4 T3 e7 o
  10. ?>" x8 v& j2 G$ L. h8 z
  11. <!DOCTYPE html>/ Z2 }7 y5 l( v* l
  12. <html>
    $ X4 }7 M* a& H
  13.     <head>
    3 X! X- N, x3 }; Z3 Q
  14.         <meta chartset="UTF-8" />/ W: D! j3 H! V. J% F; n
  15.         <style type="text/css" rel="stylesheet">
    " n$ [- p* n" U, S! ?; f
  16.             a{7 y2 v& \1 N1 s* d$ x
  17.                 text-decoration:none;6 V1 {- Q: K! b8 z/ K( z
  18.                 font-size:30px;
    ! P8 ?" C- S1 V2 r# A6 G
  19.                 color:blue;' c% U8 |; `1 W& t
  20.             }
    2 T5 A( O4 Q9 ]# K
  21.             a:hover{
    ( f' ]: F) P1 ^! U
  22.                 text-decoration:underline;+ i- @5 E+ Q' S* B( v8 j
  23.             }# J% C) z1 @/ x( N5 ~
  24.         </style>+ @5 K9 ~0 C/ z" n
  25.     </head>
    . |& U0 W; `7 C' E$ Y
  26.     <body>2 o, Y8 u9 b1 I8 b" U9 ]
  27.         <form action="form.php" method="get">* N/ y! T8 Q! o# d* p6 Z; O
  28.             验证码:  <img id="img" src="index.php?" onclick="changeCode()"/>
    ( V0 W: R0 w) W0 F. g, v- {
  29.             <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />
    1 J+ d$ k6 m, |  B. @. y) ?1 k' Q& u2 ]
  30.             请输入验证码: <input name="code" /><br />. `; ?9 B6 {+ `+ d9 _$ E  w. j8 ?
  31.             <input type="submit" value="提交" />% T% V+ U; D5 y0 f. s  y
  32.         </form>) |7 b) ~. l0 H: W) R; z
  33.     </body>
    . M& h3 d% X7 l! E  L
  34.     <script type="text/javascript">" B' S7 P4 ?  h+ T, o" i
  35.         9 D, c( c* r, \. t
  36.         function changeCode(){
    ; d" N/ k1 y* c
  37.             var img=document.getElementById('img');
    , j  f( l1 `, K8 ?9 w/ r
  38.             //img.src='index.php?r='+Math.random();1 n3 p+ k" n" _; R1 `  w
  39.             img.setAttribute('src','index.php?r='+Math.random());& v+ i" v- {$ J# D5 \$ k
  40.         }
    / h0 x" b- D0 ]. x0 g6 T
  41.     </script>7 I/ U3 C- @8 ^9 K1 |3 Y
  42. </html>
复制代码

8 @7 z8 u% y9 b- U: f& c6 y0 H, O0 c, y/ g$ L3 y
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-6-20 02:46 , Processed in 0.055595 second(s), 20 queries .

Copyright © 2001-2026 Powered by cncml! X3.2. Theme By cncml!