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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[php学习资料] 验证码

[复制链接]
跳转到指定楼层
楼主
发表于 2018-12-16 01:00:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
index.php+ j. U7 O' X2 F2 o
  1. <?php5 T" D+ k7 b3 V) k) E! J8 w0 H4 x; ~! F
  2. session_start();& D+ T; t' P. F8 A: P0 `2 S
  3. // 丢弃输出缓冲区的内容 **
    2 z( z2 C3 W  Z
  4. ob_clean();0 \: i/ z% e+ T" n1 W9 K2 O
  5. header ('Content-Type: image/png');
    ! J+ _7 V: \/ o
  6. // 1 创建画布5 W4 b8 B* G. W) H
  7. $image=imagecreatetruecolor(100, 30);
    3 c& e9 ~6 m# @6 a
  8. // 2 画一个矩形并填充背景颜色为白色; ^+ |7 `9 G8 c- i6 k9 v
  9. $color=imagecolorallocate($image, 255, 255, 255);
    9 A2 N) P% X  v" b9 S
  10. imagefill($image, 20, 20, $color);
    " E" {6 c' d' O' l( o# N- v
  11. // for($i=0;$i<4;$i++){1 a4 }+ u% B; I
  12.     // $font=6;
    1 F: o5 O3 x; z5 T" A5 b' @* K
  13.     // $x=rand(5,10)+$i*100/4;
    , m  n3 a& X/ s$ n
  14.     // $y=rand(8, 15);" c1 D4 ?% W1 z: ]2 C4 S0 @9 N
  15.     // $string=rand(0, 9);9 F( ]5 z% G% k" F3 a
  16.     // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));
    : I9 R* _- {1 y7 e0 W
  17.     // imagestring($image, $font, $x, $y, $string, $color);& i% e- r9 W; x
  18. // }
    ) u5 Q5 R$ H$ ]9 ]4 c
  19. $code='';
    / U- u4 v5 E) d9 a  i' T/ Q
  20. for($i=0;$i<4;$i++){" T2 k7 f7 W) }8 Y& \" t
  21.     $fontSize=8;
    $ K# U1 ^/ i; V: s! |' f9 U8 x& {; `8 y
  22.     $x=rand(5,10)+$i*100/4;
    ! f4 Y" F2 g$ N
  23.     $y=rand(5, 15);
    : D( E2 ?2 \( U6 ^/ l3 }: p# y
  24.     $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
    $ `& Y) C0 t3 D( q/ j" J
  25.     $string=substr($data,rand(0, strlen($data)),1);
    6 n% h) {; I# a+ h/ S4 K
  26.     $code.=$string;
    / p% s+ f, {' d4 H
  27.         // 3.2 加载自定义字体, s; v) _- ]% p) v5 W
  28.         $font_file = '1.ttf';3 W0 W3 _; t  H9 b( x& g" R) K& \
  29.     $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));
    3 `6 F0 }7 x) L) d; _1 g! F
  30.         // 把文字填充到画布" Y( Y) s5 D% q" W; |4 J0 |- p
  31.         //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);
    8 q, @9 }2 S- K3 C
  32.     imagestring($image, $fontSize, $x, $y, $string, $color);4 `  u: `8 l- \& Z7 F+ m
  33. }. e0 [- m; b+ @2 X
  34. $_SESSION['code']=$code;//存储在session里0 P$ l9 k7 \( |+ s0 c8 a
  35. for($i=0;$i<200;$i++){
    3 t. U; t# S, M" K
  36.     $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));
    ' W+ E! v& j4 X1 i
  37.     imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);
    0 C0 S3 P. O. U
  38. }  }% Q8 b8 V: E6 |  G6 r5 `4 p! ~0 j
  39. for($i=0;$i<2;$i++){
    % p6 B; V. J3 ^
  40.     $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));
    % U: c5 b' x7 \, f& ?
  41.     imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);) [+ R3 g: ^/ ?5 ~5 a
  42. }
    + g9 a/ `0 n8 H; b9 E
  43. imagepng($image);6 L/ M' c; Y* ~
  44. imagedestroy($image);0 m" E6 h6 k9 Z# u: p) e
  45. ' X* l! x* b) q3 e0 e
  46. ?>
复制代码

* b% {8 d* B5 A; Kform.php* n$ V$ Z% s; O+ H
  1. <?php5 a5 L, Z1 R: h! N5 z$ {, @  |0 X8 {
  2.     if(isset($_REQUEST['code'])){
    * v, G. o6 N8 n( g, Z8 ]/ |
  3.          session_start();
    8 M4 p0 Z2 R; y6 \- Y
  4.         if($_REQUEST['code']==$_SESSION['code']){
    0 l/ z- Z# U2 N2 R
  5.             echo "<font color='red'>输入正确</font>";
    # j* [' h' z, l" A
  6.         }else{
    * h) N, |" {. _
  7.             echo "<font color='red'>输入错误</font>";7 [5 W/ S; C0 J$ p8 s, J
  8.         }
    0 ^0 c) P' Q: g# s
  9.     }0 i4 @5 e* K  L' M8 g4 M9 U0 m* m5 R. r
  10. ?>
    # J8 p2 l9 G) a6 i' `
  11. <!DOCTYPE html>$ L" T0 ^) J/ ^9 R# l. A
  12. <html>
    8 y# ^, O2 j2 F
  13.     <head>
    " O/ y  D- \+ Y6 a" D5 r: ~
  14.         <meta chartset="UTF-8" />2 @/ A1 c1 @- g' d" ~
  15.         <style type="text/css" rel="stylesheet">
    # U8 U& g' |% J) V9 Q" X
  16.             a{  M# Y( Y/ n' D, U: P# T; D; M
  17.                 text-decoration:none;
    ' C6 P. x  p9 ?
  18.                 font-size:30px;
      o- l5 T* c, c: F1 M! r
  19.                 color:blue;
    1 }/ h: Y) A6 e$ N+ q0 i& |
  20.             }6 r0 W; h3 i; ^& @4 X3 K) x: U, ]
  21.             a:hover{
    / `4 Y. {! }/ n; O
  22.                 text-decoration:underline;
    . C4 [7 a& z$ V" ~  L! K9 c! d
  23.             }
    ; M: t0 g9 ?/ b. ^$ f$ c
  24.         </style>
    * J% ?; b% o2 h# j+ G
  25.     </head>0 c$ |, D2 W( T( i: H
  26.     <body>  R- Z  ~/ E9 ~, `6 r! h( d
  27.         <form action="form.php" method="get">2 s8 q( \) j2 q# L5 M
  28.             验证码:  <img id="img" src="index.php?" onclick="changeCode()"/>
      g$ N4 g9 A9 W6 s
  29.             <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />
    " m9 C& g7 s' w* }3 W
  30.             请输入验证码: <input name="code" /><br />* C! Q$ f* O1 n; {7 J
  31.             <input type="submit" value="提交" />) W( L5 b0 M( J7 D. _: v( p6 N* l. Z
  32.         </form>+ g- g' C, S4 t# V- n- T5 q. P
  33.     </body>
    9 Z2 S7 a, j5 u" {' u2 A' z+ }( z! T
  34.     <script type="text/javascript">
    8 j/ _0 S5 ~2 H' X: K6 w" z, W
  35.         1 F. A" P, K0 E- T- D
  36.         function changeCode(){. T7 A, v& a2 ]! {* }4 n7 }
  37.             var img=document.getElementById('img');5 v$ P. K6 F; p
  38.             //img.src='index.php?r='+Math.random();$ U  i  [6 m* }% V+ O- [4 E4 R
  39.             img.setAttribute('src','index.php?r='+Math.random());5 C; N, M  B, }: F5 {" y3 D
  40.         }
    $ I; T% N) C1 y% d, h
  41.     </script>
    ! n+ t" }9 n6 M' Y8 O& q# @
  42. </html>
复制代码
  K) K4 u) r. B1 n5 y, ^& B
5 v& x5 w% ?+ x" O/ c  U) L
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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