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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[php学习资料] 验证码

[复制链接]
跳转到指定楼层
楼主
发表于 2018-12-16 01:00:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
index.php
# e4 Y- c1 y/ f( N3 A
  1. <?php+ z( Y( E) ^# @$ {: w
  2. session_start();
    ; q) H: ]; q- r/ ?7 h' G( \! ~
  3. // 丢弃输出缓冲区的内容 **
      Z  z: I& p# f! u
  4. ob_clean();$ ~) h9 \/ Q+ d1 r8 Z/ A# H
  5. header ('Content-Type: image/png');
    7 S" |$ y, e7 e
  6. // 1 创建画布5 j) N. u4 ?# K, E
  7. $image=imagecreatetruecolor(100, 30);' S7 D. k5 T1 }. E8 j, U
  8. // 2 画一个矩形并填充背景颜色为白色
    1 n* U  Q/ _  b  M* z+ ]
  9. $color=imagecolorallocate($image, 255, 255, 255);
    $ C7 G+ ]* k% J+ K2 g" o
  10. imagefill($image, 20, 20, $color);
    1 B9 H1 J  Q* D: m7 V
  11. // for($i=0;$i<4;$i++){
    ' c5 K/ W2 p! O  f+ L; t
  12.     // $font=6;
    1 ^* q+ N5 c0 U9 q  H5 V6 ]. Q
  13.     // $x=rand(5,10)+$i*100/4;
    6 e: k* k0 X7 m% S
  14.     // $y=rand(8, 15);8 }9 f5 E( u! {# N' Z% w* p
  15.     // $string=rand(0, 9);
    8 w" [* S9 W5 `* G  V0 P
  16.     // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));. \3 a. Q6 z3 }# d! \& y6 b9 X
  17.     // imagestring($image, $font, $x, $y, $string, $color);
    0 h  Y1 `+ J: f4 w' |6 \  I, g
  18. // }/ S6 f2 ~1 D$ I6 l
  19. $code='';
    " i2 Z) w7 g# s
  20. for($i=0;$i<4;$i++){
    6 m" W# ~' k+ p! [8 x* Z8 W, [2 M
  21.     $fontSize=8;( v; w7 x9 s$ e7 n7 P
  22.     $x=rand(5,10)+$i*100/4;
    & r8 ?0 Z% T8 `' Q
  23.     $y=rand(5, 15);
    % K, T5 u% W2 r8 `. l
  24.     $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
    ' N$ H9 m3 V9 Z
  25.     $string=substr($data,rand(0, strlen($data)),1);
    3 ?( |6 b8 v" r# A+ K
  26.     $code.=$string;
    3 t7 {$ n8 ?3 N% u
  27.         // 3.2 加载自定义字体; @' k$ D' O0 F* f" p2 X, {5 r! R
  28.         $font_file = '1.ttf';
    ! r0 M: A# o1 V& q( z7 g* }' Q* R/ Q
  29.     $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));1 O' H7 U& X4 f
  30.         // 把文字填充到画布
    . U- H; i. p* o% v3 O) o' X7 x
  31.         //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);
    6 T: ~  b- S7 i2 G
  32.     imagestring($image, $fontSize, $x, $y, $string, $color);1 a; Z( [# f5 S1 h
  33. }
    5 J1 i, [) I/ e! X. `, P
  34. $_SESSION['code']=$code;//存储在session里
    5 P4 H' Z6 ~) ^. I- \
  35. for($i=0;$i<200;$i++){. C6 B$ Z; p4 Q) K5 C  D- y% ^
  36.     $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));. U0 Q- e5 ^" P9 J
  37.     imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);5 X( u5 w  f0 f. z$ O) T
  38. }* ]% U5 U) t5 Y
  39. for($i=0;$i<2;$i++){7 f% I% Y( p3 G& Y  \( s' e
  40.     $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));$ F* E# m* m* W! _8 }! w4 w% ]# R
  41.     imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);6 k) N) c# ]) d* o& v& [: u
  42. }
    ; {" p; |' H2 A5 |3 b6 }1 t
  43. imagepng($image);
      t/ `' U( u* o( {1 c/ @8 U
  44. imagedestroy($image);# X6 Y* C5 `# J2 @  C9 T# `7 E: y

  45. # X( I0 Y) H2 Z7 H' f
  46. ?>
复制代码
9 U: q! k% O/ V; F7 G4 F
form.php& U$ x$ H& b" f; y% y
  1. <?php
    8 F5 {& R7 {; R0 @! d, C5 `) [/ r
  2.     if(isset($_REQUEST['code'])){* N# f2 J  B% P5 S* \6 m
  3.          session_start();/ `, J+ \- f0 D" j
  4.         if($_REQUEST['code']==$_SESSION['code']){& z( m2 `* k4 [8 I' [& ?
  5.             echo "<font color='red'>输入正确</font>";
    ' R1 M0 l, U$ ]7 M1 j
  6.         }else{7 Z) ^* q9 H  N% H* H2 e
  7.             echo "<font color='red'>输入错误</font>";' ]% W% L6 P+ K7 g7 b: c3 ^
  8.         }) n% Y6 c( q. R, D
  9.     }7 Z8 l3 j9 a* r5 z6 s
  10. ?>
    - F# S$ o. W3 @! B$ r
  11. <!DOCTYPE html>: V2 E8 b* p/ k, T  w, c  n6 r
  12. <html>+ i$ d+ t. F% f: |) j
  13.     <head>
    - b" S( Q8 u# L8 J
  14.         <meta chartset="UTF-8" />
    3 ^2 |+ h4 m' L4 }3 {+ k
  15.         <style type="text/css" rel="stylesheet">  i# C( z) K" o$ ?  ]
  16.             a{9 q. b9 u) R1 Q: M: W7 v+ y. d
  17.                 text-decoration:none;
    ' e& Q6 Y3 X/ C/ r' ~
  18.                 font-size:30px;
    ' r2 m% o4 j0 h# Y
  19.                 color:blue;
    & t& \: E, o  `. K% R4 @0 \
  20.             }
    8 l8 X# Z! N5 a, Q0 y
  21.             a:hover{: J( x3 x+ \3 f/ k/ K5 p
  22.                 text-decoration:underline;6 I7 F/ f- p8 e9 B1 x
  23.             }
    % I( A4 d% Y5 U" n' m6 f/ [
  24.         </style>
    ) Z- ?  R& w8 }" q# m( N  B
  25.     </head>
      _# R" h, C/ Q
  26.     <body>
      h' a+ [" r& ^1 Q
  27.         <form action="form.php" method="get">
    $ Z" C( t1 J. Q# Z
  28.             验证码:  <img id="img" src="index.php?" onclick="changeCode()"/>- b! s8 E5 y  `7 e' Z
  29.             <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />
    $ I0 i# M4 o; a' F; v
  30.             请输入验证码: <input name="code" /><br />
    8 D& N) f/ I- Z% V: I7 E. R
  31.             <input type="submit" value="提交" />6 c+ ^) t5 V- Z' ]. u9 m
  32.         </form>
    2 y5 f% L* y) {$ [
  33.     </body>+ B3 ~! F, c9 `9 K! h
  34.     <script type="text/javascript">
    6 W$ r, ~5 c* M1 y1 ], V% [/ L7 {
  35.         
    + g9 n/ p2 U0 Y' i6 C
  36.         function changeCode(){
    - M0 D. ~( I4 R+ t* g
  37.             var img=document.getElementById('img');5 H5 X8 h8 u8 Y: v9 W" w/ k. V
  38.             //img.src='index.php?r='+Math.random();/ F3 B( z- j( G0 X( |, M
  39.             img.setAttribute('src','index.php?r='+Math.random());
    3 q* }3 @# a/ ~. u7 ^) ~% m$ a# W  {
  40.         }5 @  s/ E: N! E6 t2 @, z2 h
  41.     </script>
    ) I' Z8 C2 C- ]/ H
  42. </html>
复制代码

* B9 G5 o$ s3 f& C; z; _9 I# b2 i* m5 ^7 N$ K
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-8-4 23:59 , Processed in 0.054772 second(s), 21 queries .

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