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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[php学习资料] 验证码

[复制链接]
跳转到指定楼层
楼主
发表于 2018-12-16 01:00:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
index.php
/ t4 `- {* o4 g2 F8 u/ t1 C9 d
  1. <?php9 O1 _" Y1 w; U  r& @
  2. session_start();& \) B' l9 \( B5 s
  3. // 丢弃输出缓冲区的内容 **
    ' T5 O6 Q  @5 y( s- j
  4. ob_clean();  ^4 f  e9 D  M5 u# s- k; ?
  5. header ('Content-Type: image/png');
    * Y& n* R, P  a( u
  6. // 1 创建画布
    2 ~9 }  ~6 W& x+ [/ G2 H
  7. $image=imagecreatetruecolor(100, 30);
    - I. C6 p* `* }( z$ w
  8. // 2 画一个矩形并填充背景颜色为白色7 P! l5 U. \: y- i
  9. $color=imagecolorallocate($image, 255, 255, 255);8 ^; }; G: L, ?+ c2 Y! l7 ^
  10. imagefill($image, 20, 20, $color);
    - S7 T+ G+ O% d( D
  11. // for($i=0;$i<4;$i++){8 n. M! r: J. t& G! s' [+ f+ P
  12.     // $font=6;
    * l1 W. p" A: X8 s
  13.     // $x=rand(5,10)+$i*100/4;
    3 V7 ~% W8 R" w5 [' C' L; A
  14.     // $y=rand(8, 15);+ l( x  v7 @0 s3 r. i' z
  15.     // $string=rand(0, 9);
    - D, e" |% _4 i$ [% P7 s4 J6 N/ r( `
  16.     // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));1 G0 F7 o3 o1 O2 H( d; N$ E
  17.     // imagestring($image, $font, $x, $y, $string, $color);
    ) d$ }/ l: h+ c/ |! I# u
  18. // }
    9 n7 O+ i5 f' b3 a
  19. $code='';
    7 r' O2 j" z# B8 Q3 a. H- j% @
  20. for($i=0;$i<4;$i++){
    % y0 `2 s9 t/ w8 U  H. J; _' M
  21.     $fontSize=8;
    , C; p$ }" x* T7 w) Y7 |, P
  22.     $x=rand(5,10)+$i*100/4;
    5 n+ m( i6 H5 f" R* U" I
  23.     $y=rand(5, 15);
    ! ]/ g- U; E1 C/ L" e. r1 _9 H3 F9 {$ S
  24.     $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
    , L& X0 G/ G- _+ A! l
  25.     $string=substr($data,rand(0, strlen($data)),1);
    ) j6 K( `( c" [$ @$ }& G
  26.     $code.=$string;1 {8 ]& `2 }, A8 v4 w
  27.         // 3.2 加载自定义字体9 H1 X) \2 j  K- _' E- Z
  28.         $font_file = '1.ttf';+ X. [& Q$ A1 X5 a- n8 j+ k  N# M/ n
  29.     $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));$ l* }. R1 H( `; V
  30.         // 把文字填充到画布8 A' d& ^7 r, r- j9 x7 S
  31.         //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);$ I9 t0 @! p) Y7 N) w
  32.     imagestring($image, $fontSize, $x, $y, $string, $color);) J5 K3 C, m* E5 Y. O6 J6 z
  33. }8 t1 }1 S; W, V
  34. $_SESSION['code']=$code;//存储在session里
    # K2 I' j6 {# N( G' L- p' ?& T
  35. for($i=0;$i<200;$i++){' W' p- _+ @' {4 @; E0 v! t2 b/ A
  36.     $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));! F9 S. R' ^! H4 \
  37.     imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);" j/ U. J9 K3 t1 a% T% j
  38. }1 |# r* s9 Y; [/ G: w4 H8 ~
  39. for($i=0;$i<2;$i++){
    $ M; A9 E- D7 [6 r
  40.     $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));
    - v. y& X0 C9 y3 K5 R
  41.     imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);. \  ~( A. w8 O. y7 `$ h- b
  42. }
    5 g1 V, \0 X/ v- c  B9 a: {4 V; s
  43. imagepng($image);/ N1 I3 {+ s$ }
  44. imagedestroy($image);
    * J7 E  K% U& g; M8 B9 D. x- D0 t

  45. . l) V4 _+ L3 z% |4 h8 i
  46. ?>
复制代码

  D% a5 S; m2 r% f: I5 jform.php; y: Y7 i2 w4 L& h
  1. <?php
    " f. \! s2 T1 H4 t
  2.     if(isset($_REQUEST['code'])){! I& f% s% ]# O6 F& ~  h
  3.          session_start();
    2 ]6 Z& v3 O8 q: r
  4.         if($_REQUEST['code']==$_SESSION['code']){* `0 e% ^+ \$ Q  i
  5.             echo "<font color='red'>输入正确</font>";7 J8 z! x" y$ ^
  6.         }else{
    % r9 ^) d$ Z- O$ F
  7.             echo "<font color='red'>输入错误</font>";
    ( e6 \; r1 _; ]3 \: a0 n
  8.         }
    ( K1 b' z# y7 z) j: o8 Q* e
  9.     }
    ; x- U  N9 R, _. j) z5 K# t
  10. ?>- N6 a8 B0 h3 [1 F5 M4 P# m4 }
  11. <!DOCTYPE html>
    0 A2 d6 K' H7 \$ x: e, }
  12. <html>
    ! [# [" n$ X- {9 \$ y4 `  O
  13.     <head>$ B5 O: L: ?+ e7 g" N% ^0 f
  14.         <meta chartset="UTF-8" />
    . I3 y, ^3 j  H: L
  15.         <style type="text/css" rel="stylesheet">
    7 q" N" E( N. R' u0 Q" D
  16.             a{
    + C4 W3 _) c1 S" P8 v
  17.                 text-decoration:none;+ x5 A2 v8 ]1 P3 s4 S: F: L2 W  v1 G
  18.                 font-size:30px;6 P* ^& G9 G: F
  19.                 color:blue;9 O3 q5 r  O  b% G" r9 r
  20.             }
    / B9 j' V* }: S# J. \
  21.             a:hover{
    & a; Q3 c" ~4 S+ P
  22.                 text-decoration:underline;9 u1 a$ `  O2 X8 c( z
  23.             }9 E, s" C7 y* G1 q% t' I% K: A1 I, t/ c
  24.         </style>8 J/ j+ u) Z# B
  25.     </head>
    / c  c+ q) |. n) `4 \! @4 f- A
  26.     <body>
    " n. u5 H& _$ E2 ^$ O. E
  27.         <form action="form.php" method="get">
      \. O' S. J9 K. J
  28.             验证码:  <img id="img" src="index.php?" onclick="changeCode()"/>( `# d$ J; H+ b+ e
  29.             <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br /># j. J2 W) y) S# L+ B+ P, p; C
  30.             请输入验证码: <input name="code" /><br />
    7 J, P) j8 a: h7 q% G
  31.             <input type="submit" value="提交" />
    8 F5 y( H# z* l. q& o, r/ E/ f* Q
  32.         </form>6 [! f8 k9 I1 ?  J1 L
  33.     </body>
      h) j: {9 g2 O0 O6 K4 y
  34.     <script type="text/javascript">! V) H& C- u4 p- K. Z
  35.         . @: R3 b5 l% t
  36.         function changeCode(){5 b' s- K9 g& [( K7 A0 l# p, z
  37.             var img=document.getElementById('img');
    0 Y$ W3 j1 A; ?: L  o
  38.             //img.src='index.php?r='+Math.random();
    : \1 o' C/ o4 ?% l# w5 n
  39.             img.setAttribute('src','index.php?r='+Math.random());
    ; d, v" u- o- Q* g
  40.         }
    & p0 j+ ?% J9 B, }" ^
  41.     </script>+ E% ?+ F2 e! q5 |4 L
  42. </html>
复制代码
2 d6 a0 ]/ L$ r4 l0 D* o

' G. V1 |6 k; P4 j( |7 t
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-9-21 03:19 , Processed in 0.054379 second(s), 20 queries .

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