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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[php学习资料] 验证码

[复制链接]
跳转到指定楼层
楼主
发表于 2018-12-16 01:00:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
index.php, Z# ]3 s5 n% `. q& [6 n  T
  1. <?php1 w! P( j- z4 @2 _3 P
  2. session_start();0 v6 \1 U7 ?! }/ J9 {7 V& b# }
  3. // 丢弃输出缓冲区的内容 **
      j9 K+ @" V) O5 K
  4. ob_clean();& a. _# Z/ z1 ]# ^! t2 U) Z- y0 P
  5. header ('Content-Type: image/png');
    9 v' f) r7 _  p/ I: t% [' K
  6. // 1 创建画布, r5 w7 ^6 a. v/ v
  7. $image=imagecreatetruecolor(100, 30);
    : M% A, z1 l2 {8 P! l- M. i
  8. // 2 画一个矩形并填充背景颜色为白色
    . [7 ^- M3 p( t/ W( K  u
  9. $color=imagecolorallocate($image, 255, 255, 255);; A$ s9 n# _2 \5 }$ g$ {. B
  10. imagefill($image, 20, 20, $color);
    4 K5 Y+ C3 S! s* Y" m
  11. // for($i=0;$i<4;$i++){/ u9 f# Z/ P; Q
  12.     // $font=6;. H, [: ]+ E8 E! H$ t3 y
  13.     // $x=rand(5,10)+$i*100/4;7 g. }! W: N# l
  14.     // $y=rand(8, 15);2 n2 |  Y' i2 q# @' Q
  15.     // $string=rand(0, 9);* m& d* A. H6 i* e' C4 N
  16.     // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));; V; B/ _- [# W3 l+ @1 x) v
  17.     // imagestring($image, $font, $x, $y, $string, $color);
    / A6 h  Q6 C4 v; y/ }; n7 R- z
  18. // }" X$ o9 U' s3 m& C2 `& X  f# q
  19. $code='';% U2 N2 v+ G, m  B3 f, K0 e3 V
  20. for($i=0;$i<4;$i++){/ q  C2 `& V! i1 a- c* N* A
  21.     $fontSize=8;
    ; i2 R2 h; o- J0 a- c
  22.     $x=rand(5,10)+$i*100/4;
    5 A7 }9 c$ P1 o! k4 S# G
  23.     $y=rand(5, 15);
    # A; W7 B9 k$ R" j7 j
  24.     $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
    % X: j% t3 B& r1 d' ~5 P
  25.     $string=substr($data,rand(0, strlen($data)),1);
    0 P+ U! o( L" ?; J$ g
  26.     $code.=$string;. r9 d  P3 l+ }
  27.         // 3.2 加载自定义字体& A' o$ `' U' t% `
  28.         $font_file = '1.ttf';
    # t1 K- z( T6 t9 S
  29.     $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));
    % ^! H4 L8 X3 Y, o5 V1 Q5 g
  30.         // 把文字填充到画布
    0 i) u2 z* s2 M( R# h5 C
  31.         //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);$ ~$ ]6 f5 C+ C0 J
  32.     imagestring($image, $fontSize, $x, $y, $string, $color);
    # c" s3 i0 I  `
  33. }
    $ r/ m! ]/ w. \& f! D2 X2 j
  34. $_SESSION['code']=$code;//存储在session里4 Y) |; E. k8 @( N- ^5 D
  35. for($i=0;$i<200;$i++){& m: Y8 V: ]1 \. K. w% r/ U
  36.     $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));8 y# N0 \* N) h
  37.     imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);+ d$ o7 B$ o4 x7 {* t' P) c
  38. }' p0 n+ A. l! b6 Y
  39. for($i=0;$i<2;$i++){# ]6 F7 D/ X6 t* Y
  40.     $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));
    ' `( P2 `) W. g
  41.     imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);1 E' M6 f  D; K
  42. }
    5 g$ x2 Y6 g. @; i6 o' Q
  43. imagepng($image);
    - u0 D4 k6 F5 k& F+ N
  44. imagedestroy($image);$ z$ A1 _& R* K4 i& g

  45. - G2 |/ b( }9 t
  46. ?>
复制代码

. U" x2 s/ {! L2 s' F$ I& {form.php
* e2 ~7 ^8 U" k0 @9 Z0 I1 [
  1. <?php
    " J/ t  e7 [& G* P( ?% R/ s4 w
  2.     if(isset($_REQUEST['code'])){
    / t/ N/ b3 z0 e# ^$ r
  3.          session_start();3 v. y, `' A( s7 h6 d- C
  4.         if($_REQUEST['code']==$_SESSION['code']){5 C; J7 C) l) n9 E; B
  5.             echo "<font color='red'>输入正确</font>";' g0 O% j: Y+ [$ c1 l$ T+ \; o
  6.         }else{5 R* I# E* e0 R) y7 J# c6 V
  7.             echo "<font color='red'>输入错误</font>";8 l8 E* E# v( c3 d, j' f
  8.         }
    6 H% z* e- v% y+ Y3 J$ n
  9.     }
    5 o! D* n& F* I# @3 y% L
  10. ?>
    ' s4 k$ Y, M# N
  11. <!DOCTYPE html>
    ! H# U( `! X$ d7 [7 L. ^! ^, c
  12. <html>
    5 [) e8 ]- m! k) D7 Q8 H
  13.     <head>7 Q( h# }+ |2 d
  14.         <meta chartset="UTF-8" />5 X6 u* V) l) j0 o5 I& m# ^, w
  15.         <style type="text/css" rel="stylesheet">
    + g9 y& o1 r& q8 d$ @
  16.             a{
    : K$ g' i$ T' l0 q" H5 R8 e
  17.                 text-decoration:none;! L5 ?: ]/ R; e- D0 @; V
  18.                 font-size:30px;  P- z3 K6 g+ |0 I4 c' ^
  19.                 color:blue;" I( O: g" M8 q; T/ H" j5 h
  20.             }  n+ }9 h5 ~0 W' `3 W1 ]
  21.             a:hover{
    ) A* p3 h# l: |. w5 J! M
  22.                 text-decoration:underline;
    ' a% {* G1 |/ a+ F# Y8 M
  23.             }
    & a2 l6 T/ o$ F' D9 h! \4 {# Z
  24.         </style>7 o1 I- p. _! D* m  w. T8 [9 Y
  25.     </head>! b: M/ e) |" d, P! S1 I
  26.     <body>
    - O% }. N  X9 R+ z( D4 J
  27.         <form action="form.php" method="get">
    ' X4 ~" D- U" n* _- i
  28.             验证码:  <img id="img" src="index.php?" onclick="changeCode()"/>
    * h1 s4 e' G1 l8 p8 @% |& b5 |
  29.             <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />
    , U- ?! V* Y; w# W$ s. q& ]2 y
  30.             请输入验证码: <input name="code" /><br />' [5 c( p: O3 d# m- x
  31.             <input type="submit" value="提交" />" G7 i$ Z3 ~1 \" L+ f2 M4 a
  32.         </form>) P- `* t5 K$ ~
  33.     </body># e1 Y& |0 L  m: T2 b% h
  34.     <script type="text/javascript">
    ! G8 J( P0 r! H% F. t8 n! }+ ]8 @) H9 {
  35.         
    6 z# y: Y( N9 w  K, S! |
  36.         function changeCode(){9 g: t+ O/ b9 a6 @5 y$ ]/ H) o) m1 X7 U6 d
  37.             var img=document.getElementById('img');5 s  y5 q6 X  F* m
  38.             //img.src='index.php?r='+Math.random();
    ) ?, G2 p6 I2 M' A
  39.             img.setAttribute('src','index.php?r='+Math.random());
    . _/ p9 M4 t2 I+ Q
  40.         }) e/ B2 o" ^7 J% B1 x
  41.     </script>/ m' T( Y9 p0 l
  42. </html>
复制代码
: w7 Y: R* [9 E# e( k9 R
: D8 O" n1 k* m( K7 N: e: e! o! W
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-8-4 12:23 , Processed in 0.052725 second(s), 20 queries .

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