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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[php学习资料] 验证码

[复制链接]
跳转到指定楼层
楼主
发表于 2018-12-16 01:00:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
index.php# K0 D  h# \/ g4 R+ ^. J$ j- {9 z
  1. <?php1 v$ x* |$ c# W
  2. session_start();: C) S+ U3 U' D' V
  3. // 丢弃输出缓冲区的内容 **" G8 e7 a% x* P: S0 }
  4. ob_clean();
    ! `. \% G4 c4 u
  5. header ('Content-Type: image/png');
    ( q6 a3 U' ]! C- Y; X7 c1 r# i
  6. // 1 创建画布% N. \/ j; _; l
  7. $image=imagecreatetruecolor(100, 30);
    1 T) j  \* l! n+ j3 r7 f" A, ^
  8. // 2 画一个矩形并填充背景颜色为白色
    / j! Y* _+ M4 \
  9. $color=imagecolorallocate($image, 255, 255, 255);" e( m- z8 _8 z' u
  10. imagefill($image, 20, 20, $color);
    $ @) C8 p; q8 j! |4 x. \
  11. // for($i=0;$i<4;$i++){
    " F2 I( q: U* X% o+ g# \
  12.     // $font=6;
    ( F; ?# P( X2 h6 U3 v3 c
  13.     // $x=rand(5,10)+$i*100/4;& S9 t; G( U# ~$ R5 h& ^- j) K( j
  14.     // $y=rand(8, 15);
    * Y) `( I  A) ?: M5 {" s8 r
  15.     // $string=rand(0, 9);
    , j. f9 C4 M5 \$ S1 i
  16.     // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));
    " t- m$ U  B- y) ~. X9 E
  17.     // imagestring($image, $font, $x, $y, $string, $color);
    ' F" ~! X0 d8 W+ t; z6 [9 d6 K
  18. // }% ~! A+ A( u: f6 N8 V6 k
  19. $code='';$ j; \8 P" g8 W/ @2 H2 h
  20. for($i=0;$i<4;$i++){8 @# D; E, E# h: c
  21.     $fontSize=8;
    % a8 A0 u( Y# x% G( h3 K6 ^5 e. R
  22.     $x=rand(5,10)+$i*100/4;
    , U* x# w. E+ V( J8 h1 |. ~
  23.     $y=rand(5, 15);
    1 x" S, h  T% n2 N/ l5 ?
  24.     $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
    . n. `0 N9 E- T( E; p' i# Q
  25.     $string=substr($data,rand(0, strlen($data)),1);
    + S* x4 }$ d% S5 I3 ~3 |% o
  26.     $code.=$string;" z) T5 a+ f2 }; N* g  s
  27.         // 3.2 加载自定义字体
    9 z4 j+ {! k. ^( ]
  28.         $font_file = '1.ttf';
    : B( @8 E. f* N5 |; F0 q
  29.     $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));9 y8 ^) X6 Y1 d* |3 f$ I
  30.         // 把文字填充到画布0 h8 D; M) E& E# \' J. J
  31.         //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);- Q) |- ^1 ^% s" z, n4 Y- }
  32.     imagestring($image, $fontSize, $x, $y, $string, $color);# _5 @' ]+ {" w# O! B
  33. }
    8 `- n- |$ H% ]. T# s! `
  34. $_SESSION['code']=$code;//存储在session里
    ) I2 U' P; w* v* O3 M2 _& U
  35. for($i=0;$i<200;$i++){3 i7 E  ]0 X/ n+ @4 |" N2 ]
  36.     $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));3 d2 m# y( c7 c6 m
  37.     imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);9 R) G( {' N) h% }
  38. }' T- Y1 t5 L, O. r: r
  39. for($i=0;$i<2;$i++){
    : w% _8 W4 l6 U. c  I" N8 _
  40.     $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));  V% L4 T' |0 h* ~9 z$ m3 @; l
  41.     imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);
    . |5 s  T7 U) W+ U- O: ?6 W
  42. }
    3 n- @, ?3 B  ^. y+ Q
  43. imagepng($image);- B( x+ v1 s* O
  44. imagedestroy($image);! a% S/ h: T/ N1 E- e$ t

  45. 9 i* E0 k8 ^2 ^( }6 e- r
  46. ?>
复制代码

" N. [8 ]' z! A) z, o# J+ _form.php
9 G) C% Y4 K& q# _, w/ n! D
  1. <?php
    5 q+ Z) n. }  R" T, c. K7 q: a
  2.     if(isset($_REQUEST['code'])){( f" |2 w( H; N8 m3 Y6 i
  3.          session_start();
    5 k6 W2 l3 A9 y# E/ ~4 j* u
  4.         if($_REQUEST['code']==$_SESSION['code']){" K8 z" x$ @: W/ a  C" a, _
  5.             echo "<font color='red'>输入正确</font>";
      H2 d. a1 g( X" k
  6.         }else{
    ! F; n! [) W9 J7 P# S, T0 N
  7.             echo "<font color='red'>输入错误</font>";
    5 {) D$ i5 j  P3 U
  8.         }8 F- s- B/ B. ^0 d9 ~" P5 i5 B
  9.     }3 q' C! _$ s, d0 U4 g6 ^' B4 P
  10. ?>
    / J, L% k+ W: {6 V: U3 `
  11. <!DOCTYPE html>0 b! d6 Q0 I+ r, g9 ^' M
  12. <html>
    " p" q: s5 g7 K7 W! L9 c) K+ i
  13.     <head>5 T0 z1 _% u, _; J
  14.         <meta chartset="UTF-8" />( g8 w8 P0 M2 h5 i9 L
  15.         <style type="text/css" rel="stylesheet">
    8 O0 Z$ y- W+ N9 Q% S
  16.             a{
    ; S8 c* C9 f' c5 C' O8 }! D
  17.                 text-decoration:none;8 ?% k3 m: Y9 C- \
  18.                 font-size:30px;
    / K$ C- Q4 L0 c" i
  19.                 color:blue;
    ) S2 y2 Z! w- X, s" T7 a
  20.             }7 _( U' T8 g$ |. j  w1 \
  21.             a:hover{1 C- a! p$ G3 u1 j! k
  22.                 text-decoration:underline;- e9 ?& j5 S7 u& w2 B  f
  23.             }
    ' @' ?- {. y8 l0 a) Z& x
  24.         </style>7 m% S! E9 [& M0 g7 O0 o3 _1 l
  25.     </head>
    , X! z1 a! l7 c$ s
  26.     <body>8 \: o4 ]( _! l6 Q2 ?
  27.         <form action="form.php" method="get">
    2 w6 O8 B! M! \5 w5 S( q6 a+ U
  28.             验证码:  <img id="img" src="index.php?" onclick="changeCode()"/>
    ; U, {4 ]( `3 k% r' x
  29.             <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />$ O6 Q) R6 a- L
  30.             请输入验证码: <input name="code" /><br />' ~, I! I8 L' t/ @- G  n
  31.             <input type="submit" value="提交" />( V6 u# W$ l& A" K# T- v  g3 [
  32.         </form>4 N" _" Z" b! S6 T! J) J3 A
  33.     </body>6 H$ h1 }# M, L* u8 A3 E# b
  34.     <script type="text/javascript">
    2 i- Q9 ~& }: A% \
  35.         
    + b' S0 L1 A! \5 F. i
  36.         function changeCode(){
    , j6 T. h7 r$ D" [, N2 o' c
  37.             var img=document.getElementById('img');9 B3 U* @6 C4 ?5 W- H; W$ g: d' I
  38.             //img.src='index.php?r='+Math.random();
      n" \, x1 P0 p5 {. A* e
  39.             img.setAttribute('src','index.php?r='+Math.random());
      ^% I) [# Y( s. u6 w% f0 u
  40.         }
    ! Q: L$ l4 o0 w2 i
  41.     </script>/ X1 X/ k- {+ t5 k% F8 g4 W
  42. </html>
复制代码

3 m% N4 z/ g+ Y9 H4 o! a/ ?# j4 y5 \" y1 i1 B! Q0 G/ b" g9 U9 V8 N
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-3-16 18:46 , Processed in 0.053199 second(s), 20 queries .

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