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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[php学习资料] 验证码

[复制链接]
跳转到指定楼层
楼主
发表于 2018-12-16 01:00:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
index.php3 ^% [% O7 a9 ?3 M
  1. <?php6 C. N* v( b( Y+ m9 P# Z
  2. session_start();
    ( ^9 L, M5 [$ T( L4 V# M. O
  3. // 丢弃输出缓冲区的内容 **
    ' B! j3 ?; @7 b+ g$ k9 X# D# [
  4. ob_clean();
    * b1 m) B5 S3 T% f$ C' s" z' ?
  5. header ('Content-Type: image/png');
    ( o$ c* z4 |5 j* A; [* A
  6. // 1 创建画布
    : P9 H0 r  I3 _1 P& S
  7. $image=imagecreatetruecolor(100, 30);7 @" i% h- c; x' O8 Y
  8. // 2 画一个矩形并填充背景颜色为白色3 W" K" X& N9 C
  9. $color=imagecolorallocate($image, 255, 255, 255);
    + s: h) Q: H9 I$ R5 }  X- d. Z
  10. imagefill($image, 20, 20, $color);
    . K) G2 h4 P6 z1 g; B
  11. // for($i=0;$i<4;$i++){" R3 Z# w- `3 B
  12.     // $font=6;! M/ j5 V. [2 v4 g
  13.     // $x=rand(5,10)+$i*100/4;
    + k2 y& g# ~5 ]- c
  14.     // $y=rand(8, 15);
    2 E& E8 n4 w/ U1 \7 `
  15.     // $string=rand(0, 9);" o  S5 u% W2 y. P0 D
  16.     // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));! `; ^, w" F. d- Y' ?
  17.     // imagestring($image, $font, $x, $y, $string, $color);
    $ q7 }5 F  w& u3 h; \2 X) G5 C
  18. // }
      s& l. @/ t. \( |1 z2 ]
  19. $code='';7 ]4 \9 O( u* G4 z" s- O# j
  20. for($i=0;$i<4;$i++){+ }4 |) K) v; E: x
  21.     $fontSize=8;
    ) h# Z9 J6 }  T  _
  22.     $x=rand(5,10)+$i*100/4;/ A$ J% ]. R. ]' [, r% Y
  23.     $y=rand(5, 15);
    . I( q" X" }0 [/ F, ~- ?( O( r% Y
  24.     $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
      W; Z' s* j- k) P: b2 Q) |
  25.     $string=substr($data,rand(0, strlen($data)),1);
    9 X" d' o, R. h; E0 j5 M: c
  26.     $code.=$string;
    ) T1 N7 D, N+ t, A1 Y1 U
  27.         // 3.2 加载自定义字体1 ^' ?7 @4 i8 u2 Q7 D# [2 z; L  B
  28.         $font_file = '1.ttf';
    $ D  Q* D" k# N" }+ U
  29.     $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));
    ' I3 ~) P" [; ~
  30.         // 把文字填充到画布$ ]0 M9 x9 \$ e4 H
  31.         //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);
    $ Z0 p8 U5 F' m+ P1 ?5 s8 p
  32.     imagestring($image, $fontSize, $x, $y, $string, $color);, \8 P% S" E6 t! {& z
  33. }
    * b1 [. D/ `$ v
  34. $_SESSION['code']=$code;//存储在session里
    $ i% q: S$ }; B" n2 i
  35. for($i=0;$i<200;$i++){, U# f! z+ T; I$ \2 K1 s7 l
  36.     $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));
    & A* }' L+ ?' p1 U/ f
  37.     imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);; b4 w; V& b% x3 B
  38. }
    7 o% j2 `& E( d/ P( S) u
  39. for($i=0;$i<2;$i++){
    4 [+ l; B7 e8 h: b, R. m+ r# U# W
  40.     $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));0 b/ b; R- Q% _% j
  41.     imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);* y( q1 D3 k0 B
  42. }
    9 f1 R& G% l1 i' `8 Q: D. ~
  43. imagepng($image);% i# G2 j3 ?8 v  o1 Z5 }: _" I
  44. imagedestroy($image);7 X# w1 f% {6 I, ^: B
  45. . g( n( h" {! r( u/ [% \+ e7 C
  46. ?>
复制代码
" W: @) @; v7 B3 m, }5 h- j
form.php
2 R3 W. J0 J# ?
  1. <?php0 L, N) h- Q* {$ l' ^6 E
  2.     if(isset($_REQUEST['code'])){3 P% V5 \3 a; l  X
  3.          session_start();" G2 e4 _* g5 E/ J; b" C3 m
  4.         if($_REQUEST['code']==$_SESSION['code']){' N1 |  M6 U( @$ A
  5.             echo "<font color='red'>输入正确</font>";' j/ h  [. k+ k% ?$ e
  6.         }else{- s1 [6 v9 Y5 l" I
  7.             echo "<font color='red'>输入错误</font>";9 O- E" G, x2 J" d; B
  8.         }( n8 L3 ]( ?3 e7 z" h4 c5 L+ O1 t7 G
  9.     }
    # u8 u5 a4 w0 d& S$ c5 U, I# ^
  10. ?>! t- a; ~2 A& t7 ~; I
  11. <!DOCTYPE html>' k* |5 {( b9 ]4 @7 Q/ i
  12. <html>5 [& O0 _5 G" ^2 U5 \
  13.     <head>
    $ a8 }  e7 q" p: H$ h
  14.         <meta chartset="UTF-8" />
    , |6 D% d7 ]) z! Q. |
  15.         <style type="text/css" rel="stylesheet">0 N* N  U1 D8 X! j
  16.             a{
    * N0 X+ E" ~5 f, \4 s7 {$ U
  17.                 text-decoration:none;
    ; k/ m9 y0 Q! D) {  ^7 u
  18.                 font-size:30px;
    & }( {* D5 Y- g2 ~
  19.                 color:blue;
    , A+ Y/ I2 B& v* h& L9 V# {  `. h
  20.             }' L; L' u: f: c4 q) c
  21.             a:hover{' E, [/ R. l' j6 i5 u
  22.                 text-decoration:underline;
      J7 K8 M  x& _
  23.             }" v0 H5 r$ Q  {9 w9 Y
  24.         </style>/ G0 C# _, S8 z2 [
  25.     </head>  U1 y% X+ i" x5 R
  26.     <body>$ g! b4 M; T5 O. |' ~1 B, c7 z* B& p
  27.         <form action="form.php" method="get">
    3 N& P+ s$ N- @9 w3 o, f
  28.             验证码:  <img id="img" src="index.php?" onclick="changeCode()"/>
    ) M; f5 R1 D/ h0 X
  29.             <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />
    0 p+ E% Q" w6 ~2 d6 ?: ~. k
  30.             请输入验证码: <input name="code" /><br />7 ^8 E/ c! G2 V2 ~% N9 ^5 j
  31.             <input type="submit" value="提交" />2 u( q, B6 v9 N( i3 ]$ k
  32.         </form>0 f2 z5 ~2 J0 W
  33.     </body>; A3 {1 n: M8 y$ q- F8 f% q& c
  34.     <script type="text/javascript">
      p4 b: k# e6 W- a3 _
  35.         
    9 j5 U4 B, l0 o+ \3 j( a
  36.         function changeCode(){
    ! X9 a  |. a( b2 X& g) Z
  37.             var img=document.getElementById('img');
    " z& h- V9 j2 I8 S) d. G
  38.             //img.src='index.php?r='+Math.random();9 `4 {/ W0 o2 i2 L" e
  39.             img.setAttribute('src','index.php?r='+Math.random());8 @- Y) r# L3 P7 i  s
  40.         }3 T6 y- f& |8 K+ }
  41.     </script>
    $ K0 F2 O0 x7 Q  K# ~0 ^. h
  42. </html>
复制代码
4 c7 u) n- i7 z  R% [/ R- G% @

8 U( Q9 Y- A4 N
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2024-5-2 02:07 , Processed in 0.110055 second(s), 19 queries .

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