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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[php学习资料] 验证码

[复制链接]
跳转到指定楼层
楼主
发表于 2018-12-16 01:00:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
index.php
# `- {; h% r3 U& ]2 q3 i
  1. <?php1 T  A: I- o* K7 A6 s
  2. session_start();9 D" x0 u/ T- a+ c6 x* f, G
  3. // 丢弃输出缓冲区的内容 **
    1 L, s  Z+ n- r) t% S: N) a
  4. ob_clean();2 p) K/ w* W. O& {* O
  5. header ('Content-Type: image/png');" X  G! Q) H! a! y3 O5 s+ x
  6. // 1 创建画布, c" a8 v$ s8 N
  7. $image=imagecreatetruecolor(100, 30);
    3 [4 Q3 _, ^. y$ F3 V
  8. // 2 画一个矩形并填充背景颜色为白色: u3 @6 n, `5 ]* P( e1 h
  9. $color=imagecolorallocate($image, 255, 255, 255);
    ; u; p7 |, f) `) M
  10. imagefill($image, 20, 20, $color);6 O( Z  O. d1 g# S. L3 T+ y
  11. // for($i=0;$i<4;$i++){
    . T2 n! h1 K2 g! Z
  12.     // $font=6;. @1 Z& ^6 r& T
  13.     // $x=rand(5,10)+$i*100/4;  p6 a- A& N, r) G1 S
  14.     // $y=rand(8, 15);! e; e9 z+ e) Y4 C- ^, B  |
  15.     // $string=rand(0, 9);- v1 v4 Q. X! A* q* l, i" p9 ]5 s
  16.     // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));$ O3 r: U' m8 p3 U! X5 z
  17.     // imagestring($image, $font, $x, $y, $string, $color);6 E; o: l4 |, s( N
  18. // }
    0 Z# w5 r2 s. e3 @" k  P& F3 Q
  19. $code='';4 A3 u; z# C9 N/ v  K
  20. for($i=0;$i<4;$i++){1 B2 ]2 v, @3 h+ Z5 y3 \
  21.     $fontSize=8;' |$ }$ a" m$ C" M) z0 q8 J
  22.     $x=rand(5,10)+$i*100/4;+ k1 D6 q5 u- \
  23.     $y=rand(5, 15);9 j# `, P0 V7 F- E0 m( l; J/ ]9 N7 Q+ _
  24.     $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
    6 x( p  Q7 w: {
  25.     $string=substr($data,rand(0, strlen($data)),1);
    ( c, k& ?' e$ q' k; _
  26.     $code.=$string;" m9 |- K+ S$ ^
  27.         // 3.2 加载自定义字体
    0 O$ ^( Q8 m$ |2 O; j
  28.         $font_file = '1.ttf';7 b7 v" A8 e2 I
  29.     $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));. }0 k5 k- d0 l
  30.         // 把文字填充到画布
    3 b; S' O% ^5 u, {
  31.         //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);
    0 T4 B  |1 p$ o# k; |7 N
  32.     imagestring($image, $fontSize, $x, $y, $string, $color);  \% A; N4 e6 I! k. q
  33. }) a: ?1 |  P$ v8 c
  34. $_SESSION['code']=$code;//存储在session里
    ; I! V' L  U4 y
  35. for($i=0;$i<200;$i++){; g1 n, H/ T& A" e( P+ J$ Z
  36.     $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));
    & i6 Q0 w: \6 N
  37.     imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);* Z! n" s- N4 c( V' K
  38. }
    % N( `9 S2 O! E5 C; i- \7 N
  39. for($i=0;$i<2;$i++){: @( F! Q2 o/ P6 H
  40.     $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));6 `' q) s9 ?4 j4 a+ n5 X
  41.     imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);
    8 s  ~% h. {0 U7 d& ]2 i
  42. }
    4 b- ^# G' p0 ~% ?' h
  43. imagepng($image);) H' K0 {& j: m, y
  44. imagedestroy($image);" U" z+ [, P  c4 f/ T- _+ N( Z
  45. ) F0 B6 K$ h0 y+ d5 z* T1 l4 |2 o
  46. ?>
复制代码
5 E, c9 V3 f4 B, o) j1 u1 H
form.php
) E% Y6 w8 S, p4 F5 {9 c$ b1 P% n
  1. <?php* `- m# d3 n& \- r" Y- r/ W" m+ N
  2.     if(isset($_REQUEST['code'])){) Z: c" ?- a$ f" h
  3.          session_start();
    8 `) }! {- N3 o8 B) o
  4.         if($_REQUEST['code']==$_SESSION['code']){# }1 J9 g2 y: h2 }
  5.             echo "<font color='red'>输入正确</font>";
    * f7 d0 U( z# l! I2 H7 O! D
  6.         }else{2 @5 s2 B" X: E" K( U$ T% l
  7.             echo "<font color='red'>输入错误</font>";
    6 Q; g* Z6 r0 c7 }8 b
  8.         }
      n  Q/ C5 `7 J) H
  9.     }
    1 \; q: D! i+ v! w/ _
  10. ?>
    3 N% |8 x* J5 F8 s) Q" l
  11. <!DOCTYPE html>6 ?0 a; q+ N4 y* P2 z- h* a
  12. <html>
    + C. F% n6 W" K6 h8 {, ~' M
  13.     <head>! g/ z% y- y6 I4 a' E
  14.         <meta chartset="UTF-8" />5 E& s; C/ h" G
  15.         <style type="text/css" rel="stylesheet">
    ) s2 j- Q! r4 M& W7 @* p/ C
  16.             a{
    ( B" j0 V* D# j7 C5 {  }  a
  17.                 text-decoration:none;
    ) {: i6 }1 B; L: z" N5 F, r- B3 m
  18.                 font-size:30px;
    : n  p) d3 {8 G' v% S
  19.                 color:blue;$ ^( ~$ V/ Q, z  V1 Z1 v5 W7 _
  20.             }0 A2 r% I! \' S6 M! k
  21.             a:hover{- B; z1 N7 ]# S
  22.                 text-decoration:underline;
    * `1 ^' f6 W. A' q
  23.             }
    : q( D7 Z. g% V' x4 u$ s
  24.         </style>- i8 R. M" h9 ~2 B- v
  25.     </head>6 o9 J6 u$ u& [+ L) R( I
  26.     <body>. m$ Y2 P/ c# ?6 I: O, V
  27.         <form action="form.php" method="get">
    4 B. X8 B( d2 [3 N, E* \
  28.             验证码:  <img id="img" src="index.php?" onclick="changeCode()"/>/ B3 t. ^: x7 _! ?9 W
  29.             <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />) h) J& V; g0 R& w1 x
  30.             请输入验证码: <input name="code" /><br />
    & f' x. x) x8 R2 _& `4 w
  31.             <input type="submit" value="提交" />
    * U2 h; a3 w1 z& w
  32.         </form>  c# S9 }1 _! t1 ~+ W2 q
  33.     </body>
    " J( n" L- ~) ^* q  I
  34.     <script type="text/javascript">
    3 ^: Z# k& g' H; k& Z4 s' P
  35.         ' h6 ^. m4 f4 }( p& L9 i+ g2 d+ M
  36.         function changeCode(){
    $ J9 ]( A" y+ J6 H
  37.             var img=document.getElementById('img');
    0 @. k7 p( V, ^0 W2 W
  38.             //img.src='index.php?r='+Math.random();
    ; W% u4 |2 c$ @" N
  39.             img.setAttribute('src','index.php?r='+Math.random());
    ! w) D) N2 q, ^% }: w" P, T. m
  40.         }; |2 c) ^- k4 \3 y" R+ B$ {
  41.     </script>1 v( [! H! W1 F; G
  42. </html>
复制代码

2 K' o7 [# |4 I% c% B8 S  |5 Z0 P- ?4 G; ^& r
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-4-30 21:44 , Processed in 0.062741 second(s), 19 queries .

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