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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[php学习资料] 验证码

[复制链接]
跳转到指定楼层
楼主
发表于 2018-12-16 01:00:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
index.php
- u( I  Y' p& _0 P# x& K
  1. <?php" g3 ^8 o. W, {/ E' ~7 I2 c9 q
  2. session_start();
    . q( Z3 d0 M1 p/ ^, s
  3. // 丢弃输出缓冲区的内容 **
    + _. c* s/ n1 v, j# ^( I
  4. ob_clean();
    4 W7 _9 S' K- Y' e, ~1 B+ K
  5. header ('Content-Type: image/png');
    6 j' n& k+ P+ r1 _: b0 F
  6. // 1 创建画布: o& C/ h9 b) m) H/ N9 o) x0 X
  7. $image=imagecreatetruecolor(100, 30);+ [+ E# S+ ?1 y
  8. // 2 画一个矩形并填充背景颜色为白色
    - }, n; g+ P3 T7 I6 C; t; ?
  9. $color=imagecolorallocate($image, 255, 255, 255);( Q9 {( s/ F& i
  10. imagefill($image, 20, 20, $color);; J+ \& d; [+ ~/ u! l1 m8 ^, O
  11. // for($i=0;$i<4;$i++){- D5 C- P& ?2 T( @7 G5 b
  12.     // $font=6;& ]9 r* v) E% Y4 T0 r! F& [- O
  13.     // $x=rand(5,10)+$i*100/4;
    0 N9 ]) s& L6 h  B) p; C& l
  14.     // $y=rand(8, 15);
    : y0 d( T* ^- b1 ^# F
  15.     // $string=rand(0, 9);
    6 k) h% R. U0 l6 K' y, K) c6 o
  16.     // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));, K4 E1 v2 e% w7 O, I
  17.     // imagestring($image, $font, $x, $y, $string, $color);5 ?0 U8 M$ ]- O/ a; K
  18. // }
    ' q% }; f6 k. n8 ]
  19. $code='';# K# |2 z& e# r+ _
  20. for($i=0;$i<4;$i++){7 ~5 V$ ], E8 h3 Y5 Z  }8 g7 v% }
  21.     $fontSize=8;
    ' A7 G/ i+ R. b9 `7 M: V8 `
  22.     $x=rand(5,10)+$i*100/4;
    7 f2 y% y% [4 D, B0 W$ Q" C
  23.     $y=rand(5, 15);8 B. n+ s* U+ z/ r! {: ?
  24.     $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';! h% [* Z# k" F; _
  25.     $string=substr($data,rand(0, strlen($data)),1);3 L3 Q9 R  a4 X4 B& C7 ~6 Q% o+ w) \( v
  26.     $code.=$string;* H% S' i7 w* ~% N+ {
  27.         // 3.2 加载自定义字体; h4 |# _8 q7 U1 H
  28.         $font_file = '1.ttf';
    9 V9 d' c2 Y6 R/ k- ]$ W
  29.     $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));# y4 L3 G/ e- i
  30.         // 把文字填充到画布
    " ?. f) I2 j8 s0 j. e7 \1 M
  31.         //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);, v0 h- u; e! b  X
  32.     imagestring($image, $fontSize, $x, $y, $string, $color);
    ' G# ?' u, \5 _0 I  Q
  33. }! G" R0 W/ [  W8 u8 ]8 p
  34. $_SESSION['code']=$code;//存储在session里2 I# i) W1 m( H) O8 ~, N
  35. for($i=0;$i<200;$i++){
    0 E, T1 F* W2 J5 [
  36.     $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));
    " l# `% O, n+ _$ ?( K) u" u
  37.     imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);: L" w& e- n* U
  38. }
    + t' v6 F. v, Z; r" |$ b
  39. for($i=0;$i<2;$i++){8 g* ?4 o, S' L  i
  40.     $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));2 i  I4 ?9 W4 T7 V$ `& |% W( ^
  41.     imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);
    2 p+ Y" q2 W; X- W
  42. }
    " p0 O" }* h' d# l: A
  43. imagepng($image);
    : M# ]2 M8 w1 q9 S0 u
  44. imagedestroy($image);2 i, f2 m. w" x9 J- u+ X8 V# R

  45. 6 g  u0 w1 y4 C7 v- {
  46. ?>
复制代码
5 v9 ^+ h; q6 p5 D% N( S1 N
form.php: p4 T- q/ j8 o' J  @/ M: q
  1. <?php1 o, U; `4 d! P6 ~
  2.     if(isset($_REQUEST['code'])){
    8 H2 Y6 T- l& H$ o/ L. ^  [4 \9 u
  3.          session_start();* z5 U. C  ]. n' w, x- b7 m9 L
  4.         if($_REQUEST['code']==$_SESSION['code']){
    ) |+ V6 q) \1 s, y; [0 P
  5.             echo "<font color='red'>输入正确</font>";; m& \& F4 s" M" R& \/ A  x
  6.         }else{
    : j0 h. d5 C) y5 ~& J
  7.             echo "<font color='red'>输入错误</font>";4 {! H# P, D" v- h9 I6 Z
  8.         }
    6 _  _( X; ~* Y4 A. ~$ m9 e
  9.     }
    / ^4 O, H5 `0 H* r; q* E8 t
  10. ?>
    : E5 ], d% T+ z% ]' ^
  11. <!DOCTYPE html>
    ! J9 B4 x6 }. n0 j) z( n* y- a
  12. <html>5 g0 f2 J4 G1 R& A( `. Z* g
  13.     <head>
    4 H' Q" R. Z) ~. e
  14.         <meta chartset="UTF-8" />" g9 r' o- f$ y
  15.         <style type="text/css" rel="stylesheet">) L/ v2 ~' t+ U' b+ c
  16.             a{
    7 e3 ?& O5 `' v5 o" d: [+ Y
  17.                 text-decoration:none;( s# m6 d# L: Y. L( G6 A
  18.                 font-size:30px;4 f% z% |6 T) d3 C5 f1 V
  19.                 color:blue;
    ) D/ {3 k' S! z; o' M6 {* A
  20.             }% o; E3 Z$ c% F# w
  21.             a:hover{
    0 b3 v/ O2 F- D4 a. ~
  22.                 text-decoration:underline;
    , v6 w$ K8 R# v" g3 V1 \
  23.             }: q2 j" V9 }: x: R( t- E5 R/ g
  24.         </style>
    5 c4 c- u" Z- X" _: b  t
  25.     </head>9 w. J6 d7 k& z0 _7 a! m- X  P% U
  26.     <body>
    & R$ m$ R0 \/ l! ~
  27.         <form action="form.php" method="get">) G1 t7 P0 I1 a  p
  28.             验证码:  <img id="img" src="index.php?" onclick="changeCode()"/># O6 D5 O1 _8 t" C2 h
  29.             <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />
    : y' _; I7 }! Z  }& s3 c
  30.             请输入验证码: <input name="code" /><br />
    ' J2 _; i/ T8 E/ c+ w" }7 f
  31.             <input type="submit" value="提交" />
    . q; c2 O- O. p/ Z* j
  32.         </form>. Z. a$ Q* s8 M
  33.     </body>" z5 Q& ]: q* {5 v
  34.     <script type="text/javascript">. d$ x! V) _; T5 ?2 t
  35.         
    9 ~3 u$ n- W7 M; `6 R4 h
  36.         function changeCode(){2 u4 `" _2 ]- }) u) q& P' M
  37.             var img=document.getElementById('img');3 z) k. i& v( F- P
  38.             //img.src='index.php?r='+Math.random();6 k* b7 P! u1 d; n1 F& ~
  39.             img.setAttribute('src','index.php?r='+Math.random());
    ! b+ Q# p5 M8 Q+ F  o3 u. [
  40.         }
    ( S' \# l. ?0 r( u" T- |
  41.     </script>1 L! A; V, T$ i9 T
  42. </html>
复制代码
$ f3 V. X$ h9 [, z0 R9 G8 d8 }

3 l& K2 ^7 S+ ^* r; C; k* {; f+ B
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-1-30 15:56 , Processed in 0.078535 second(s), 20 queries .

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