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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[php学习资料] 验证码

[复制链接]
跳转到指定楼层
楼主
发表于 2018-12-16 01:00:13 | 只看该作者 回帖奖励 |正序浏览 |阅读模式
index.php
2 j+ Y3 R& x& A2 @2 T! E
  1. <?php
    3 s' b' f/ N4 u( S0 _6 }0 p
  2. session_start();# R% ?* ^3 l7 l6 W' d
  3. // 丢弃输出缓冲区的内容 **
    $ O; `# g. B0 G3 n5 r" Z: L3 F
  4. ob_clean();4 X0 Y( d2 ~4 D8 F9 e3 @
  5. header ('Content-Type: image/png');7 ~8 r7 |# u3 Q6 ]) c7 s
  6. // 1 创建画布. `) y( J7 C" Q% s  i9 Q% P
  7. $image=imagecreatetruecolor(100, 30);
    5 o  h# l1 w! o9 k/ j9 t% t: O/ ~* K
  8. // 2 画一个矩形并填充背景颜色为白色
    4 W/ N& _9 p: X3 J/ x
  9. $color=imagecolorallocate($image, 255, 255, 255);
    3 _- q  ~$ _" w' |
  10. imagefill($image, 20, 20, $color);" N. b$ I% |. t1 H* ^+ [, m9 u6 }
  11. // for($i=0;$i<4;$i++){
    8 K9 \0 f4 Z, \* D" r
  12.     // $font=6;
    & K- L5 K9 l6 O. C7 ]
  13.     // $x=rand(5,10)+$i*100/4;
    4 _0 F: K5 u2 T' X6 Z5 x- v
  14.     // $y=rand(8, 15);, h( Y" E4 e/ {* Q' C5 p, R1 B
  15.     // $string=rand(0, 9);
    0 l* d1 ]+ `( _$ v' Z  h" n. O
  16.     // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));
    , K: _/ x" n( m+ m9 b2 U8 F
  17.     // imagestring($image, $font, $x, $y, $string, $color);3 \# b, W8 Q- @) }3 p1 T! q* o4 ]
  18. // }, f" @  `) r& J: s
  19. $code='';" J0 @7 S- z: u! I' E* B& ]
  20. for($i=0;$i<4;$i++){
    % J! V. a0 J/ q# i9 ~$ J5 p
  21.     $fontSize=8;
    % R* u; I7 M" q" ^
  22.     $x=rand(5,10)+$i*100/4;
    ; X  J2 ]* h) R' x0 H2 ~6 D
  23.     $y=rand(5, 15);
    8 b5 Z/ d1 ]2 b" [7 y
  24.     $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
    % Y% p1 Z& Y! d  Q; ~+ ?
  25.     $string=substr($data,rand(0, strlen($data)),1);$ k/ L# q0 {) M$ o* m/ N
  26.     $code.=$string;
    3 R  |8 r) G" u& n* V. r. j
  27.         // 3.2 加载自定义字体
    5 S5 O" O* G1 G# b
  28.         $font_file = '1.ttf';
    " p$ m) c1 \% X1 ?& U
  29.     $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));
    4 q# R( @( Y- g2 J  f' |/ e
  30.         // 把文字填充到画布6 L# W! ]& k8 l
  31.         //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);
    1 P4 y9 u' b2 Q
  32.     imagestring($image, $fontSize, $x, $y, $string, $color);5 G  G! X& ^  Z. ]; W: j6 X
  33. }0 f; |4 w- C5 l' u
  34. $_SESSION['code']=$code;//存储在session里) `1 @( T3 B2 ]/ \6 X4 E
  35. for($i=0;$i<200;$i++){, q/ S6 K0 I* q3 m4 |8 V
  36.     $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));
    " Y8 c3 O/ e) V, g8 B: }* C" Z
  37.     imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);; c. b2 _( x) |5 n; L
  38. }
    # ~9 J* g9 Q4 y. z! c  H4 N
  39. for($i=0;$i<2;$i++){
    * W* r3 P& \* H$ a5 B3 q' @
  40.     $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));& M5 f) t9 ?) S+ T2 e
  41.     imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);; P# G5 h: E7 w! W  s8 P+ c
  42. }
    5 A) b- [6 p; f  @. Y. a
  43. imagepng($image);+ e7 u! r. i& D" E! g
  44. imagedestroy($image);$ ?* o& ?+ c$ m; c$ \+ l

  45. ( u: X; z2 H/ t6 r1 y" t; j( R
  46. ?>
复制代码
  S- R( s9 \+ }& _+ ^  l
form.php
6 i" |; l( }( ?/ R
  1. <?php
    # M; c+ ]: j  U& z+ V0 l* d% h+ `: l
  2.     if(isset($_REQUEST['code'])){* W+ h! V/ s/ S
  3.          session_start();
    ) @/ p! H' Q$ h$ ^" S
  4.         if($_REQUEST['code']==$_SESSION['code']){3 i* T1 n7 a; k& J. _4 p7 S7 F& o
  5.             echo "<font color='red'>输入正确</font>";4 j4 H+ x- Z# l! H& g1 o
  6.         }else{
    7 @  G1 Z( l) k( Q
  7.             echo "<font color='red'>输入错误</font>";
    - t$ ^! P5 T7 j. t: l/ d7 ?
  8.         }% `7 V" k* O& z' C1 |& s
  9.     }
    8 A; s. v% {3 d1 f. E- z; D: A& _# }
  10. ?>3 U  v% c  b" D4 T
  11. <!DOCTYPE html>
    1 r1 c7 x4 j6 @8 i0 ~5 i( @  c2 z, O
  12. <html>
    1 ~. {  w; \6 [6 `' l
  13.     <head>0 U& U  j) A! G% i0 _7 W# L* R
  14.         <meta chartset="UTF-8" />
    ) [5 s9 _! x& j, O- j7 w$ g2 Z1 L
  15.         <style type="text/css" rel="stylesheet">
    4 |7 _. g' C/ L, g' U* g
  16.             a{
    " s, A4 O- o8 t
  17.                 text-decoration:none;* Z: ^+ A8 m; ]9 \* i9 i3 B% s
  18.                 font-size:30px;$ M" C$ Z8 i+ D
  19.                 color:blue;
    * M$ Z( I& i+ l9 S7 N- O0 L# o
  20.             }) m: a6 n* D! M+ @8 |; o
  21.             a:hover{
    / y9 B" D6 k& a! i, N
  22.                 text-decoration:underline;
    ! ^8 f7 O; ~3 z" R0 U
  23.             }4 q6 z. B7 e9 i7 L0 a) h
  24.         </style>
    ' }) {3 x' A. \9 _
  25.     </head>
    6 H( [" b( e. \! H0 T  L8 b
  26.     <body>8 M/ o! R3 X5 W2 A6 [
  27.         <form action="form.php" method="get">
    ' d4 f, S3 j% C. \
  28.             验证码:  <img id="img" src="index.php?" onclick="changeCode()"/>
    3 I6 T2 ~9 G* ~2 \
  29.             <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />
    + ?! e* K2 a- d& R5 X/ A+ M  Q
  30.             请输入验证码: <input name="code" /><br />* u' t) g4 o6 \+ f# Q) ?+ W, m
  31.             <input type="submit" value="提交" />
    5 N7 U. y3 F8 s$ }
  32.         </form>
    6 x0 O: B: p. C+ ]& j. q4 O; T
  33.     </body>$ i; Q9 ^! o7 M/ v: |8 x- M, t1 q3 g0 g
  34.     <script type="text/javascript">$ o" Y% {5 z" H; I8 _( s7 K
  35.         ' V8 H- H. u6 o) u# |& [" d
  36.         function changeCode(){
    ! p6 j2 z& \" i/ [/ B/ p
  37.             var img=document.getElementById('img');
    & F  a3 ?! A% E* L
  38.             //img.src='index.php?r='+Math.random();
    : J( A% c/ N8 ]+ [
  39.             img.setAttribute('src','index.php?r='+Math.random());$ M; _" r6 `* v! q: }4 _# Q
  40.         }
    # F% G" Z  }3 {' c
  41.     </script>
    . n% E, b8 C3 v/ r2 t, W
  42. </html>
复制代码

+ {6 ^; ]  w) G' Q+ i  n0 S9 n! l4 p) ~+ k- S
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-6-20 04:17 , Processed in 0.059989 second(s), 20 queries .

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