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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[php学习资料] 验证码

[复制链接]
跳转到指定楼层
楼主
发表于 2018-12-16 01:00:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
index.php2 w. s/ |( R6 |" d$ E
  1. <?php9 c5 C2 Q/ ?  \
  2. session_start();6 D, k2 }! V5 N: K* ~1 e6 E
  3. // 丢弃输出缓冲区的内容 **
    7 O# D4 c& a* X" u
  4. ob_clean();. _& _6 d% W; C( N
  5. header ('Content-Type: image/png');7 _$ N& m$ r; V% t! {- ^& K. F. I
  6. // 1 创建画布$ l1 `1 m+ E. L
  7. $image=imagecreatetruecolor(100, 30);2 q5 M, Y% M5 ^( a
  8. // 2 画一个矩形并填充背景颜色为白色
    / P& p! D7 {% d: w5 U  S
  9. $color=imagecolorallocate($image, 255, 255, 255);
    3 X+ y' v$ m$ E% K# g
  10. imagefill($image, 20, 20, $color);% ]! T  ]1 s) z4 b
  11. // for($i=0;$i<4;$i++){) o; b, v% s- q
  12.     // $font=6;
    ( V1 V* K$ f) a" u7 a6 n9 _
  13.     // $x=rand(5,10)+$i*100/4;
    & e, W' r  Z7 G2 Q6 q
  14.     // $y=rand(8, 15);
    + l; N; U( Q8 ]7 X6 `6 n
  15.     // $string=rand(0, 9);0 S- X* w8 @0 c, ^1 z1 {' Z
  16.     // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));
    - e6 t( C  q, Q+ M) y
  17.     // imagestring($image, $font, $x, $y, $string, $color);, |6 `/ a1 Q' ~: ]& x& U7 c
  18. // }  y: C+ b0 e4 g) e
  19. $code='';
    + n& ~& i; D8 X0 a) e' J
  20. for($i=0;$i<4;$i++){* f- {* {7 O+ F
  21.     $fontSize=8;! x; [# W0 m, g6 R$ h
  22.     $x=rand(5,10)+$i*100/4;
    0 z2 _1 c6 E! c$ O5 F% y( q
  23.     $y=rand(5, 15);
    . b, n6 v7 x' f( N5 |1 y5 `
  24.     $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
    : J# H( Y+ n) A6 \: I& ]# d& o1 Q
  25.     $string=substr($data,rand(0, strlen($data)),1);
    , E2 v/ f, O/ f! r8 G
  26.     $code.=$string;. K  n6 q3 Q' K, M) }' S: v* j$ F
  27.         // 3.2 加载自定义字体
    $ B" Y5 D. O: @; m: }( y
  28.         $font_file = '1.ttf';& c' {6 W) P2 N* Q  p# a6 a7 z
  29.     $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));
    ( m2 X) I0 S4 a: x3 ]5 a
  30.         // 把文字填充到画布  O, O7 ?4 y+ ]) C
  31.         //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);4 h9 G" ?  q$ s
  32.     imagestring($image, $fontSize, $x, $y, $string, $color);
    ) L! y# R/ h% y* `, z9 Z- s9 W' Y, Z
  33. }
    * X' @" @4 h- w. H; X
  34. $_SESSION['code']=$code;//存储在session里
    : V; {7 k6 b3 q; `
  35. for($i=0;$i<200;$i++){! E! G1 i! M0 I: E) U
  36.     $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));
    - |/ g9 b  R0 ^2 J$ j4 o
  37.     imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);
    - {8 {) v( P! c# U1 o8 l
  38. }! @$ a# d' E' {4 u* |
  39. for($i=0;$i<2;$i++){
    ' s+ \% L% N6 [, Q' G$ s4 P
  40.     $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));
    0 h# X6 F( [) n8 n0 N' }
  41.     imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);6 s. i- a4 b; V3 ~! @* y# k& a
  42. }
    9 [8 {8 j" p. N; K, l
  43. imagepng($image);; b' P' ]7 G) q2 r
  44. imagedestroy($image);
    6 ?* d  i" e- S9 r! Z4 x
  45. ' e5 n4 _0 `- E  w4 C5 R# Y; @
  46. ?>
复制代码

4 d2 U+ D6 S) ~5 y7 M, @form.php/ A5 J: }3 D6 H( V) k  X
  1. <?php& g( l, }, O8 ?* _8 A5 u' r8 z1 S2 |
  2.     if(isset($_REQUEST['code'])){1 h- i) k% J: ^" n% d
  3.          session_start();
    5 d: y' v$ V/ B. |# b* }. ]' v
  4.         if($_REQUEST['code']==$_SESSION['code']){9 f& r* ]9 \9 ^
  5.             echo "<font color='red'>输入正确</font>";
    # h$ S- s0 a4 R3 p
  6.         }else{) l' i" v1 p7 T% {1 t# n
  7.             echo "<font color='red'>输入错误</font>";' s9 t4 Q; U  k0 K; L& ^
  8.         }- K/ Z" }# L: i- n
  9.     }$ D, E( X2 Q: x# n5 H" y
  10. ?>% x# A. P$ F$ ?4 g" _/ B! ]1 _
  11. <!DOCTYPE html>! P: f, H9 A8 X+ _9 M- G
  12. <html>
    $ S7 J( `$ N" F& @, A/ s9 `- q
  13.     <head>/ g4 o5 x, s' i/ t! m7 o
  14.         <meta chartset="UTF-8" />
      B/ v4 G$ Q, o  G
  15.         <style type="text/css" rel="stylesheet">
    ; r! B  A3 m! Y- Q+ B8 r- |( p
  16.             a{
    & Q3 W% M& S( f2 Z* j8 H9 J
  17.                 text-decoration:none;
    . x4 Y4 ]/ n/ U- F4 x/ F3 f+ i
  18.                 font-size:30px;: X+ ~& f3 W& \6 w' b/ W4 L+ ~& Y
  19.                 color:blue;
    % K4 n! ]2 p2 b1 U7 z7 B* ~& l. x
  20.             }
    ) d6 e; E" V6 W) L" n, L, M
  21.             a:hover{! T, j+ p& Y0 C- {5 L! \
  22.                 text-decoration:underline;
    $ A6 d! s- o1 }( f* ?7 b1 e: ]8 ^
  23.             }
    ( c) K2 X. J$ P3 O
  24.         </style>
    % a! v1 n" ]7 z: A
  25.     </head>
    5 X! M* `: x# k1 Z# ]
  26.     <body>9 y; N: y2 _7 P& n3 v& @
  27.         <form action="form.php" method="get">9 K% n- F! Q6 k5 T
  28.             验证码:  <img id="img" src="index.php?" onclick="changeCode()"/>5 h& Z% S4 Y  P1 k" M9 z4 a( z0 t
  29.             <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />
    - ~# v$ t6 L- t2 M
  30.             请输入验证码: <input name="code" /><br />
    7 ^0 z% D+ |; Y  S% M  g( s6 h3 M5 W
  31.             <input type="submit" value="提交" />
    . V8 x$ |0 k! a( e" S% W2 z+ J
  32.         </form>- S: m6 Y+ d' \) v6 B5 |3 u7 d0 A
  33.     </body>
    6 G  ^1 g" k# L9 k
  34.     <script type="text/javascript">
    * {+ Q4 B# V+ S1 s
  35.         2 ]9 A2 c* o1 ]$ p
  36.         function changeCode(){
    " V* \5 O, x$ v" g3 z
  37.             var img=document.getElementById('img');' y( D. b8 E* X2 J# X4 _- Y0 s7 V
  38.             //img.src='index.php?r='+Math.random();, z& i0 g- R; j0 a2 G
  39.             img.setAttribute('src','index.php?r='+Math.random());- H. j1 ]# |9 R5 {, T( N, M
  40.         }
    ( I. y) z- q8 L7 ~. E0 X
  41.     </script>
    + c( G; M7 B- i1 N
  42. </html>
复制代码

1 P3 n+ \# ~: g, X! c; ^8 S+ F+ O
. j$ {  j" g3 b1 z
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-6-20 01:53 , Processed in 0.060729 second(s), 19 queries .

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