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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[php学习资料] 验证码

[复制链接]
跳转到指定楼层
楼主
发表于 2018-12-16 01:00:13 | 只看该作者 回帖奖励 |正序浏览 |阅读模式
index.php
) T* ]# \/ D4 y7 _+ W! D0 S) F# m" W
  1. <?php
    * c# d3 ]1 o/ q( @% F  V3 D- J: |4 P6 t! ?
  2. session_start();
    3 J6 L; X! t8 P7 L  G. I9 w% w
  3. // 丢弃输出缓冲区的内容 **
    4 q: Y+ w9 I* T$ J* z4 D
  4. ob_clean();' ]  x9 @$ v3 {7 r% o, n
  5. header ('Content-Type: image/png');
    ! E* s' I5 _4 H1 I4 a* Q
  6. // 1 创建画布
    . J' y+ K, \1 F- ^& ]% J
  7. $image=imagecreatetruecolor(100, 30);
    7 H( [3 N+ w2 `7 J+ r3 n* L2 }, S- S
  8. // 2 画一个矩形并填充背景颜色为白色
    ( J# C' j/ R' O1 a; `5 c2 t* J
  9. $color=imagecolorallocate($image, 255, 255, 255);- j( q, b/ p8 Y4 }0 r4 D7 `" H% e
  10. imagefill($image, 20, 20, $color);6 z: \3 M7 `" a; R! H& L9 m" k: a
  11. // for($i=0;$i<4;$i++){
    * [. y+ H% Z: t& g' K7 j! V& U
  12.     // $font=6;
    * f, b8 X- V+ `+ X+ R
  13.     // $x=rand(5,10)+$i*100/4;
    , @6 ]* l# @; P: t# w4 m& _9 v
  14.     // $y=rand(8, 15);, O& P. p; \6 t& [+ t
  15.     // $string=rand(0, 9);
    % V1 |8 s* j% w+ H$ g# Q
  16.     // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));
    6 s: T- O4 s* x! T
  17.     // imagestring($image, $font, $x, $y, $string, $color);: M8 {/ e. I! H/ m& x
  18. // }8 `! }. o" V9 J& [
  19. $code='';
    $ W: |5 \, T5 Y- V
  20. for($i=0;$i<4;$i++){8 G9 G2 E' P/ ?: _
  21.     $fontSize=8;% U: k# r6 G. n; ^) o0 ]& c  E
  22.     $x=rand(5,10)+$i*100/4;
      d6 R" u  m$ O/ t% J
  23.     $y=rand(5, 15);
    3 z; R) U% ]0 R- a
  24.     $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';: M/ `- J. N5 P
  25.     $string=substr($data,rand(0, strlen($data)),1);
    0 t: j* Z: t+ B' E9 D
  26.     $code.=$string;5 B' o; |/ d6 S4 A+ g1 F5 x: p
  27.         // 3.2 加载自定义字体
    - H/ m( B8 Y, l% ~( I0 w) @5 `& r6 x
  28.         $font_file = '1.ttf';
    5 U, g! |8 V3 q/ X, ^* m, x
  29.     $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));, i2 t3 c0 e. r" |- z, Q8 E
  30.         // 把文字填充到画布4 H+ T- ]& ^8 {- x: I
  31.         //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);2 U# K. x9 ^2 J( K6 d  h% o
  32.     imagestring($image, $fontSize, $x, $y, $string, $color);
    7 c: ?! r" j6 m% i6 S8 F+ G
  33. }3 C. [# P0 z* ~% Q' E# X8 ]( f8 Q$ W
  34. $_SESSION['code']=$code;//存储在session里
    6 g7 E" q# [2 u, d- H5 I# i
  35. for($i=0;$i<200;$i++){
    . e- z" r: R/ H5 V9 ?8 [* I
  36.     $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));
    & o- y% K% s& o- a4 y$ D& i" ?
  37.     imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);
    3 o0 x" R/ `  o7 I8 Q9 ~- k
  38. }
    ; {+ \2 l1 r/ k
  39. for($i=0;$i<2;$i++){
    & o& _( J/ o! A/ w
  40.     $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));
    ) J7 T' a. {" o% T1 w2 W5 _
  41.     imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);
    2 O. i+ i' w: D3 U* H$ s
  42. }
    5 t' p- ?0 c0 k" U5 Z
  43. imagepng($image);
    7 D8 P4 Q6 D, H. a4 h0 ]7 w* V6 h) e
  44. imagedestroy($image);7 F' v/ D& A" e" J

  45. ! ^5 g/ w0 r4 V9 n+ s) g
  46. ?>
复制代码
9 B6 m8 G2 E0 _3 f
form.php
# E1 u' |8 w. [( z2 \
  1. <?php
    / V# c$ n/ A: z$ t) F
  2.     if(isset($_REQUEST['code'])){
    1 S5 {) R8 S  u/ C5 X* f( W% P, c
  3.          session_start();: q( }0 K. p+ `
  4.         if($_REQUEST['code']==$_SESSION['code']){+ d1 K& ?& Y1 Z/ m& S
  5.             echo "<font color='red'>输入正确</font>";) P& s9 \% W* W2 o1 \+ y
  6.         }else{5 k/ J* ~! B% C' T7 P+ L; @" E; Z
  7.             echo "<font color='red'>输入错误</font>";
    ! P& ]! |5 H. p7 g! f
  8.         }
    . r; w, S, X4 ^: O3 s7 ?; ?
  9.     }" G) _  a+ \1 m9 w
  10. ?>+ K$ f0 J% f5 x* I0 W) z
  11. <!DOCTYPE html>
    4 o$ u  F( W5 `2 ~$ w
  12. <html>& U3 _  Y8 i2 k3 o; u
  13.     <head>
    + O- T8 q, j% Q
  14.         <meta chartset="UTF-8" />
    5 R4 C8 G5 p9 }/ j. ]1 W$ @& J. g+ t
  15.         <style type="text/css" rel="stylesheet">( k9 h) E4 P( y3 o) _, a; A6 b6 N" H
  16.             a{
    ) j$ f& h$ Q: l7 q, X: a
  17.                 text-decoration:none;- K. L) _; q, d) T7 d' q
  18.                 font-size:30px;
    $ ^9 \7 [2 G, t8 a$ R$ J: h
  19.                 color:blue;
    0 o7 A& _" o7 Q1 p8 M5 w
  20.             }9 c+ }+ N+ B1 }. o% ]$ E' |
  21.             a:hover{, U% n2 `8 P+ L
  22.                 text-decoration:underline;
    $ l2 q" O9 u8 z! b
  23.             }
    & x: @1 X! u& M! u- x* L7 h6 r2 w0 @( y
  24.         </style>& r/ v4 D, E: S, W7 z+ X
  25.     </head>5 S% @, }) y5 O0 }1 S. n+ o
  26.     <body>
    9 }% U# E4 M; u! v( _
  27.         <form action="form.php" method="get">2 ^0 l! K# D( u, h) U  G" X! f: ]
  28.             验证码:  <img id="img" src="index.php?" onclick="changeCode()"/># m( ?5 j% b2 r( _  U
  29.             <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />3 M! P0 O+ n+ a2 J( F0 Z
  30.             请输入验证码: <input name="code" /><br />9 L/ ]7 t# Z3 [7 w7 M' L
  31.             <input type="submit" value="提交" />
    4 E% X; c/ W4 `: }* V
  32.         </form>
    " s) ^" K/ ?8 h
  33.     </body>
    5 A7 z* a- f1 K) \. U% L
  34.     <script type="text/javascript">
    & N4 U& w1 p& k
  35.         
    3 i/ [& x3 V4 p: f
  36.         function changeCode(){8 t, m$ ~( M) E# F1 }  L4 i
  37.             var img=document.getElementById('img');& D# @6 Z' q- T$ c8 g
  38.             //img.src='index.php?r='+Math.random();1 |  {0 A4 ]. E5 S  U# Z, W# _5 D, p
  39.             img.setAttribute('src','index.php?r='+Math.random());
    $ U, H/ K3 C& I  d8 b* T
  40.         }
      T8 N9 u! V( S' J1 k% R! N  p
  41.     </script>
    / V' f" d0 C8 f9 F: h
  42. </html>
复制代码

9 `+ {0 W: F) R7 w( A
8 u1 B2 O  ^8 F; I
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-1-30 16:38 , Processed in 0.068863 second(s), 21 queries .

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