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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[php学习资料] 验证码

[复制链接]
跳转到指定楼层
楼主
发表于 2018-12-16 01:00:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
index.php
9 Q) z8 b4 c4 ]: w# q+ V9 J1 f
  1. <?php
    2 E$ H8 [% l( F  i$ c+ V# ~) J# {
  2. session_start();) T8 H( K3 y% _: N1 }7 s" K
  3. // 丢弃输出缓冲区的内容 **
    ( t9 m7 a7 {: \+ W' u
  4. ob_clean();. t4 }( ?; G  \, D* \; s" H
  5. header ('Content-Type: image/png');* J  w. N& [3 t. B
  6. // 1 创建画布; X- n, z7 O5 |8 N
  7. $image=imagecreatetruecolor(100, 30);9 }& V. o  z' U+ n7 ~% m
  8. // 2 画一个矩形并填充背景颜色为白色
    $ D1 s* b$ @, D$ n
  9. $color=imagecolorallocate($image, 255, 255, 255);
    " M/ b4 I# M- m) o7 B5 o/ _
  10. imagefill($image, 20, 20, $color);2 ~" y* K/ B" m2 [1 d3 a' Y
  11. // for($i=0;$i<4;$i++){
    2 s4 H( V) v/ H" `7 V1 D
  12.     // $font=6;0 W8 v+ Q2 L; O: B" e
  13.     // $x=rand(5,10)+$i*100/4;
    * p& \( C4 }5 N7 O* C
  14.     // $y=rand(8, 15);& v5 m, }- W6 E3 Y4 e) y/ I6 E
  15.     // $string=rand(0, 9);2 U8 v$ u+ x; T4 f5 B* X. j
  16.     // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));, ^" h5 U7 r% E
  17.     // imagestring($image, $font, $x, $y, $string, $color);5 P2 V% w4 Y( L: t. A+ Y) g
  18. // }0 r9 j2 Z" D, x* B* K, z' x
  19. $code='';" a% j0 Q# _0 ]2 D
  20. for($i=0;$i<4;$i++){
    2 C$ ~$ e6 K- m9 Q5 G! k
  21.     $fontSize=8;
    # y# ^4 x, l9 Q5 g
  22.     $x=rand(5,10)+$i*100/4;
    2 z, T9 o9 J5 m1 b' j: J1 j% }* T
  23.     $y=rand(5, 15);4 c- q7 x- l! s( N
  24.     $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
    2 c* ^% P1 Z' k5 i& M  U7 ?8 l
  25.     $string=substr($data,rand(0, strlen($data)),1);' }% K6 V6 s) c9 S/ Y2 u
  26.     $code.=$string;
    5 q, b+ i1 g! U: o: ?& U2 u
  27.         // 3.2 加载自定义字体
    - U, M/ {: J2 {! s" h
  28.         $font_file = '1.ttf';  V" Q7 h5 J$ ?# ]
  29.     $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));
    + a6 k+ Q" A; W4 Y& I) X8 E! ~
  30.         // 把文字填充到画布
    9 d- u# H5 R3 O4 a% c0 y, z
  31.         //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);
    / |' c1 O9 Y) D) A9 ~4 ?3 T
  32.     imagestring($image, $fontSize, $x, $y, $string, $color);: g- k" M, P$ ]8 a* G6 V* S; j
  33. }1 y; i1 ~0 I+ C2 W( ~
  34. $_SESSION['code']=$code;//存储在session里( b& k1 |, f  ~- {
  35. for($i=0;$i<200;$i++){* E6 X; c  x) b
  36.     $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));
    ' N* r% a- S8 F% x" x0 V
  37.     imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);
    . z9 S3 ?( f; ^( k
  38. }0 ?- g4 Q" i6 P1 [
  39. for($i=0;$i<2;$i++){6 f- ~& f3 d0 y4 t; A
  40.     $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));
    + M6 ^' M+ R. P  D4 Y  y' Q
  41.     imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);# v- d1 Q! u2 m8 s! ?
  42. }: u# r" A  y. [2 ?
  43. imagepng($image);
    : ~& r' _: U+ e  E4 Q
  44. imagedestroy($image);" l2 j1 j3 [0 f

  45. ) Q8 ~1 y9 a( v) C# u! g1 |) S( x) u
  46. ?>
复制代码
. K4 `# ~  s+ v1 J! L
form.php
6 c5 K8 D4 F5 G: ~
  1. <?php
    ! x' ?5 b, a( l4 f' a8 h  E
  2.     if(isset($_REQUEST['code'])){
      S- z. P' s% T
  3.          session_start();
    , q/ D3 m7 K3 u1 G6 H
  4.         if($_REQUEST['code']==$_SESSION['code']){- ?5 Y5 N3 v9 |, D! I+ v1 t
  5.             echo "<font color='red'>输入正确</font>";
    2 T$ Z* E: b8 }7 C
  6.         }else{
    3 _. g2 p6 Z+ t- _
  7.             echo "<font color='red'>输入错误</font>";. a0 n0 u1 H5 f# P/ I8 d% S6 l! M
  8.         }
    2 V* K" {( [6 f: m6 c
  9.     }) |) t+ T+ X1 c- a
  10. ?>6 E- Q/ S* H; H" l# n, G6 ?
  11. <!DOCTYPE html>  R& g" d' _- o& {$ Y" k
  12. <html>
    - E8 e8 `& C9 V
  13.     <head>
    $ U5 X) _6 Q# u( P  Z
  14.         <meta chartset="UTF-8" />
    , H; K5 t+ |6 ]1 H; X
  15.         <style type="text/css" rel="stylesheet">
    % K8 @. ]! p3 I2 b) X* p, K
  16.             a{0 f* w( q: r1 B. b2 m
  17.                 text-decoration:none;
    ' ^0 `$ q- W' c  A  G1 n
  18.                 font-size:30px;. Q. I0 `" D0 D, O) {
  19.                 color:blue;; {7 c* k# V& ~. r2 i2 @5 \5 W
  20.             }0 w" z6 T# W0 k+ ~3 }0 f( [
  21.             a:hover{
    4 }, }$ ]# b2 `5 H
  22.                 text-decoration:underline;1 ?0 Z3 m# M; U2 p* N
  23.             }- G/ M' v: C* R& i: s
  24.         </style>
    ; y8 V6 v- H# b6 ~7 q
  25.     </head>8 c3 S  L! U7 g. R0 w/ M$ [
  26.     <body>
    ! }2 F6 W9 Y% i/ _
  27.         <form action="form.php" method="get">
    : n$ J) x7 }% [' I
  28.             验证码:  <img id="img" src="index.php?" onclick="changeCode()"/>7 W2 v7 r2 e( @4 ~8 B
  29.             <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />2 I' S- n1 Q/ k; d% g; l
  30.             请输入验证码: <input name="code" /><br />
    8 z6 c0 K! Q  k6 X) h) d- b
  31.             <input type="submit" value="提交" />7 ]( l( p+ v* x' W6 G
  32.         </form>
    ! H/ W. ~2 z% y
  33.     </body>, ~0 G. o3 c' @2 S+ J# k9 o, [6 |* z  m
  34.     <script type="text/javascript">
    , n- C9 A' o3 \* H+ R& t3 i. E
  35.         9 g+ g' K8 Y" }0 ^/ k
  36.         function changeCode(){
    / H' G9 g$ n1 C, F1 m  B  p) z
  37.             var img=document.getElementById('img');( {2 I0 ?- p% p  ~
  38.             //img.src='index.php?r='+Math.random();$ c, n9 ^& s& I8 ]0 S+ e" _
  39.             img.setAttribute('src','index.php?r='+Math.random());" o- P& K% d5 g. v- R+ ^
  40.         }2 A4 j4 h( Q2 E7 e
  41.     </script>
    " M7 p1 \' Y1 e! w
  42. </html>
复制代码
' o) E0 _6 t7 C" A
" n9 {9 J/ ^' w- j( J8 x
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-6-19 22:40 , Processed in 0.052441 second(s), 19 queries .

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