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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[php学习资料] 验证码

[复制链接]
跳转到指定楼层
楼主
发表于 2018-12-16 01:00:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
index.php
2 T3 v- K6 a7 g+ F* C1 ]% m
  1. <?php
    7 ~3 A/ W  a& _) c
  2. session_start();0 Z2 w( d, L% p( j" T8 X9 T  N3 \
  3. // 丢弃输出缓冲区的内容 **. m6 I) N9 o' v, \9 J
  4. ob_clean();
      M2 `5 G: p7 K, ~
  5. header ('Content-Type: image/png');8 s3 g9 ?) a' Z. }# C3 p: ~. r
  6. // 1 创建画布3 N$ e9 ?( m7 c; O6 o% a6 n* U8 N
  7. $image=imagecreatetruecolor(100, 30);5 S. |. {' v, n" q. t
  8. // 2 画一个矩形并填充背景颜色为白色# v# l+ H7 I; o! k" h2 e" Z  x, ]/ H
  9. $color=imagecolorallocate($image, 255, 255, 255);# v) O' i/ g: c9 k
  10. imagefill($image, 20, 20, $color);% V: N2 c: r0 ?( j8 n! `* G
  11. // for($i=0;$i<4;$i++){: B' h; W3 W1 R4 |& z
  12.     // $font=6;
    : n' ?/ M; A; e0 y9 R0 p; l
  13.     // $x=rand(5,10)+$i*100/4;1 ~& P4 m# c7 ^' p
  14.     // $y=rand(8, 15);
    5 E$ d6 S( z' ~2 U- I9 _
  15.     // $string=rand(0, 9);
    " k1 ?8 M# |3 o- `
  16.     // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));
    2 a9 }& [2 C5 S0 V3 ?
  17.     // imagestring($image, $font, $x, $y, $string, $color);2 ]/ A: \' ~" \7 H7 u0 x
  18. // }
    ! @( J/ V8 I" s% L5 i- m. R
  19. $code='';
    . o* ?" l/ I/ T( ^: B
  20. for($i=0;$i<4;$i++){6 x" u$ {2 c; W1 b
  21.     $fontSize=8;
    / t7 b8 S9 T1 f$ j8 }8 o3 Z
  22.     $x=rand(5,10)+$i*100/4;% U# f  b. |; f+ G/ ?1 k
  23.     $y=rand(5, 15);  y" |) j/ y$ m2 g, b5 s- a0 [
  24.     $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
    * g9 R$ r, T( ^2 I. f6 A/ Y
  25.     $string=substr($data,rand(0, strlen($data)),1);
    : W- E+ x$ x! P
  26.     $code.=$string;. }( l# O) q4 |& E4 d0 A: i
  27.         // 3.2 加载自定义字体
    6 t4 c% O, i$ p# _% N! G2 l
  28.         $font_file = '1.ttf';0 B8 R# x8 y7 _$ h) O
  29.     $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));
    3 J( k7 Y* c; V! @! V
  30.         // 把文字填充到画布6 R. F! p/ Q9 O9 h, }# P
  31.         //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);
    9 ]# }  h# v, S& @, n" \4 l3 R
  32.     imagestring($image, $fontSize, $x, $y, $string, $color);
    $ f  q! }) z0 m4 S4 z: Y! v
  33. }
      e" C. f( ?, X* I* c, ~  w! I0 N
  34. $_SESSION['code']=$code;//存储在session里! q5 j& \9 v( H* e# T. d
  35. for($i=0;$i<200;$i++){: c) J+ P, T% Z; i1 m
  36.     $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));
    , i0 m4 @; O5 H# r+ B# y! W& j
  37.     imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);: h8 G3 h. w! D" n' t
  38. }; g9 |- ?1 g) \8 F' T
  39. for($i=0;$i<2;$i++){: D$ X2 g( V' P. E2 w: d
  40.     $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));1 g! D9 `5 @8 v1 \
  41.     imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);8 n3 l, V3 ~" e, g& v
  42. }9 B, q+ U: C) |  S3 z
  43. imagepng($image);
    - S* T3 Y7 |& i8 L, V8 w
  44. imagedestroy($image);- C( _$ h0 H0 ^% O+ `5 ?

  45. . q3 S/ s- W) G. K8 u. w) Q
  46. ?>
复制代码

$ |# y4 a/ G6 q, H, e6 Aform.php
0 n# K7 W( `% k9 X, o( F" a
  1. <?php
    $ `# _0 n2 D  G7 h  u, [
  2.     if(isset($_REQUEST['code'])){
    $ m$ g6 r7 t5 I! K
  3.          session_start();
    2 _; [% }! K; ^4 \1 X
  4.         if($_REQUEST['code']==$_SESSION['code']){
    % J0 y8 Q" O/ I0 S9 l
  5.             echo "<font color='red'>输入正确</font>";
    ) @, a, d% R0 x4 j0 }4 p& t
  6.         }else{2 [* t+ X7 a6 S9 c+ I: v
  7.             echo "<font color='red'>输入错误</font>";
    . m$ n5 R3 v0 c8 @% |$ e$ c
  8.         }0 o: _( u5 Q5 s4 s8 H" R7 Y
  9.     }
    7 p# c" O' a* M4 c0 D
  10. ?>$ c& W5 H$ e0 ^/ g/ W: _/ y4 r
  11. <!DOCTYPE html>
    3 |9 T0 c7 R. I4 Y: M
  12. <html>7 g% A* ]! l- J( Z
  13.     <head>
    " |1 N, \2 h2 U8 c
  14.         <meta chartset="UTF-8" />4 r4 H8 K7 `. a7 i9 K- M
  15.         <style type="text/css" rel="stylesheet">
    9 g6 t2 j& T- `% j. ?/ w/ {
  16.             a{* ~+ [( ^& I9 ?$ o+ k
  17.                 text-decoration:none;
    % g& B! ?* c3 w; S/ ~
  18.                 font-size:30px;
    5 B2 |( x6 L: [$ P9 N$ f" |
  19.                 color:blue;; X! m/ v: V5 A  S  ~% L) r( _
  20.             }
    # @& `; W( M" d; M! t' {
  21.             a:hover{- @1 |% I  a/ p" I. p( f
  22.                 text-decoration:underline;& ]. K, b. \0 g  G1 z( K- N
  23.             }6 Z6 I+ X. a: f0 p; I
  24.         </style>
    ) V# ~) d  u& V7 }0 J) F
  25.     </head>  n2 p2 |/ O6 G: `8 `( H  F* `
  26.     <body>
    # C, q" E) t. d) K
  27.         <form action="form.php" method="get">3 i; ?- Z7 Y" I& w5 w- I/ R; z
  28.             验证码:  <img id="img" src="index.php?" onclick="changeCode()"/>
    & O- l: w8 t; h+ S) r
  29.             <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />( N7 U3 D! x1 @4 t& |
  30.             请输入验证码: <input name="code" /><br />
    , U( F& d% ?" w8 D; q1 p
  31.             <input type="submit" value="提交" />
    & a: @) ~. _( Q5 P3 h) G
  32.         </form>4 k9 H+ L% d7 h" h2 p
  33.     </body>
    # W6 m  |; p: U% r! q
  34.     <script type="text/javascript">$ {% @% C: s1 Z  A# o
  35.         
    6 I8 f; \5 N. U! T  T0 j
  36.         function changeCode(){
    " M( s' z7 z# z* A, L2 c! V
  37.             var img=document.getElementById('img');
    3 O  }5 U" H+ U% N% a& G
  38.             //img.src='index.php?r='+Math.random();
    - Y9 S8 ~3 [( ]3 r6 K
  39.             img.setAttribute('src','index.php?r='+Math.random());
    . a0 P( y& }$ l/ Z6 u7 H
  40.         }
    3 T$ t& {1 b& N5 z
  41.     </script>
    5 Z" L' d2 ]$ Q' G( f3 W
  42. </html>
复制代码
. w! M) F# f+ k  H, p; q
2 m) r' Y4 e$ o
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2024-7-1 10:24 , Processed in 0.148446 second(s), 21 queries .

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