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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[php学习资料] 验证码

[复制链接]
跳转到指定楼层
楼主
发表于 2018-12-16 01:00:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
index.php
. D# ?4 X2 N! @( Z
  1. <?php7 X# f: T6 W5 E! \( c5 [, L( Q4 `
  2. session_start();
    . v, ?" O* z: C+ j. F( `  q% [6 ]
  3. // 丢弃输出缓冲区的内容 **
    , a, W/ y( g& i: \6 K) `/ ]
  4. ob_clean();3 T. v  x2 E( H1 s* e
  5. header ('Content-Type: image/png');
    # D: }1 u% Y) l
  6. // 1 创建画布
    . R4 B) P, }1 B/ v& g; M; P
  7. $image=imagecreatetruecolor(100, 30);
    7 t% V, M1 x/ ^: Y  Q: u! E
  8. // 2 画一个矩形并填充背景颜色为白色
    . Y, S# D/ J0 `- o3 M& u3 b
  9. $color=imagecolorallocate($image, 255, 255, 255);
    * V3 }! [$ Y$ u( b- E, [* j
  10. imagefill($image, 20, 20, $color);1 o7 @+ m4 j' K1 W  i
  11. // for($i=0;$i<4;$i++){
    1 O8 u6 l% k: {* x
  12.     // $font=6;# o) h6 ^2 W" {6 @9 l3 _
  13.     // $x=rand(5,10)+$i*100/4;- X  s  O1 g+ u1 ~4 y* y: ?2 f
  14.     // $y=rand(8, 15);
    2 Y( i. l1 i+ C7 \9 q
  15.     // $string=rand(0, 9);2 N, a9 ?) k9 n  L% [! k: D
  16.     // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));* ^+ u- M5 B7 u
  17.     // imagestring($image, $font, $x, $y, $string, $color);, ?. Y5 [" J$ ~
  18. // }
    9 Y* M2 R* h. c1 ]% F; z6 ^
  19. $code='';
    ( C( K" M$ b. f* m; Y1 {* _2 y* ~- E
  20. for($i=0;$i<4;$i++){. L; j! |+ k9 R( \0 D4 v# X
  21.     $fontSize=8;
    * \# I  |6 t3 I( s
  22.     $x=rand(5,10)+$i*100/4;7 \& m/ t- K( Q8 q5 c! J
  23.     $y=rand(5, 15);
    ; a- u. E8 [: w  q" e: @
  24.     $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';# H7 W' _1 h1 {1 J$ P% e, `
  25.     $string=substr($data,rand(0, strlen($data)),1);1 Z' ?3 j$ |& ?# w2 T. y4 B
  26.     $code.=$string;
    : U" g3 D# y& P) M
  27.         // 3.2 加载自定义字体
    & Z: {- G) \/ j* n7 y4 i
  28.         $font_file = '1.ttf';
    ) R3 ~6 r# Z  d' ?
  29.     $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));
    " z! W4 E( q2 ?3 N7 d& ^  p
  30.         // 把文字填充到画布% c  O4 {- u; W
  31.         //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);8 h: Z1 h" J. F1 y  w& |2 L
  32.     imagestring($image, $fontSize, $x, $y, $string, $color);, q" h) x5 l: y/ t2 d# T
  33. }
    $ _2 _; P" t4 u
  34. $_SESSION['code']=$code;//存储在session里6 j: r" Q- q; f% G$ u
  35. for($i=0;$i<200;$i++){
    + Y. W5 T7 f" _7 h; e
  36.     $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));
    % r' H& J- x$ A- Y
  37.     imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);
    : {! n, g3 I: U$ E0 }) c" u' U' B
  38. }
    # K3 S. b6 l$ N* y( o) O2 x  C
  39. for($i=0;$i<2;$i++){
    ) q- z2 L) l9 T/ R. z- k, m
  40.     $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));
    : ~7 z& l% L+ f
  41.     imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);
    3 \+ a/ y( [7 ~# x" d4 G, X/ p& Y
  42. }
    ; S! R! @$ J6 b6 m8 ^
  43. imagepng($image);+ \8 A* j! r/ ~/ f7 _, G
  44. imagedestroy($image);
    : t; r6 x/ k6 Y0 [+ G2 z& @
  45. . G6 p1 b+ o* H8 @. d
  46. ?>
复制代码
. V$ c& R0 T4 S
form.php$ m! u8 S: o2 @0 {: b  l& c2 _
  1. <?php/ e" C: ?: s8 P  C' D6 S
  2.     if(isset($_REQUEST['code'])){
    / n* H: w& Q& J5 e
  3.          session_start();: d! e( ]+ R: m( c
  4.         if($_REQUEST['code']==$_SESSION['code']){
    2 n# _8 _2 h" ?; B8 G* J( Y
  5.             echo "<font color='red'>输入正确</font>";
    ( q: C4 j( ~  v! g8 N5 e* S
  6.         }else{
    + u5 s; k2 k% B% t
  7.             echo "<font color='red'>输入错误</font>";
    ( O" e% ?2 U6 ]* ?
  8.         }
    9 @8 W$ U* W6 W, \: [! P% W
  9.     }
    2 |: s& F& R5 q7 Q% K
  10. ?>
    4 M* @5 D1 f2 `# v. w+ B
  11. <!DOCTYPE html>
    # g9 m* f- u  p, E
  12. <html>
    9 T7 V- U, [; X* Z; }9 B& j0 N
  13.     <head>
    # u% P5 M; m6 v( e+ |  s
  14.         <meta chartset="UTF-8" />! N" L' @3 A+ v' A2 ^
  15.         <style type="text/css" rel="stylesheet">
    ) n4 ^9 d2 n$ N3 a8 A* m# L' F
  16.             a{1 b- e- {. |+ b
  17.                 text-decoration:none;( v, W8 [2 E, h1 O
  18.                 font-size:30px;
    / H. O) g' L* A
  19.                 color:blue;
    # x' s  y- H6 z/ W
  20.             }3 G5 k2 t: Y$ ^. u
  21.             a:hover{( O, w& O9 R& U6 |; x
  22.                 text-decoration:underline;: @/ \9 |: g/ u% b8 o# ^
  23.             }
    # h; L, p5 a% H. P. S/ ~5 N, ], A1 P* H
  24.         </style>
    . r; r3 m2 P2 U( T( p
  25.     </head>% Q" Q* j+ c' c' @) [6 T) ]. X5 u# |. Z
  26.     <body>
    ) U0 e* s- \6 v6 y9 H* s
  27.         <form action="form.php" method="get">& w6 o0 q( E6 C
  28.             验证码:  <img id="img" src="index.php?" onclick="changeCode()"/>6 A% U, {; I$ M1 V1 S
  29.             <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />7 p; z5 h5 B  N) W, T% f1 ~
  30.             请输入验证码: <input name="code" /><br />
    & n6 O: _( c1 L& T' y
  31.             <input type="submit" value="提交" /># n3 E- W: U. b
  32.         </form>" h" R. {3 _! T" T, l* q! d
  33.     </body>
    0 U  Q/ M; B6 T
  34.     <script type="text/javascript">7 [) u8 V$ V6 X; e1 Y7 \
  35.         
    / S6 y! a  k7 X# y8 ^: j/ M! d4 \
  36.         function changeCode(){# L1 Z, Y! g9 d. Y
  37.             var img=document.getElementById('img');
    6 i; s/ a3 }" E" S
  38.             //img.src='index.php?r='+Math.random();* U9 {+ O' l' j8 J4 A
  39.             img.setAttribute('src','index.php?r='+Math.random());
    / M, Q1 f8 O  @" A3 V* W1 X* x9 l
  40.         }
    * r7 z2 U% q: A. z& C
  41.     </script>) o) D. v( T4 b% R' p* Z: D
  42. </html>
复制代码

: L7 b' N) V6 w) X6 t9 n) u" G- L0 Y. T; K& j2 f7 o6 U
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-1-30 11:35 , Processed in 0.081919 second(s), 19 queries .

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