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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[php学习资料] 验证码

[复制链接]
跳转到指定楼层
楼主
发表于 2018-12-16 01:00:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
index.php7 o- ^1 N' [) b+ p& U
  1. <?php
    8 C: \. m, W0 `: p9 r: p7 p
  2. session_start();4 H# J' W  f7 G% a
  3. // 丢弃输出缓冲区的内容 **
    / E: J+ M8 _$ [* o/ u
  4. ob_clean();
    " A9 ~3 j5 O* R2 B, {+ j
  5. header ('Content-Type: image/png');
    $ I, @9 N2 Q4 R0 G; C1 E3 ?+ t
  6. // 1 创建画布. \1 S, m) Y: N6 V: v+ h  h0 E, W
  7. $image=imagecreatetruecolor(100, 30);7 s* g2 _# z2 \5 e4 ?6 o. y
  8. // 2 画一个矩形并填充背景颜色为白色7 B( `( s1 x  m# ?, i$ X
  9. $color=imagecolorallocate($image, 255, 255, 255);
    0 r% S! r/ g5 n  z) S$ B
  10. imagefill($image, 20, 20, $color);
    * w+ N9 U, ?. d, Q0 ~' k+ ]
  11. // for($i=0;$i<4;$i++){2 \2 {3 A/ U$ \2 m# C9 F0 j
  12.     // $font=6;
    0 w: o0 P& S4 }4 H
  13.     // $x=rand(5,10)+$i*100/4;
      `- z+ u4 |5 l: s. W* f( [) _
  14.     // $y=rand(8, 15);7 k! l6 i% B. p! V1 t! u6 [& i$ P! B
  15.     // $string=rand(0, 9);/ x: Y1 ]8 _- s$ U( O
  16.     // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));
      H1 L/ Y, j. @1 Z3 Q
  17.     // imagestring($image, $font, $x, $y, $string, $color);9 U4 p! k: o. ^; B
  18. // }
    - `! |0 \) d: _, s$ i/ Y
  19. $code='';# }' m/ X7 r) U& y& ^
  20. for($i=0;$i<4;$i++){. g+ b  m& u/ ]2 ^: @
  21.     $fontSize=8;
    9 A% q8 u' Y' `' m( O" W
  22.     $x=rand(5,10)+$i*100/4;' g9 `: A2 Q% |6 M( {2 S
  23.     $y=rand(5, 15);
    2 c0 a( Y8 Y! q! j* Z
  24.     $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';" s1 R8 p, p- J" d8 F
  25.     $string=substr($data,rand(0, strlen($data)),1);
    % d- ^: q9 Z' W" W  E7 e5 V: F" U" D
  26.     $code.=$string;: f: m* n* R' b% n: f6 t
  27.         // 3.2 加载自定义字体
    & V+ X3 R$ v5 j% h
  28.         $font_file = '1.ttf';/ f0 c2 G/ r% v  ]8 l$ J
  29.     $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));
    % R% ]7 K! D/ y5 [  E6 r& C8 z
  30.         // 把文字填充到画布, n3 @/ ^; E- Z+ T6 F. ~0 @; H3 T
  31.         //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);
    - }* S% I3 \( {# n& P$ j
  32.     imagestring($image, $fontSize, $x, $y, $string, $color);
    & F* r# o& H! s( i  Z  b
  33. }7 J% n; {3 k" v3 a( {# Q
  34. $_SESSION['code']=$code;//存储在session里
    $ r/ o% |  d; W1 ?
  35. for($i=0;$i<200;$i++){5 n7 E' O) q4 |' H/ I9 e% p- _
  36.     $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));! Z0 Z! k% C( U
  37.     imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);
    ( E: _" p0 L& j9 Z' f; ~
  38. }
    # o+ |* S! f' v* N1 |- C; _. F
  39. for($i=0;$i<2;$i++){
      O7 o( N( @# Y
  40.     $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));
    4 g, p  n# X, N# m/ y
  41.     imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);
    6 J9 j  m' K4 Z3 j) e0 s6 G
  42. }9 _" U/ R/ P) Q$ l" N
  43. imagepng($image);# w7 o/ ?0 C: z7 Y( l3 T
  44. imagedestroy($image);
    ) P+ V5 ^! U5 n0 g! T

  45. : J# K& h. \% M* d9 S
  46. ?>
复制代码
* D0 h; H9 x& X7 }8 c& Q& j; Y
form.php! k$ i! o8 R) S1 E/ S2 w( I2 o/ ]
  1. <?php
    3 F! o* W2 c( f
  2.     if(isset($_REQUEST['code'])){
    0 \4 C% J$ N7 ^- m% ~; H; h
  3.          session_start();; j- c% F% K+ g/ v0 g
  4.         if($_REQUEST['code']==$_SESSION['code']){- Y% L" f7 U8 c) b6 D! [
  5.             echo "<font color='red'>输入正确</font>";( z3 y* ^* X+ R/ |* i# X8 g& |/ {6 T
  6.         }else{
    - C- y! S; Z' {1 c7 X( I
  7.             echo "<font color='red'>输入错误</font>";; s4 \, @( f+ p* g$ H* b8 ^& ^3 `8 z: U
  8.         }
    : t: C  q" T; I( ^1 P3 Z
  9.     }
    ) S- N  c2 V) n
  10. ?>- z3 V; d; Y" O) L
  11. <!DOCTYPE html># c, I) k+ M0 A4 p: Z
  12. <html>' D' a+ S; }7 X3 ?/ W) R; C1 t0 Z4 j
  13.     <head>
    " z, O$ A1 ~) K5 t3 D
  14.         <meta chartset="UTF-8" />
    $ d# o* Q& h+ q/ Q
  15.         <style type="text/css" rel="stylesheet">
    + j+ `) _( Q& M6 v& ~, Y
  16.             a{! u, o6 C% z, z
  17.                 text-decoration:none;
    * E  w) N3 e9 _0 E
  18.                 font-size:30px;  s! z4 v1 O! ^0 Y" y
  19.                 color:blue;' }8 f8 u' X, u: n9 G# I
  20.             }
    % n5 R- e* F5 Q# L7 Q
  21.             a:hover{/ Z; Z6 ]$ [2 T" O% F
  22.                 text-decoration:underline;# o. t3 m/ a* `
  23.             }" S& _, `' L+ D. `' k0 n
  24.         </style>; `# u8 d/ q* o
  25.     </head>' @0 e( J0 i8 W2 i, p8 }4 F
  26.     <body>
    : m' f+ Q) h6 o1 K9 R
  27.         <form action="form.php" method="get">
    ! @; i. c  \$ ?6 ~1 D4 Q. k. C" y
  28.             验证码:  <img id="img" src="index.php?" onclick="changeCode()"/>
    , k$ ^+ B6 W' J- R) G. d
  29.             <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />+ L$ b; }9 L5 P
  30.             请输入验证码: <input name="code" /><br />! u  U1 u3 ]1 T% d0 f
  31.             <input type="submit" value="提交" />
    1 i4 i4 a- O' _' L; \
  32.         </form>4 `/ ^& Y% W* ~  W7 c
  33.     </body>
    8 y& U" [. ]/ h4 F0 E
  34.     <script type="text/javascript">
    5 z- N! ?  C, m
  35.         ! q/ k: r& O2 @6 M6 T7 `' i
  36.         function changeCode(){: H, W' V0 D2 f7 h1 V: T- A
  37.             var img=document.getElementById('img');
    ( Q2 l% {$ y' N2 _( G+ a  D9 b1 Q
  38.             //img.src='index.php?r='+Math.random();7 @4 c" a. n3 O8 {) t! j& O5 E
  39.             img.setAttribute('src','index.php?r='+Math.random());) f. P- t& @0 @( G
  40.         }
    $ r7 Y3 e+ O  W8 H5 f9 U
  41.     </script>7 X3 p1 f2 E0 a. H$ @, [2 u
  42. </html>
复制代码

8 J  b5 N: s) j! c; C+ F" t
5 C* Q7 w6 m# S6 e' Y& v
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-8-4 23:58 , Processed in 0.048886 second(s), 19 queries .

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