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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[php学习资料] 验证码

[复制链接]
跳转到指定楼层
楼主
发表于 2018-12-16 01:00:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
index.php* v" Y- g; o6 W7 [3 D8 l- Y( [' h
  1. <?php
    8 Q5 L1 t8 O2 |; G$ T# C+ P
  2. session_start();
    3 [9 p+ M7 \- C5 U' g9 z$ K, j
  3. // 丢弃输出缓冲区的内容 **
    : b" U, D6 Z/ u5 w) R2 G1 t  i
  4. ob_clean();
    , O4 ~( F. [5 l# s- U
  5. header ('Content-Type: image/png');! ^# ?4 o, L' S! L
  6. // 1 创建画布" w/ |  w, C& Q2 J' C1 A
  7. $image=imagecreatetruecolor(100, 30);
    , x7 b! _% _; `; I
  8. // 2 画一个矩形并填充背景颜色为白色
    % ~: K6 z8 X; }  G/ W1 p3 H" I
  9. $color=imagecolorallocate($image, 255, 255, 255);* [" R" M; o% y) z4 q
  10. imagefill($image, 20, 20, $color);
    5 T2 `/ E9 R( C) _; _5 P
  11. // for($i=0;$i<4;$i++){
    1 h9 v# {6 _& [; {: I! d
  12.     // $font=6;# \* W* i; z. S" }3 ^
  13.     // $x=rand(5,10)+$i*100/4;
    0 q# ^$ T3 i! h$ L8 Z  K  I
  14.     // $y=rand(8, 15);
    $ ~3 c7 ^" Y7 Q) ?; N! C; c
  15.     // $string=rand(0, 9);
    # x% P* s! V) D. F7 B4 s
  16.     // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));
    $ s! q3 J4 b, `; m/ ]
  17.     // imagestring($image, $font, $x, $y, $string, $color);- y9 v, E/ y0 S9 G! j
  18. // }7 N3 i' K' c7 U, p; A
  19. $code='';' S0 h% y) ~" n# l; u* X
  20. for($i=0;$i<4;$i++){& z4 j% S! F* w0 C  k/ G
  21.     $fontSize=8;& g8 W% ?, b% K% u8 y' X0 S
  22.     $x=rand(5,10)+$i*100/4;
    ) k0 |  Z3 A+ G1 @# b
  23.     $y=rand(5, 15);5 W# V! j+ `1 ~4 Q2 |) ~6 @* ^
  24.     $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
    " r6 k* @: V& _# N
  25.     $string=substr($data,rand(0, strlen($data)),1);
    * k5 F' e9 W4 s* W! e4 a0 V
  26.     $code.=$string;, J7 o, |, w+ M) m- q
  27.         // 3.2 加载自定义字体3 Y7 V+ r2 j8 x4 i% }: D
  28.         $font_file = '1.ttf';& Q& ]. F/ T6 C! q( K, F+ S  T
  29.     $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));9 Y/ t2 U! b& T! \4 P/ O3 P& W. M
  30.         // 把文字填充到画布
    . ^0 x1 C# S6 ?  I  B) r6 M9 w
  31.         //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);/ H) T" K1 K) L1 C
  32.     imagestring($image, $fontSize, $x, $y, $string, $color);6 d% f: Z  v6 G" r+ s0 R
  33. }
    # o- w5 s+ X; @5 ]  t, z2 r" Y" T
  34. $_SESSION['code']=$code;//存储在session里
    ; ]* y; q0 x) j, x- O, `
  35. for($i=0;$i<200;$i++){4 N1 ], X2 [7 K5 r8 [3 M& N7 q2 Z$ q
  36.     $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));/ u6 }) F; q* b9 X8 `" g. P
  37.     imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);
    / ]: x" q! x3 w% x
  38. }
    . s2 t4 W9 _: V7 D4 j  q
  39. for($i=0;$i<2;$i++){' A9 o% W# S" O7 ]
  40.     $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));" f. p+ d$ f* J* v' b
  41.     imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);0 \  x/ B) ]$ {* D, `- J
  42. }/ }' a5 ?- Y' O5 F6 l) P9 t
  43. imagepng($image);' v1 ^! w+ V& _7 b. [+ j
  44. imagedestroy($image);
    1 q( v1 W6 c. i. f$ u+ k

  45. 9 f3 ~% j. p+ S) |  I' ]9 Q2 o
  46. ?>
复制代码
- j& B0 L& z' ?0 m1 j' v$ Z8 k# G! `
form.php+ I! ?* R, n7 I1 Q: K
  1. <?php5 G9 x/ i* E5 P. u1 ]
  2.     if(isset($_REQUEST['code'])){. _7 h- o- `" C- f8 r+ P* l
  3.          session_start();* Z! z8 ^* Z, P0 m, v2 A
  4.         if($_REQUEST['code']==$_SESSION['code']){6 a$ T2 ^4 }) N( b1 h
  5.             echo "<font color='red'>输入正确</font>";
    $ _6 V0 o* ?- U. _
  6.         }else{$ O6 j8 \) ~$ Q
  7.             echo "<font color='red'>输入错误</font>";
    7 G! R' d, r& l1 B4 ?" [
  8.         }* t( L, N4 T" ^& B& A) l! J2 G
  9.     }7 x1 ?+ V3 }! }3 ~; F) F: ]
  10. ?>
    ( i' D6 A$ D( h8 x( |0 M
  11. <!DOCTYPE html>, v. V  Q1 C0 z0 G9 j, }9 e' i4 [
  12. <html>% y1 C: d: p% L, |: X: H
  13.     <head>
    % {9 W- O" X' ]  m5 h
  14.         <meta chartset="UTF-8" />$ a) m$ R, I+ G
  15.         <style type="text/css" rel="stylesheet">
    3 y# M" \1 o( I! F, d$ f, E% p
  16.             a{: X1 r/ j8 Y  T  z9 {8 A
  17.                 text-decoration:none;0 O- A2 h; G% d' B% b$ n; Y
  18.                 font-size:30px;, ]+ [0 o$ B, q% [
  19.                 color:blue;: V/ T  L( J* _7 D' F  O
  20.             }
    * i2 n$ h7 I. E- A- I. r
  21.             a:hover{5 s0 S8 E1 _) Z4 c: }/ |
  22.                 text-decoration:underline;6 ]! e% Q4 k& Y# a9 h
  23.             }
    . S2 Z' m) s9 R* F* _" M
  24.         </style>1 i- O/ G% Q4 r3 {
  25.     </head>
    7 |- a, z" t, x+ J
  26.     <body>
    # o2 u8 J' t6 \2 w; `2 {2 p& {
  27.         <form action="form.php" method="get">
    , e% s  {9 t. G5 }4 z  |' W+ e% }5 O
  28.             验证码:  <img id="img" src="index.php?" onclick="changeCode()"/>9 ~3 p3 x& u* C3 ^; M
  29.             <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />
    " O* O. t: X! P( Z8 @) x8 c5 w/ d5 F" J
  30.             请输入验证码: <input name="code" /><br />! O2 [+ F* W8 O) p
  31.             <input type="submit" value="提交" />
    * \1 V6 A6 s, x( K: Q
  32.         </form>$ v: U  Y. z, E5 p2 s6 X4 W8 [
  33.     </body>
    / q+ G9 E# O+ R8 ]0 E4 \
  34.     <script type="text/javascript">
    0 O: n& V2 B9 W. F0 [; r
  35.         : w8 k; B! M# v( f1 O; Q
  36.         function changeCode(){) [8 y6 I( R/ x5 v! T3 d2 a
  37.             var img=document.getElementById('img');
    $ a4 M, h/ O9 n6 `3 |- [1 f$ A" J
  38.             //img.src='index.php?r='+Math.random();% V& \# J5 R4 t
  39.             img.setAttribute('src','index.php?r='+Math.random());( J  M5 ]$ s; j( n; \
  40.         }3 N6 [( O8 l2 E0 {4 _; F' E8 C
  41.     </script>6 i& u& N6 F; Z5 f7 u+ C5 M
  42. </html>
复制代码
' V* p, ]# l# ]# J2 L8 t0 Q' U. n

+ G; @; g8 C7 k/ l, [
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-3-17 20:00 , Processed in 0.074071 second(s), 19 queries .

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