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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[php学习资料] 验证码

[复制链接]
跳转到指定楼层
楼主
发表于 2018-12-16 01:00:13 | 只看该作者 回帖奖励 |正序浏览 |阅读模式
index.php' e5 M+ V6 h/ v& c. w
  1. <?php- v9 M) |' ?( C. h, ~, b+ X' ]* u
  2. session_start();
    $ m) X0 f) q; R- _+ U
  3. // 丢弃输出缓冲区的内容 **% z% X  c: y! C$ k
  4. ob_clean();
    ' D  k, z, b4 k+ |
  5. header ('Content-Type: image/png');$ _# d* j% o3 P# Q
  6. // 1 创建画布; v8 d% |0 Z* ?" W( x  H& p
  7. $image=imagecreatetruecolor(100, 30);
    ' W: h' ]3 Q+ W
  8. // 2 画一个矩形并填充背景颜色为白色5 H  E- S4 x* G/ R2 v
  9. $color=imagecolorallocate($image, 255, 255, 255);7 B8 ~3 t: n* f+ K5 N+ r- p7 E9 B
  10. imagefill($image, 20, 20, $color);
    . H, s7 y' U. S) s  F
  11. // for($i=0;$i<4;$i++){( i: g4 A, \- l/ Y: h3 l
  12.     // $font=6;
    * U! r# _& h3 |3 |$ ~. g
  13.     // $x=rand(5,10)+$i*100/4;" y  {" U% m' q, m( X, B
  14.     // $y=rand(8, 15);  b2 r. T7 y% ?; ^; Q* z' W
  15.     // $string=rand(0, 9);
    ' I6 \) @2 M/ R0 r8 `
  16.     // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));0 e. Z9 ]! ~, _) B' @* u9 \
  17.     // imagestring($image, $font, $x, $y, $string, $color);
    1 m! U' N( s: o- P
  18. // }% @( r/ x# H0 Y2 L4 o( ^
  19. $code='';: _' e- G2 l5 P" @3 O/ F7 m! C
  20. for($i=0;$i<4;$i++){
    : s& F5 U4 W1 M( \
  21.     $fontSize=8;
    2 K. K) Z% V; c/ r2 T5 `' [
  22.     $x=rand(5,10)+$i*100/4;: G. Z% p1 ~6 s" M  ~
  23.     $y=rand(5, 15);- p9 n) d/ F* j
  24.     $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
    $ f: d5 b4 m" z& d5 t7 U4 m$ z$ p
  25.     $string=substr($data,rand(0, strlen($data)),1);
    ! t% T7 V( h2 V- G( m
  26.     $code.=$string;0 M# E4 S, G/ F7 g* d. n( T
  27.         // 3.2 加载自定义字体4 E* c0 e% ?5 A9 B4 R/ U& ?
  28.         $font_file = '1.ttf';
    0 N- F/ d) j, D$ a3 P$ {! q& m
  29.     $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));
    : c4 M- L  ?. ?# A2 _" s
  30.         // 把文字填充到画布, Y4 U7 b6 A* q. O2 B/ _; d
  31.         //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);* E& a* d; f+ j
  32.     imagestring($image, $fontSize, $x, $y, $string, $color);
    & C" L1 h+ ~- {2 @! A  J
  33. }: k) X# \. @. h) |
  34. $_SESSION['code']=$code;//存储在session里
    4 v( }. f1 N, ^' I
  35. for($i=0;$i<200;$i++){$ v+ W# k0 D) m0 g# I$ X/ x# ?- h" Z
  36.     $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));
    ! h+ k: I5 B7 P
  37.     imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);
    ) \& Q, [; e+ H! N9 s# P3 S
  38. }2 W( N" s! |% z4 B% o
  39. for($i=0;$i<2;$i++){. W: v8 W% X4 R+ _1 F
  40.     $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));% L! R' `4 B  c
  41.     imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);
    # U$ `$ [- V2 L
  42. }
    0 Y5 `( v" I  _$ H% f6 Y, y
  43. imagepng($image);
    6 Q! m" S' G( l) g" e" R
  44. imagedestroy($image);
    ) ^. [7 n, ~0 R% r0 U7 I& m9 l
  45. # h( a$ g  J1 p' Z7 X: ^1 f
  46. ?>
复制代码
: r8 N; o' n& z' u5 u/ b1 w' z4 Q
form.php& P- k. v% l( h$ }1 U3 A! o" V* b
  1. <?php
      g! d. u3 ^- D3 V9 m( [% @
  2.     if(isset($_REQUEST['code'])){( H- L+ B: m0 O
  3.          session_start();% P0 j/ {; b: Z8 Q6 h1 X
  4.         if($_REQUEST['code']==$_SESSION['code']){5 h0 E0 q! Z1 x) L+ P- P
  5.             echo "<font color='red'>输入正确</font>";
    , b! \  R5 Q. t' m' ]
  6.         }else{
    2 c5 a# E, g# i9 J; j
  7.             echo "<font color='red'>输入错误</font>";7 m0 x% Y+ P% [# F
  8.         }# P5 {& s* q" ~0 l/ V7 L1 ?
  9.     }3 R! g, E7 l6 v7 ^+ ]0 \7 u5 Z' H# h
  10. ?>6 O0 G& C$ N" R2 Y; Z
  11. <!DOCTYPE html>
      m& s" u0 p) N3 l
  12. <html>* }. l9 r  J- c' |- d# M8 U
  13.     <head>, X  Z. h* ]( j2 u5 ^5 S
  14.         <meta chartset="UTF-8" />% @0 i  l: R" P# r- q( _3 b. u" b
  15.         <style type="text/css" rel="stylesheet">" y0 N" o/ \9 Z( ?; o0 w
  16.             a{
    / G! T+ M0 d1 c% F( n' e" t
  17.                 text-decoration:none;
    - `: u4 h7 L) g+ [9 N) I
  18.                 font-size:30px;9 [- @/ M8 S6 Q
  19.                 color:blue;
    1 z% Z- s0 n$ T
  20.             }# f' r6 C: g- P
  21.             a:hover{
    ; F+ [! C5 Q3 p  s
  22.                 text-decoration:underline;
    / s3 `% d: \! e6 f+ o
  23.             }: v2 W7 N: j0 W5 _/ L
  24.         </style>6 S# a1 V! }  \& \$ o) X
  25.     </head>
    % f/ e0 s. A4 @1 t: P# k
  26.     <body>
    " n$ P0 g2 [) I' V+ G2 r4 Z; l
  27.         <form action="form.php" method="get">
    / Y6 \- A7 R/ E" A8 E" w4 Q! l
  28.             验证码:  <img id="img" src="index.php?" onclick="changeCode()"/>; P. I8 u! n0 R/ A. x
  29.             <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />% K( I" I7 q8 M
  30.             请输入验证码: <input name="code" /><br />5 K, N7 M7 x% l0 R3 A0 T/ a' S
  31.             <input type="submit" value="提交" />" _" ^' S3 N  @7 R
  32.         </form>
    & r, c/ v* i+ s1 H; l, b
  33.     </body>8 F9 l  @/ }, a1 G, u
  34.     <script type="text/javascript">9 z; e  ]8 ?& j) m4 S( c
  35.         
    + d4 u4 Z  \3 ]9 g  e
  36.         function changeCode(){
    2 n# M) t3 J: P
  37.             var img=document.getElementById('img');# c' ?. R- h( A
  38.             //img.src='index.php?r='+Math.random();, [* {/ ?2 G" _. r$ S' S
  39.             img.setAttribute('src','index.php?r='+Math.random());
    * d2 Q; C# i* d' Z
  40.         }
    , f& O/ O" F& b7 v
  41.     </script>4 ?5 x$ A4 s5 @. t+ d* X
  42. </html>
复制代码
0 I5 s& }$ u% W+ u1 {) ]6 z
) m. i( }/ ]- T! n
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-3-17 21:47 , Processed in 0.059552 second(s), 20 queries .

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