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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[php学习资料] 验证码

[复制链接]
跳转到指定楼层
楼主
发表于 2018-12-16 01:00:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
index.php
  {- D; F! B7 M3 k. U; [
  1. <?php; ?/ l; E  N. Q: s# i
  2. session_start();
    " `7 P7 W8 T% u  _: ~
  3. // 丢弃输出缓冲区的内容 **
    2 Z9 ]; v  `9 R
  4. ob_clean();; t* P6 D7 N7 t. o& C5 N: \! w
  5. header ('Content-Type: image/png');
    / g) r* Q& u4 N! |5 ^7 n; e7 j
  6. // 1 创建画布
    , ?8 R- p4 P8 |6 L
  7. $image=imagecreatetruecolor(100, 30);
    2 Y/ [1 F+ [) M3 `
  8. // 2 画一个矩形并填充背景颜色为白色
    ; C6 y% ~& t5 i% q, r, p4 f2 K$ C
  9. $color=imagecolorallocate($image, 255, 255, 255);% v$ B6 }. \! j+ Z- c- t
  10. imagefill($image, 20, 20, $color);) h' B! e8 s( e3 \6 _: p# y
  11. // for($i=0;$i<4;$i++){
    1 n8 Q; s4 y/ j. \( i4 ?
  12.     // $font=6;0 P" I% v! ?0 I; A7 M
  13.     // $x=rand(5,10)+$i*100/4;
    1 F4 y- P3 i0 H
  14.     // $y=rand(8, 15);* y6 A4 d  q, ?4 t
  15.     // $string=rand(0, 9);4 ~* {5 h, [9 h/ g0 m
  16.     // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));
    4 t, C' q$ a8 q# \
  17.     // imagestring($image, $font, $x, $y, $string, $color);
    5 F7 `% n9 i/ W6 {* Y7 `
  18. // }
      M) K; e4 F4 N# K
  19. $code='';# z" }' J/ a7 H) j
  20. for($i=0;$i<4;$i++){
    / C8 {) b4 p, m
  21.     $fontSize=8;; ]; \4 g* `" h
  22.     $x=rand(5,10)+$i*100/4;* T3 N, `2 f5 k: \2 e
  23.     $y=rand(5, 15);* L  S2 @% @  C' R+ L
  24.     $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
    # |$ f) R) d, I! _  g/ @+ Q
  25.     $string=substr($data,rand(0, strlen($data)),1);
    ' E4 r, `9 l5 t  C% Y& J4 ]- |
  26.     $code.=$string;
    1 v% B# c4 N/ [, f
  27.         // 3.2 加载自定义字体9 A8 @; Z5 }0 p* V) V
  28.         $font_file = '1.ttf';
    ( F3 r& Y/ ]  U  V
  29.     $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));
    4 ~4 P0 C. Y' g+ {  C- l0 H5 ?6 Z
  30.         // 把文字填充到画布
    - {3 W, z9 l( f! e: y
  31.         //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);+ V1 ?" h# K9 J, J* {8 r+ W5 j& a! ^
  32.     imagestring($image, $fontSize, $x, $y, $string, $color);& T% F& Q0 V7 n8 E- d
  33. }2 r' P- F+ b9 \: @! N7 C: z1 @
  34. $_SESSION['code']=$code;//存储在session里
    - n2 C* j, R9 M9 B5 o% g- b/ j
  35. for($i=0;$i<200;$i++){4 b% @  ?- T+ ]/ j6 _
  36.     $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));1 _( N- q3 `* s9 ^" S
  37.     imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);' H- u5 U6 v7 D( T* f
  38. }! ~5 Q3 |; ]) o" r; s; H
  39. for($i=0;$i<2;$i++){
    : i$ P) f% @5 z: n, v" d- g8 O2 a
  40.     $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));: Q+ G% ^1 K, j6 ~7 u+ ?5 N5 w
  41.     imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);
    $ ^0 N: B- {6 Y$ _, J) }
  42. }& C& G: X8 J: Z/ r" J3 U
  43. imagepng($image);/ b2 z) r7 Q) L& k* `) U/ p- ]  _
  44. imagedestroy($image);
    & q/ g* j6 X! P$ ^8 J1 a

  45. 5 W! f1 K, P0 h# F* I+ d
  46. ?>
复制代码
0 J5 v# A/ r& a7 K
form.php. A. _$ M: S8 b" a2 e; T
  1. <?php9 m# _8 z1 z2 n+ e; T
  2.     if(isset($_REQUEST['code'])){' w! N* x/ N4 l  `* B' d
  3.          session_start();% O* n* O" e1 k% [% w
  4.         if($_REQUEST['code']==$_SESSION['code']){
    3 ~7 v5 l4 s9 _9 D; d9 h
  5.             echo "<font color='red'>输入正确</font>";
    8 L! }: V& d0 E) c
  6.         }else{) _4 X0 T4 u% o# @8 T% [0 d( s
  7.             echo "<font color='red'>输入错误</font>";4 {' t* p7 C" W' W/ G' O+ r* K
  8.         }
    0 S* O# h. b. G* J8 j# ]4 `0 _
  9.     }2 ]% y- b8 F( N4 V% m& _% f
  10. ?>
    2 ~* O" y5 b0 [, e+ i
  11. <!DOCTYPE html>$ t* P8 O+ i2 q1 F$ ^
  12. <html>
    9 G) a0 P, S3 w# O
  13.     <head>9 G& A' C* `" x$ |
  14.         <meta chartset="UTF-8" />2 N7 p2 n$ ~' k; f
  15.         <style type="text/css" rel="stylesheet">0 r5 K& X. K+ @4 q. O
  16.             a{
    5 Q! r3 J4 L4 s  h
  17.                 text-decoration:none;  [$ o6 F" }# ~& g: `0 x4 L% I- o
  18.                 font-size:30px;# ^7 T6 y; j/ i5 I
  19.                 color:blue;
    3 Y: Z; |* w% q3 T; O
  20.             }
    . m1 M! k3 r' w9 x/ \! Y' f
  21.             a:hover{* X( v1 e1 S0 _& |4 b
  22.                 text-decoration:underline;1 R2 _) p. d8 ^4 Y/ Z  e' [
  23.             }
    5 o" r2 v7 L. ?' ?1 D7 ]
  24.         </style>
    * E4 K4 [' ^( C& Q2 }/ @( e5 Y3 V
  25.     </head>
    * i, @7 Z5 ]3 `! M9 K& U: z
  26.     <body>
    5 M; u6 h3 \5 Q/ a
  27.         <form action="form.php" method="get">
    3 g" o! c& `3 n6 h& t2 v) v$ a
  28.             验证码:  <img id="img" src="index.php?" onclick="changeCode()"/>
    2 n! }. ~. A# Q  ~! R8 D3 X
  29.             <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />
    / M, B- g. w/ I, k' ?
  30.             请输入验证码: <input name="code" /><br />- Y# B* P$ }' a; H2 F
  31.             <input type="submit" value="提交" />
    * t1 d1 y) K. ~% u
  32.         </form>
    6 w  ^1 D) B. @0 @8 {) k; c
  33.     </body>
    ) R' D% B3 W! n! f+ S) z" ^
  34.     <script type="text/javascript">9 f2 I- V' o1 L' L( W7 R4 D+ j
  35.         
    ' ]( G. @& F. z; G7 ]( Y
  36.         function changeCode(){
    ; i2 B4 r& n/ M! ~9 U, X
  37.             var img=document.getElementById('img');
    % O5 `/ S: w# u" y- c
  38.             //img.src='index.php?r='+Math.random();# n, U( R7 }) C/ |
  39.             img.setAttribute('src','index.php?r='+Math.random());" h4 r% S" \. Q1 `6 ], o0 Y. Q
  40.         }, p: o7 y0 O4 ~, \. K
  41.     </script>
    # z" V; V1 V  i% h; f  a
  42. </html>
复制代码

' x; l7 ]$ ?) f" O1 x9 J: C+ `9 o- C2 ^+ e
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-8-4 14:08 , Processed in 0.053884 second(s), 20 queries .

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