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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[php学习资料] 验证码

[复制链接]
跳转到指定楼层
楼主
发表于 2018-12-16 01:00:13 | 只看该作者 回帖奖励 |正序浏览 |阅读模式
index.php( j$ g7 |2 A! x% _: B! U8 Y7 o
  1. <?php! v  g/ l% Q+ v6 I. x
  2. session_start();1 z; X6 B4 y8 U4 f
  3. // 丢弃输出缓冲区的内容 **1 m  r) [8 F4 t& F+ P* [
  4. ob_clean();. v6 g. v0 e9 e# J2 R9 m7 d- ~" f
  5. header ('Content-Type: image/png');
    9 M! u9 p7 g6 X. n
  6. // 1 创建画布3 e9 g. a9 q/ }  w9 e3 }+ f1 g! J' I
  7. $image=imagecreatetruecolor(100, 30);
    4 K8 p# t# r- s5 d" X& R
  8. // 2 画一个矩形并填充背景颜色为白色( M# X/ C' S4 {* d2 X% d$ \
  9. $color=imagecolorallocate($image, 255, 255, 255);
    6 X! F, M; n1 \& U9 i
  10. imagefill($image, 20, 20, $color);9 a# ]  K5 y' D& L
  11. // for($i=0;$i<4;$i++){# z! k7 E( Y, q8 A9 H
  12.     // $font=6;
    9 s5 ]7 ]! }) d0 S, l" d7 v
  13.     // $x=rand(5,10)+$i*100/4;1 p+ l7 x- h1 I; y4 V; ~& Q. P
  14.     // $y=rand(8, 15);
    : A  M. z$ e1 L
  15.     // $string=rand(0, 9);
    ! j1 w* ]# b- t4 _+ R* S3 V* y' y$ M$ ^
  16.     // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));$ _6 n, q$ [; Z  U
  17.     // imagestring($image, $font, $x, $y, $string, $color);
    7 o: x9 b- c& m: j) N4 p
  18. // }  j7 B& e2 f. ?) J
  19. $code='';
    % C  V$ L$ i2 q5 a
  20. for($i=0;$i<4;$i++){* ]$ e3 a) K7 W
  21.     $fontSize=8;6 Q. y. b3 D9 }3 a/ i; G4 `- d
  22.     $x=rand(5,10)+$i*100/4;) X5 C$ b2 F- X# t) I
  23.     $y=rand(5, 15);! [8 j1 U, k" }% H9 P# o& Z
  24.     $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';. t' q; D9 y: Q  ^  ~
  25.     $string=substr($data,rand(0, strlen($data)),1);
    ! I" B* K$ `4 |0 n2 d
  26.     $code.=$string;
    + {4 B2 E  ]7 t# C8 H  R" B' u
  27.         // 3.2 加载自定义字体
    & k" A: v0 s& c  F
  28.         $font_file = '1.ttf';, z5 R. h& J6 p  F
  29.     $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));, K, N2 L( E' M- h/ g4 s+ ~! M$ C& B
  30.         // 把文字填充到画布) K& A, H; u& u+ V0 Z- V
  31.         //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);
    : r( S- l4 |0 P& l" x
  32.     imagestring($image, $fontSize, $x, $y, $string, $color);% n; O( g, \5 W; L" y3 |
  33. }
    - v* [$ H) H, p; ~
  34. $_SESSION['code']=$code;//存储在session里
    1 V' I7 v5 k/ e5 y# x$ W
  35. for($i=0;$i<200;$i++){# C/ |" V  F  O" }$ J; z' J
  36.     $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));7 {7 B: }# C/ O* V
  37.     imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);! `# ~- b) e4 W* A8 V2 ~% H
  38. }
    : b9 Y; `/ e4 [+ j% C, d
  39. for($i=0;$i<2;$i++){- ~! v% Y0 ?9 V
  40.     $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));
    2 P' Z; _4 K1 T8 d9 J
  41.     imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);4 q% a! t7 }* e7 l6 K7 L
  42. }+ G: [0 u8 \: m* M: U
  43. imagepng($image);
    0 s. a7 t% b6 e2 K* N2 J
  44. imagedestroy($image);* v% s9 `# Q  n; Y

  45. & p8 q  K; H& o
  46. ?>
复制代码

1 ~3 _4 W$ h. n' |/ ^( ]5 Y4 qform.php
5 G1 `! {" p8 k; q
  1. <?php# ?8 x) X) n, r6 w3 S
  2.     if(isset($_REQUEST['code'])){
    . d" p& P- M( R9 Q" b* G9 w
  3.          session_start();
    " B1 w" l+ K+ r0 v. q7 m+ P* G  T" ]
  4.         if($_REQUEST['code']==$_SESSION['code']){5 r, c& R! _- u$ ]4 r8 ~
  5.             echo "<font color='red'>输入正确</font>";
    / Q) K" ]0 b8 B) N  e, ]3 a3 n
  6.         }else{8 s- P6 ~% A* y: O1 H
  7.             echo "<font color='red'>输入错误</font>";
    2 ]0 C7 `; {/ Z2 t" y8 C
  8.         }
      y; S  l7 U$ s: L& W- q
  9.     }
    ( y  o3 u* |2 s* T
  10. ?>  t- K( o/ }6 d( p! k
  11. <!DOCTYPE html>
    4 C0 n0 u6 T0 g. c$ y
  12. <html>
    % L; R* ?* k, C8 x
  13.     <head>$ J4 X' m  ~0 x1 H9 B# c2 }
  14.         <meta chartset="UTF-8" />
    + M' v9 p2 ]1 N4 R- A2 C. ^
  15.         <style type="text/css" rel="stylesheet">
    ) P" J! e% N" P0 c) S3 V7 F, w
  16.             a{
    * x$ J- ]6 D# V* b- T. {
  17.                 text-decoration:none;: z" o2 d( ?- |9 B: b/ C
  18.                 font-size:30px;
    . p' K5 `# ^$ n
  19.                 color:blue;) N+ f+ M" n" a% T2 I
  20.             }" D6 p: R8 f8 i9 o* S" U
  21.             a:hover{
    7 M, z) K+ b) Q7 `" E6 q8 b
  22.                 text-decoration:underline;/ L. f- ]0 U  d: y" d6 [
  23.             }
    6 n" L5 y( B8 W* P; }& {: Z7 F; X
  24.         </style>
    5 H8 t# D6 F7 D2 t) O' @
  25.     </head>
    & i2 e, l/ y; D" R
  26.     <body>
    " P& H$ j# F7 |" l4 w. w, l
  27.         <form action="form.php" method="get">
    ! p- N7 @1 d4 w4 O
  28.             验证码:  <img id="img" src="index.php?" onclick="changeCode()"/>' q: ~' t0 r- j0 c- I/ F
  29.             <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />
    " d8 b" n% n% s. X
  30.             请输入验证码: <input name="code" /><br />1 M% H+ M. M- A$ G9 e* ~) Y% I, C
  31.             <input type="submit" value="提交" />
    " B9 t0 m2 s7 r! d* G
  32.         </form>' [. w! w7 s: _* ~
  33.     </body>' \; s. [: W- s+ n2 X$ |+ `* h& X
  34.     <script type="text/javascript">* A2 U$ @+ o* ~  O- v) q
  35.         
    & v+ k! u2 q. Y9 w9 c6 K
  36.         function changeCode(){% h, x: c- \7 e6 R8 l  Y  Y; _
  37.             var img=document.getElementById('img');7 ^+ c: s/ @9 d5 J% f, H& _. Q5 \+ q
  38.             //img.src='index.php?r='+Math.random();
    / d" m2 j6 z- k2 P& Q/ \6 ?- m* ]
  39.             img.setAttribute('src','index.php?r='+Math.random());
    # y; r; C+ U* ^4 Y) g* T* s7 v( M
  40.         }( m; p: b: k- H" v
  41.     </script>
    # Q  G: r/ k  x, Z- F
  42. </html>
复制代码

- s& ]* _& C  i; u) D) K% c  D7 ]0 u1 E9 z* M' J- l" W
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-1-30 16:00 , Processed in 0.055123 second(s), 20 queries .

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