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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[php学习资料] 验证码

[复制链接]
跳转到指定楼层
楼主
发表于 2018-12-16 01:00:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
index.php" |( @  K5 H, b" @5 t
  1. <?php. W7 Q. W; l7 J* W; k9 }
  2. session_start();" H  h( @$ x. h3 Z
  3. // 丢弃输出缓冲区的内容 **
    % x8 i( q" d0 E4 Y" }7 X/ K* x
  4. ob_clean();& ~. G  B4 o4 ?+ `! J
  5. header ('Content-Type: image/png');5 B" E; H0 P5 G$ \3 u7 a' ^" n2 g
  6. // 1 创建画布
    $ \5 ~  v* n0 ~& J( R7 c7 V
  7. $image=imagecreatetruecolor(100, 30);
    $ I  g, l% A/ y. Q6 W3 h/ C* ^4 F
  8. // 2 画一个矩形并填充背景颜色为白色
    ' R% B- ^# [1 r3 U1 r6 @
  9. $color=imagecolorallocate($image, 255, 255, 255);
    . Q! @) X/ S! |* J9 X
  10. imagefill($image, 20, 20, $color);5 R% `2 {7 Z0 n6 g3 Q3 r
  11. // for($i=0;$i<4;$i++){
    - g2 p5 F" |. Q
  12.     // $font=6;- M' l  l2 L3 \7 |
  13.     // $x=rand(5,10)+$i*100/4;3 g- F" |& ~) P; o' D3 W
  14.     // $y=rand(8, 15);
    1 ^  ?8 p6 S% O; p& d
  15.     // $string=rand(0, 9);9 C* r' [8 V2 E9 ^. r
  16.     // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));2 Z  j% l0 @3 S* s3 _" w
  17.     // imagestring($image, $font, $x, $y, $string, $color);9 }; t# G( h  Z! J" w3 Y% W
  18. // }7 h+ G- P1 k9 S" {, C* q6 s: `
  19. $code='';
    : D& H' W8 w6 [8 M! }* C
  20. for($i=0;$i<4;$i++){
    0 K5 z+ `% x. q1 z1 |+ z* O8 ^  i
  21.     $fontSize=8;) S0 H( x; s/ L6 S; k  c( r
  22.     $x=rand(5,10)+$i*100/4;
    3 A8 ]' |. t) V$ X7 y+ n+ ?; S4 f
  23.     $y=rand(5, 15);
    , m( j# b$ x: m" N8 G
  24.     $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
    : ?: X4 Z( t. c. D. }
  25.     $string=substr($data,rand(0, strlen($data)),1);) @/ M7 P) q/ T; x% o
  26.     $code.=$string;" l3 M- [; l& y4 t$ l: |
  27.         // 3.2 加载自定义字体
    5 k. J. Y: P$ S8 N( n# C& j
  28.         $font_file = '1.ttf';( {& ~5 G! B% d% K5 ^
  29.     $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));
    " u, M' X' l3 o- Z) o2 r
  30.         // 把文字填充到画布
    # V, o) g% s; X! ?- L# C$ h5 s
  31.         //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);4 G7 {9 K9 h* N0 e) E8 i5 p. _
  32.     imagestring($image, $fontSize, $x, $y, $string, $color);
    * V, l1 n, _) @, |$ w
  33. }
    " U: D! [% I* Y/ S7 ?1 P
  34. $_SESSION['code']=$code;//存储在session里5 N) g  K6 i+ Y
  35. for($i=0;$i<200;$i++){% I$ o- D1 Q7 E
  36.     $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));
    - G3 n0 j) [0 J6 q2 U
  37.     imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);
    , _+ C1 G, S1 H9 T. x
  38. }3 d8 o* D  d9 }$ i( m. a7 [
  39. for($i=0;$i<2;$i++){% g# F& }, M1 O, y0 @# k: ]
  40.     $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));
    7 C, {; \" v  a' x- Y: N
  41.     imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);
    ) k- [: Y. K, T8 m7 d
  42. }
    5 Y9 r8 V. n( f+ [% o1 x9 ^  o
  43. imagepng($image);2 r& A( F9 @& ]4 R- I
  44. imagedestroy($image);# ?0 N) c7 A0 M
  45. 9 Y4 I+ ], L6 U+ d6 B9 K# u
  46. ?>
复制代码
3 K8 N  M; y4 G3 i
form.php
3 R: t4 F. d# y6 z6 P
  1. <?php2 s7 c7 e: q. d$ {
  2.     if(isset($_REQUEST['code'])){
    / k( w  u2 f6 O  Y' l2 u/ T' G
  3.          session_start();
    & x/ G3 Q- z) h8 ]  H+ e
  4.         if($_REQUEST['code']==$_SESSION['code']){4 w5 S* ]4 ~3 _* t, S  i8 h
  5.             echo "<font color='red'>输入正确</font>";, t6 x& ^6 d: Y- e3 P7 o
  6.         }else{
    4 g0 g4 v3 o4 w$ I$ }3 ^
  7.             echo "<font color='red'>输入错误</font>";
    . @4 ]; s# j+ n# S1 i# v
  8.         }
    # K/ ]* ?4 X1 b& [
  9.     }- e! U4 M& a4 K2 K+ Q# Z
  10. ?>% D; L+ N! [9 Q) f$ n6 O
  11. <!DOCTYPE html>0 w( k3 ^+ j" o7 p- W4 H3 J8 m
  12. <html>* ^. K* Q) Z- |, |9 |( [7 w
  13.     <head>
    9 ?% K! W7 t8 I* O6 c9 i
  14.         <meta chartset="UTF-8" />
    . d- u1 I3 I' t9 ~. f0 a" f8 }, j
  15.         <style type="text/css" rel="stylesheet">
    * z/ T8 D9 p) c+ [9 Y) T' m* m
  16.             a{- j( ?; R  r$ I# \, W5 V
  17.                 text-decoration:none;
    6 V! e$ H# K7 G5 ]  P" T) E- Q. W
  18.                 font-size:30px;5 H! O! P6 Z# X
  19.                 color:blue;
    5 \5 h! k/ ~& I: I0 ]7 ]7 G
  20.             }8 W( a! ?1 @9 z3 R/ m, l
  21.             a:hover{
    8 d( m5 G9 Z$ s# B. b% k
  22.                 text-decoration:underline;. d7 X' _+ ^1 x
  23.             }
    " ?6 S- l6 y/ H: f4 ~7 y6 N: Y" F
  24.         </style>& W9 F1 s/ M5 |; Q
  25.     </head>
    0 Q# s! z% O) y, T! g/ |; B
  26.     <body>
    ) p, h+ `+ ^. w8 p
  27.         <form action="form.php" method="get">
    " n' z9 K1 q* v3 v
  28.             验证码:  <img id="img" src="index.php?" onclick="changeCode()"/>
    5 \3 P* m6 w( p
  29.             <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />
      g7 M' D3 D9 ^* E% H$ |
  30.             请输入验证码: <input name="code" /><br />
    6 X  A6 k6 T1 e1 b+ Q# H- Y* r
  31.             <input type="submit" value="提交" /># D: M7 H) w* D4 [4 ^$ L
  32.         </form>8 J' |4 u+ ?! ^- {3 l$ Q$ e9 D
  33.     </body>
    * I6 f# y/ W0 |' q
  34.     <script type="text/javascript">
    * q6 o& g5 S& B* ?
  35.         / H# x" q1 e5 l4 l, N' [
  36.         function changeCode(){
    . Z0 C5 z7 ^: _+ u' F
  37.             var img=document.getElementById('img');
    " m. v& i/ x8 @8 p4 d% p- B
  38.             //img.src='index.php?r='+Math.random();
    8 U2 K7 X; P4 }) _; @- j6 e) q
  39.             img.setAttribute('src','index.php?r='+Math.random());
    9 f2 e7 V" I0 y: ~" P; k! f
  40.         }' P5 o* R( a! n) t4 Q& z& Y: Q
  41.     </script>1 D& H3 I1 M7 v6 e- L
  42. </html>
复制代码
5 V6 P4 b4 M1 A1 n& ^3 C; v
, C3 n5 J; M$ C# I% I2 F0 {  b1 ]( Y
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2024-5-9 02:29 , Processed in 0.109578 second(s), 19 queries .

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