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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[php学习资料] 验证码

[复制链接]
跳转到指定楼层
楼主
发表于 2018-12-16 01:00:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
index.php6 U  B) Y- ]! ]9 v; N
  1. <?php/ N( p0 n; d$ c) i9 v
  2. session_start();
    ( p8 g! |) z& L6 E9 s
  3. // 丢弃输出缓冲区的内容 **1 l2 D' c. }0 l- Q3 }3 b5 f
  4. ob_clean();( g! i! ]* N0 K: l- t
  5. header ('Content-Type: image/png');+ A7 g# R9 H2 P6 L
  6. // 1 创建画布
    ( s: |; ^3 C: @2 w) m  y
  7. $image=imagecreatetruecolor(100, 30);2 e8 x+ \8 Y* A; G- T$ j  j
  8. // 2 画一个矩形并填充背景颜色为白色
    ( p9 I) S2 v" j7 S- k: \$ D, Y+ K
  9. $color=imagecolorallocate($image, 255, 255, 255);
    ) t3 S: [$ r$ |2 ^6 D/ t2 p, ~/ j
  10. imagefill($image, 20, 20, $color);
    , E& f/ n, ~7 y5 M% H
  11. // for($i=0;$i<4;$i++){& ^; |5 T. o. T, q
  12.     // $font=6;
    # }. {  x; ?7 i: u& b; k3 M: Z
  13.     // $x=rand(5,10)+$i*100/4;
    - ^3 q* y1 @, D0 H, O1 w$ j5 O
  14.     // $y=rand(8, 15);9 Y9 Y  o# H( N2 Z* Z3 ^8 f. M
  15.     // $string=rand(0, 9);# H* f  q2 h" C5 T! D
  16.     // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));
    ; t$ C% j$ i7 I
  17.     // imagestring($image, $font, $x, $y, $string, $color);
    - D) X! v1 ^! z% w
  18. // }0 ]* ^) J9 r2 r/ q: z* J7 C/ @
  19. $code='';
    ! O6 \! z/ K8 t4 J
  20. for($i=0;$i<4;$i++){
    3 K4 {6 p+ Z% M" L
  21.     $fontSize=8;
    ' J7 O/ f$ d* N8 f
  22.     $x=rand(5,10)+$i*100/4;, t# E& a* r+ _
  23.     $y=rand(5, 15);
    , w8 Z! {+ w0 ]- p4 b
  24.     $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
    + v; Q- g+ v/ P8 T; X/ `6 u
  25.     $string=substr($data,rand(0, strlen($data)),1);
    # [, r  C% |! h# T  N" \0 v
  26.     $code.=$string;
    ! }" I) C1 }( F: I! B! w
  27.         // 3.2 加载自定义字体
    % W% c7 m; n+ j* \
  28.         $font_file = '1.ttf';+ V, p* b( D& t3 o+ o
  29.     $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));
    % H; F  O: z5 h% R) h2 g( e
  30.         // 把文字填充到画布/ r) v8 J2 e8 }) w+ w& I8 |
  31.         //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);8 g$ o0 r4 ]* G
  32.     imagestring($image, $fontSize, $x, $y, $string, $color);5 N: ^0 G' y& I& P6 H
  33. }
    & b  B: M+ E- o, V; _( U
  34. $_SESSION['code']=$code;//存储在session里
    " ^; ^9 C: b) y4 F) S
  35. for($i=0;$i<200;$i++){
    4 W1 b/ V0 A- Q
  36.     $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));9 x+ \5 E) f+ i' C( d2 u
  37.     imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);. G# r: Q1 j0 D# c0 R
  38. }5 P: `  n- T. z$ {& l
  39. for($i=0;$i<2;$i++){
    ! Z$ z# @6 {, O' F9 \
  40.     $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));* T$ l+ a7 m" B% Y3 Q' r5 F
  41.     imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);
    / O" j# }( Q" Y& ]
  42. }+ ~& Y9 Q: o9 F! J6 {8 i
  43. imagepng($image);
    4 c8 @* k" ^' f1 m
  44. imagedestroy($image);- r  Q+ ?" d2 ~, S. B

  45. ! i9 y) E: V/ d- K
  46. ?>
复制代码
2 m. E, n' l9 e/ l2 Q
form.php
9 O; M8 s( W0 r  X& ]
  1. <?php  z! u* o$ N1 m! R+ Q6 n6 u$ ?
  2.     if(isset($_REQUEST['code'])){# m: ^: J# ?8 l
  3.          session_start();
    ' Z/ H/ w+ t, N& u+ Q
  4.         if($_REQUEST['code']==$_SESSION['code']){
    ) p. N; L% _' x5 L2 n9 J" L4 s
  5.             echo "<font color='red'>输入正确</font>";/ u( g) a' q2 m
  6.         }else{
    % m: u2 [2 |9 J  F( q
  7.             echo "<font color='red'>输入错误</font>";$ T$ B- w7 ~& ^. |& w5 P
  8.         }" r4 u4 a7 }* m* g
  9.     }6 O( [$ K- h/ E2 v
  10. ?>
    , }: W8 u; t, M
  11. <!DOCTYPE html>
    - q; g' r) @2 ?* g
  12. <html>* d1 V4 D4 I( m
  13.     <head>! c  p% P& p4 k
  14.         <meta chartset="UTF-8" />- {* Q5 K  B7 R! ]4 G7 }
  15.         <style type="text/css" rel="stylesheet">7 }' F; n3 }" }5 o5 a, r
  16.             a{) r8 ~3 \: X6 I) y
  17.                 text-decoration:none;1 r1 z, v$ \1 m' j; T' N. V
  18.                 font-size:30px;/ X& A$ W' I( e* g* o
  19.                 color:blue;
    " y% H7 W- ?; M+ e
  20.             }% k0 L9 q6 n6 D" m# N$ m
  21.             a:hover{  L; @  n4 A) |& u$ I
  22.                 text-decoration:underline;
      B) L  ^  M8 @, A) \
  23.             }+ z3 g" r/ }, a
  24.         </style>
    $ ~; L$ t, a" H  J7 x9 D( g
  25.     </head>5 R- K$ N( P$ X; @
  26.     <body>3 v  W% h# N' R( w% r
  27.         <form action="form.php" method="get">; _3 P; L% u: U2 E
  28.             验证码:  <img id="img" src="index.php?" onclick="changeCode()"/>
    9 w. n" q( ]1 T/ p8 D# C2 `  [
  29.             <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />
    / ^) r: {# ]. i7 w8 v7 J
  30.             请输入验证码: <input name="code" /><br />) T0 M$ Y, w. F+ ~: f* ]2 \
  31.             <input type="submit" value="提交" />- [2 m, |: K8 O
  32.         </form>. \4 M% p" g" a: n! a9 R8 _
  33.     </body>2 O! i+ a  ]- ]) v, U& N; `$ \( f
  34.     <script type="text/javascript">
    ' m0 d: B& P% s" r) l
  35.         
    " e/ h" ]5 B& \* q
  36.         function changeCode(){
    : y5 M8 O0 |( j( z$ V
  37.             var img=document.getElementById('img');& J2 n! ]1 V7 m8 s. O6 I- [
  38.             //img.src='index.php?r='+Math.random();
    $ e/ p$ {  ~2 a8 m
  39.             img.setAttribute('src','index.php?r='+Math.random());
    / Q" ]# z' t2 ]  @: y
  40.         }. c' o, m6 [( M+ k, b/ t  g
  41.     </script>
    9 c. l5 k. j" [2 x: k
  42. </html>
复制代码

7 ^/ k2 F; N9 ~  T; Q) b- B6 x" h8 s
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2024-5-19 23:41 , Processed in 0.113330 second(s), 19 queries .

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