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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[php学习资料] 验证码

[复制链接]
跳转到指定楼层
楼主
发表于 2018-12-16 01:00:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
index.php" V4 Y: r/ E: K: L8 J
  1. <?php
    # I+ f7 v2 a9 L$ j6 K* |5 u  }9 C# A
  2. session_start();  {$ F( I: D* B) \0 H2 d
  3. // 丢弃输出缓冲区的内容 **! y4 n1 U6 z4 f1 h- B  {
  4. ob_clean();, {/ k2 F+ H, V0 {$ E7 {; v
  5. header ('Content-Type: image/png');
    ! y; @! a8 n9 |2 g# j% o% w, I9 ?
  6. // 1 创建画布
      W7 K' L& s0 A, ~: J
  7. $image=imagecreatetruecolor(100, 30);
    ) d1 Y9 y* v0 ^" }
  8. // 2 画一个矩形并填充背景颜色为白色
    7 R* C) y+ Z# B; x
  9. $color=imagecolorallocate($image, 255, 255, 255);. j7 E3 l& l9 E1 f; G) Q
  10. imagefill($image, 20, 20, $color);
    0 u3 Q& a" f+ f7 b+ _
  11. // for($i=0;$i<4;$i++){
    1 g& F9 _  N& @/ U) J) e( y* _
  12.     // $font=6;
    ( a3 x4 X. q) ?7 `$ p* p3 L
  13.     // $x=rand(5,10)+$i*100/4;
      T7 B, C, h* F& D
  14.     // $y=rand(8, 15);/ E% g" b) v) B6 h, w, w
  15.     // $string=rand(0, 9);7 i* i! F- Z" W& Z" L& E% u
  16.     // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));& b/ k3 L7 C) u) ~
  17.     // imagestring($image, $font, $x, $y, $string, $color);
    - k7 y4 {  x& d9 y0 j1 F$ P" g/ f
  18. // }3 X& |& I6 M. j2 D- P" c
  19. $code='';7 H* v5 o! l, a( L! q" @4 L
  20. for($i=0;$i<4;$i++){3 F3 S" G7 P. J( ~
  21.     $fontSize=8;; Y5 V  [( `+ @( q, r9 B
  22.     $x=rand(5,10)+$i*100/4;
    , w  a! T  e9 S
  23.     $y=rand(5, 15);) h. \- [9 D* G% B/ W% A
  24.     $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';" Q+ B; @4 D# t
  25.     $string=substr($data,rand(0, strlen($data)),1);
    % v+ a9 ~  M! R" h0 m
  26.     $code.=$string;
    9 T% r9 q' l7 l* n# X9 U
  27.         // 3.2 加载自定义字体
    ! ?% ^1 \+ j, g! c) u
  28.         $font_file = '1.ttf';" {7 O* T" l% s  ]3 s
  29.     $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));7 l% B  L5 x1 B2 ]$ C, }3 x# j
  30.         // 把文字填充到画布" P- o* E8 V( z3 a- z7 c, A
  31.         //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);( Q' m+ l) k$ H
  32.     imagestring($image, $fontSize, $x, $y, $string, $color);- i2 j" b& V1 Q) O2 D
  33. }
    0 G; c  P2 @, [% Q/ ]1 o
  34. $_SESSION['code']=$code;//存储在session里! B1 c7 p4 W* O3 p1 \! X
  35. for($i=0;$i<200;$i++){5 `% M$ y' p7 _" h/ E
  36.     $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));
    - U" T$ m# y# l# o3 E' \
  37.     imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);
    1 D8 y5 T4 o/ l# Z9 C) |
  38. }
    8 J1 I& [5 c2 u  V
  39. for($i=0;$i<2;$i++){
    % T) S4 j1 L6 B: v
  40.     $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));7 o* v/ J  z% O* r  ^+ K
  41.     imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);; X, o. k4 c6 D- ?% N: Q
  42. }5 f- B% X. [) s6 C- ^+ _' W& q; A
  43. imagepng($image);
    1 U; r' P% Q6 k) o( S1 e0 }2 g
  44. imagedestroy($image);4 ?# @4 `$ _* |6 J

  45. ' ?7 x1 [8 n; K5 k3 t% [3 [
  46. ?>
复制代码

' m7 W- }% g# J1 rform.php# u! u# p9 y" o/ @1 _- r' P
  1. <?php
    % d* V4 W& z9 T2 Z2 [4 {9 l  j" O. M
  2.     if(isset($_REQUEST['code'])){
    # T( z# t: f2 j1 S1 Z4 T6 U
  3.          session_start();
    & P$ n5 W& i8 {* v' ]/ t9 U
  4.         if($_REQUEST['code']==$_SESSION['code']){
    6 T% Z6 a) r$ x4 Y" q) i- w
  5.             echo "<font color='red'>输入正确</font>";; Y% h' q; x, P- M/ [1 T1 e
  6.         }else{
    + }- K# d- ~" d) G5 ~9 U: L
  7.             echo "<font color='red'>输入错误</font>";
    9 }+ r5 j( o7 ?* A
  8.         }( N/ c( N$ v5 C, a  G
  9.     }. ?1 V' w1 c6 w- o8 j1 a' i8 ]
  10. ?>
    9 S5 V! ?( B+ g/ o, H8 U" k/ ^7 N
  11. <!DOCTYPE html>0 `; d4 Y' [% c) m
  12. <html>) A  K6 M! o. w9 B- q. U
  13.     <head>
    7 n, R; C: o( C) D) A
  14.         <meta chartset="UTF-8" />, x2 G! W5 |3 V& ]
  15.         <style type="text/css" rel="stylesheet">
    ' H7 H. m! ]/ ?
  16.             a{. w$ H2 b$ I  x( U
  17.                 text-decoration:none;
    ) i, Q! y# b9 V0 Z6 a
  18.                 font-size:30px;
    8 o# a2 D- Y/ J& Q! r
  19.                 color:blue;
    % M4 b# R8 m  G. z
  20.             }; j& f" {+ A* B8 P
  21.             a:hover{
      K9 {6 C  w' f" J- ^
  22.                 text-decoration:underline;
    ) o3 S: y) N8 u
  23.             }
    * y; G: z7 N* W0 k" \9 o# C
  24.         </style>4 n3 H/ S, G0 b( }0 Q9 H, p
  25.     </head>, c! @! b6 s$ X; [% b4 P, f
  26.     <body>1 `$ ]& |2 K- n( F7 t. {
  27.         <form action="form.php" method="get">0 W3 C# E9 U! C
  28.             验证码:  <img id="img" src="index.php?" onclick="changeCode()"/>
    : I7 W3 \5 _# o. w. y
  29.             <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />) b. D/ J" }$ T, |
  30.             请输入验证码: <input name="code" /><br />
    3 e7 n& P! [! m1 Y
  31.             <input type="submit" value="提交" />) I; e0 J6 L* B3 Y
  32.         </form>' p+ x: h( K& J5 t! Y' A  H
  33.     </body>
    ( p/ g0 S& ]. A& A9 B( F4 L
  34.     <script type="text/javascript">
    ; c3 X" `0 C+ l) R
  35.         
    4 m4 d4 V; B: e! N- G* r8 h( t
  36.         function changeCode(){4 f! L8 m3 t9 j: M
  37.             var img=document.getElementById('img');
    - l2 s# l' ]! Q# `' H- d
  38.             //img.src='index.php?r='+Math.random();6 t4 k& @( q* M9 M& C% e& J
  39.             img.setAttribute('src','index.php?r='+Math.random());3 `% x" m  }' Z. J( }2 z, g9 h
  40.         }: h% M" k8 r- x0 x( Z  ^4 q# k
  41.     </script>2 z7 _+ f9 f& I6 J
  42. </html>
复制代码
$ X$ g9 K! ?# P

& ~6 u0 P" G3 ^) `/ f) p" w
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-5-2 18:27 , Processed in 0.073197 second(s), 20 queries .

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