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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[php学习资料] 验证码

[复制链接]
跳转到指定楼层
楼主
发表于 2018-12-16 01:00:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
index.php
, D/ r* q( M7 o9 ^' }
  1. <?php( l& C7 Y3 y8 x; x4 t% B; g
  2. session_start();
    " \- ~( s; n+ X3 {  p( R
  3. // 丢弃输出缓冲区的内容 **3 u- r& a& p5 W# A) g* z# N
  4. ob_clean();( z" [" Q; d4 P7 d
  5. header ('Content-Type: image/png');7 w" j. `# j  p
  6. // 1 创建画布
    & _5 W3 u. I4 {+ @- s
  7. $image=imagecreatetruecolor(100, 30);+ w0 e; z. s+ O, r- t# @2 d$ L
  8. // 2 画一个矩形并填充背景颜色为白色4 [7 Z* d* d" ]. I2 r0 N: Q
  9. $color=imagecolorallocate($image, 255, 255, 255);4 P; u8 n2 O* H
  10. imagefill($image, 20, 20, $color);) P3 R8 y% c5 N8 @8 n' w
  11. // for($i=0;$i<4;$i++){
    3 c, o1 h3 M/ @1 y  u: A
  12.     // $font=6;
    * g; j& R% b& ?! |# n: s- z  A6 H
  13.     // $x=rand(5,10)+$i*100/4;
    ! p, c, A" t& c" p+ F
  14.     // $y=rand(8, 15);
    ; c+ `# \/ }* c( G+ h; b
  15.     // $string=rand(0, 9);0 N6 T+ q( y  W- ^5 t' I
  16.     // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));
    % ~2 ]5 L0 x# \$ Q* J: s( V- d) f
  17.     // imagestring($image, $font, $x, $y, $string, $color);
    0 m  t1 L2 I2 o' v5 r# t$ ?
  18. // }+ P4 F# C" [/ p: |
  19. $code='';, L! l$ W% D4 `+ L4 i, {# U" H1 I
  20. for($i=0;$i<4;$i++){
    3 s& X1 E) S3 M0 b5 X" [1 P4 Z
  21.     $fontSize=8;' l( G# G! C# J
  22.     $x=rand(5,10)+$i*100/4;- m/ p, K; \! o9 w" O+ }& [) b
  23.     $y=rand(5, 15);9 F( t* Z9 ~% @% D* I. z
  24.     $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
    2 m; ?9 f$ o4 q8 @( `* A+ y+ O
  25.     $string=substr($data,rand(0, strlen($data)),1);. M4 S# j4 }+ A1 n' ]3 v
  26.     $code.=$string;& T& v, _+ h5 @' P5 R0 s" R
  27.         // 3.2 加载自定义字体
    ! p1 t3 M# I9 i& b
  28.         $font_file = '1.ttf';* ~7 y. z0 v& G  C3 J* b  K6 n% y
  29.     $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));/ }. Y7 c. v% h) v/ w
  30.         // 把文字填充到画布+ O6 l0 j7 }; B# l6 W) ~7 S
  31.         //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);
    6 {, I" C" N7 q' ]( u
  32.     imagestring($image, $fontSize, $x, $y, $string, $color);
    ' c7 r6 p* G; T
  33. }5 S" c" `/ H. B) T4 r8 m2 D
  34. $_SESSION['code']=$code;//存储在session里
    # T3 ~- t( C# }" d8 J; P
  35. for($i=0;$i<200;$i++){8 @  A8 [2 J3 {5 ^; x
  36.     $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));# T' }% I$ i# K. ^' T
  37.     imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);% t* ^0 b+ o% k
  38. }
    ! m0 Q5 U9 X! l) K+ U8 l( M3 U* M, {
  39. for($i=0;$i<2;$i++){
    9 Q& ]1 u0 K, N( v  ^. c4 Q; d
  40.     $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));
    7 j% i1 M$ B( p( x" H: @
  41.     imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);
    % h* I. u0 l* k  Q( d
  42. }
    , O" X4 d8 y. K% O6 c; k* y
  43. imagepng($image);( T3 ~* m2 g" F; v( p
  44. imagedestroy($image);
    ) c* B0 x" `' R* e' _) K1 A& P7 J. V
  45. ' b3 _" S1 Z' \6 m' k
  46. ?>
复制代码

2 A0 T* `4 Q9 I  Z7 gform.php
$ y4 ~0 g6 f9 C9 b; ]" T0 V; K" ?
  1. <?php
    8 u! F: y, X+ T8 w: H
  2.     if(isset($_REQUEST['code'])){
    - W* q& ~8 u$ ~, z% i: U( t- ^
  3.          session_start();
    + u, x9 B+ C) C/ F" w- i3 N+ v
  4.         if($_REQUEST['code']==$_SESSION['code']){* f7 V; i9 ]5 T2 ^3 M1 E, _) B
  5.             echo "<font color='red'>输入正确</font>";
    7 P7 u1 I2 T1 X/ B2 R, R1 E
  6.         }else{
    , x$ p7 y- E) }' ~' O
  7.             echo "<font color='red'>输入错误</font>";1 ^& g3 O4 a0 g
  8.         }
    6 ]4 U- o# ?3 z( |$ M
  9.     }
    - t: h: Q$ P. @9 Z6 i8 y. e# F
  10. ?>6 u8 n* x6 t' X: Q! M4 @/ K: s
  11. <!DOCTYPE html>
    . ]2 z) L* l$ [3 b$ y# h1 F
  12. <html>
    0 `* [" m0 v% I' [+ r3 ?
  13.     <head>
    " u) d0 U3 N4 y) }  J5 Z& Q; r' p2 ]
  14.         <meta chartset="UTF-8" />
    . E" j# I4 N( ?5 U$ z
  15.         <style type="text/css" rel="stylesheet">! b( Z4 g. ?; O5 t8 J& f5 a2 x! v( r
  16.             a{
    " _6 j; I& V& c9 j
  17.                 text-decoration:none;. K, M! ~' |9 c9 ?" t5 k
  18.                 font-size:30px;3 e! B7 C- ?2 j0 d& Q
  19.                 color:blue;
      g  }( W4 Z, r: l) W) p8 j
  20.             }- C8 {2 w* B9 p* ]4 V
  21.             a:hover{
      A1 ^6 f$ A) p8 ?
  22.                 text-decoration:underline;
    ) o8 [9 ?1 v& _9 f+ G
  23.             }, P) l* l" k; j7 m# C9 |, J
  24.         </style>3 @. Z8 ]- O9 h5 o/ f# d( V( `
  25.     </head>" Z4 l) n+ I8 _5 A
  26.     <body>
      T  P( Q. ~3 p4 `1 j; k4 Q
  27.         <form action="form.php" method="get">
    ! H$ f% d! d/ r2 i2 S: H4 \6 B4 L
  28.             验证码:  <img id="img" src="index.php?" onclick="changeCode()"/>
    1 k# O6 N6 [! ?& _
  29.             <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />' ~* M# t1 E+ k* O
  30.             请输入验证码: <input name="code" /><br />& Z- t* \& C$ I  j+ H& d
  31.             <input type="submit" value="提交" />
    + n* d( w3 ~& L( }, u( `& L9 o; R
  32.         </form>1 ?# p) N: Z! }9 }& k, s
  33.     </body>
    , D8 h; X4 ]7 t+ O
  34.     <script type="text/javascript">1 U3 N3 V4 _/ p" g8 L9 t
  35.         
    7 h4 I9 C$ ]& _
  36.         function changeCode(){
    6 Y" f7 e4 @& M; W5 E; p  q+ D+ [9 ~
  37.             var img=document.getElementById('img');+ x+ L* r) }* J9 W0 g7 u0 c
  38.             //img.src='index.php?r='+Math.random();
    ( m' J1 V0 B# ?. b; o) U  I
  39.             img.setAttribute('src','index.php?r='+Math.random());; B; l' y) r5 K) A6 s, Q" W
  40.         }
    ' `8 R8 \: d: j+ l4 \9 ?& B
  41.     </script>
    % Y' E/ F# s$ l! e6 x
  42. </html>
复制代码

8 X8 w3 D% H8 N. p5 U( |9 U& {7 Z! l5 l4 v1 ^
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-9-22 04:11 , Processed in 0.054758 second(s), 19 queries .

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