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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[php学习资料] 验证码

[复制链接]
跳转到指定楼层
楼主
发表于 2018-12-16 01:00:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
index.php7 i8 v, ^: h0 |1 M% c) x1 B
  1. <?php
    7 [' O6 p5 E# s
  2. session_start();
    9 X+ T# r6 L+ U5 g: _
  3. // 丢弃输出缓冲区的内容 **- `* ]# l- y2 i* b9 G
  4. ob_clean();
    1 o# p! z! Q9 F# ]3 e
  5. header ('Content-Type: image/png');1 Y( j3 }: k/ O
  6. // 1 创建画布
    1 C) C' ]; ^3 [5 _" _' u/ Q4 d
  7. $image=imagecreatetruecolor(100, 30);' C8 d0 y& P0 W4 W0 e1 N; i
  8. // 2 画一个矩形并填充背景颜色为白色
    6 s/ w( k0 S# F, x
  9. $color=imagecolorallocate($image, 255, 255, 255);0 K: X: `4 M! c' {0 \) [
  10. imagefill($image, 20, 20, $color);
    2 E. E4 a8 P# q& l: P
  11. // for($i=0;$i<4;$i++){
    # P: c1 W& t8 C3 z* A
  12.     // $font=6;, E$ q9 X3 w( H/ m* k6 p
  13.     // $x=rand(5,10)+$i*100/4;
    # M) ?8 E6 x% {; {
  14.     // $y=rand(8, 15);4 c9 r, \" B) F# J+ P
  15.     // $string=rand(0, 9);/ |0 G* X/ I) r/ Q0 l2 E8 O& L
  16.     // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));: s2 s" t0 o6 X7 t8 Y  W/ r
  17.     // imagestring($image, $font, $x, $y, $string, $color);
    & W7 N# {0 H, U0 D. D& S2 w# w
  18. // }
    2 L6 @1 B6 t5 g% Q" k: T9 d
  19. $code='';
      m! P$ m6 l9 b/ |, S( O/ c% X8 M
  20. for($i=0;$i<4;$i++){
    - G: O3 ?3 \7 I1 a6 }7 D
  21.     $fontSize=8;5 y( I0 v9 A0 E0 r! J" m$ D; y
  22.     $x=rand(5,10)+$i*100/4;+ n) [. X  ~! z& ^! f* I
  23.     $y=rand(5, 15);
    0 d9 p/ g, i6 W  [$ G% S0 A
  24.     $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
    # [" |% L/ w. Q5 {+ m% Y! U8 d* R
  25.     $string=substr($data,rand(0, strlen($data)),1);
    " M! }! J% W3 [& q
  26.     $code.=$string;
    6 A; {' S  Y! m' A& Y! w
  27.         // 3.2 加载自定义字体8 x9 d! z% O3 |  H) m3 q( r
  28.         $font_file = '1.ttf';4 H, s2 |0 [) r  D* r" f
  29.     $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));
      J! m9 D8 {' [! K5 |: z
  30.         // 把文字填充到画布
      z( L8 Y3 @( ?' V/ L
  31.         //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);
    / W. y/ V9 s& U
  32.     imagestring($image, $fontSize, $x, $y, $string, $color);% g1 f) ~- K3 y1 j
  33. }: |' R" L' q: _7 q
  34. $_SESSION['code']=$code;//存储在session里
    . A& W/ F1 U% Q0 D
  35. for($i=0;$i<200;$i++){' g, |* ~$ E+ `3 Y: |& P
  36.     $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));8 a+ T4 b- o% [3 F& v
  37.     imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);
    5 p6 I/ J# ^# p  u/ {# J
  38. }
    ; c  N$ B  b& _& j; x% r# Q8 X: D
  39. for($i=0;$i<2;$i++){
    / V7 v1 J' ~* L) }2 A$ L3 a/ P
  40.     $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));7 B" c: w' W& y% h: [  N$ \
  41.     imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);& O, \% |7 T- O: Z% M) W
  42. }3 X! q4 @/ a$ z7 Q- S
  43. imagepng($image);
    ( h# o) }0 @  |6 `- p
  44. imagedestroy($image);& ]" n) Q. D7 m$ X0 w
  45. ' k8 c, A$ x1 U6 H
  46. ?>
复制代码
! q* t: S! B& v' g! `3 P4 w3 u' F
form.php# \9 h1 l' D4 ^4 Q
  1. <?php' H6 `; u9 [% x3 F( t) o  x& J
  2.     if(isset($_REQUEST['code'])){0 x7 ^& }+ @8 i3 x# n
  3.          session_start();% i* x, \2 E( C( P8 ?0 s+ B+ B
  4.         if($_REQUEST['code']==$_SESSION['code']){* U  B" |3 `: U7 G8 Q. h  y
  5.             echo "<font color='red'>输入正确</font>";
    ) l; i. o8 A2 q  z! W" v
  6.         }else{' b% I; D2 K& a! e
  7.             echo "<font color='red'>输入错误</font>";7 |% F( }' d, C" V
  8.         }# o) r$ P' N$ h$ T: U* }0 ?1 J" Y! }
  9.     }
    ' U: q+ c2 ~' P5 w& c" X! v) {7 W
  10. ?>
    2 I& D% U1 }: G& U8 j
  11. <!DOCTYPE html>
    3 U8 A* W& ^; l4 l" I
  12. <html>
    . ^7 ^4 u; @% Y9 O
  13.     <head>5 t% W7 [* T( r5 @  l9 |& `# B
  14.         <meta chartset="UTF-8" />. Y$ }; X: r* a# {# N
  15.         <style type="text/css" rel="stylesheet">- P- _8 T: u- V6 l
  16.             a{: J7 N. u8 Q9 }+ y
  17.                 text-decoration:none;
    * W. T. I' h6 P( h2 J: l3 j
  18.                 font-size:30px;) J3 Q6 |( G% U1 R
  19.                 color:blue;
    * H1 a* u7 N, z; j* G. X
  20.             }
    4 L- u$ M( b1 E- G6 k
  21.             a:hover{0 h. A7 h8 ~( Q  ^: r1 ?# [$ @" E
  22.                 text-decoration:underline;
    9 y( j& o4 y1 c) U, G2 J: y
  23.             }+ M! ^. |  Q0 u+ u5 `
  24.         </style>
    7 O! w1 O7 B* ?' u- s: a
  25.     </head>
    7 x1 g3 ~* E% T
  26.     <body>  B9 P& D( Q* n; e, a4 I
  27.         <form action="form.php" method="get">! c& A$ `$ y' a
  28.             验证码:  <img id="img" src="index.php?" onclick="changeCode()"/>+ u$ P& k2 u: k0 ?
  29.             <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />
    3 D: ^- A& g( W- N  ]
  30.             请输入验证码: <input name="code" /><br />
    % o1 y. e+ h$ K# P, Z
  31.             <input type="submit" value="提交" />
    ' i# x' o$ S/ t8 B% m, H& z
  32.         </form>
    9 t' Q( r5 S+ P: }. V
  33.     </body>
    , |& ~* j% Q; F/ }
  34.     <script type="text/javascript">
    0 v1 K$ P; \5 A6 j2 L: b# U
  35.         
    ; P& z7 }+ L5 o% i
  36.         function changeCode(){8 T( w. x% I4 J( K
  37.             var img=document.getElementById('img');
    ; {1 e" @3 h3 ?
  38.             //img.src='index.php?r='+Math.random();
    " ~4 Y* }7 H* k5 C* L  o9 [* }; M
  39.             img.setAttribute('src','index.php?r='+Math.random());1 |& d5 Y5 {) P& h7 h: D: W
  40.         }
    3 ~; w7 p. C5 ?& u
  41.     </script>- f& \' C$ t. x/ T0 l5 R& b7 Q
  42. </html>
复制代码
6 W& E9 ?' C# `. X
' w$ d( t% Z7 @
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-8-4 10:51 , Processed in 0.054069 second(s), 20 queries .

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