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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[php学习资料] 验证码

[复制链接]
跳转到指定楼层
楼主
发表于 2018-12-16 01:00:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
index.php
6 a: J/ U1 W; \7 L* ^: J( \
  1. <?php
    6 f6 T* ?) t( }8 w+ h
  2. session_start();
    ! t1 j* i6 E* p7 O5 O$ Y% w( S& p
  3. // 丢弃输出缓冲区的内容 **
    0 A( T7 c, O  [) |
  4. ob_clean();* k* q. t6 ]. l7 x5 T  R
  5. header ('Content-Type: image/png');* N  J6 ?* M& U  R
  6. // 1 创建画布' ~/ g+ t+ y  I1 |6 L0 @
  7. $image=imagecreatetruecolor(100, 30);" w! p6 S) J4 O* S9 [
  8. // 2 画一个矩形并填充背景颜色为白色. N7 M9 w" B$ R+ l
  9. $color=imagecolorallocate($image, 255, 255, 255);
    ( h- H% v2 T3 P( `4 \/ O5 X
  10. imagefill($image, 20, 20, $color);+ h( ^1 \5 |8 x5 @" g6 B; L
  11. // for($i=0;$i<4;$i++){9 d! M- {! Z% P) I3 m* F/ W  e
  12.     // $font=6;
    ! B, s4 N6 U  ~2 y% t# h/ o
  13.     // $x=rand(5,10)+$i*100/4;/ b* k# @2 @$ _- W$ i: @
  14.     // $y=rand(8, 15);3 {9 Z7 d" N/ p0 |5 R
  15.     // $string=rand(0, 9);
    & `' p! E! B4 \3 O0 @
  16.     // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));
    & Q+ c0 |7 s8 p2 F. t% Y" [, B5 F4 l" o
  17.     // imagestring($image, $font, $x, $y, $string, $color);
    8 S% y" O0 H3 B4 K7 N* ^" p( L
  18. // }! p' O+ G* V6 w; Y3 d8 _
  19. $code='';, |, Y- _: U9 }& g$ M; Y
  20. for($i=0;$i<4;$i++){
    8 s- j+ k* g, |  i! ?0 a
  21.     $fontSize=8;
    : F2 T9 `8 T# U8 e1 B  ~
  22.     $x=rand(5,10)+$i*100/4;
    % }3 }% y  q# M5 O1 ^2 V
  23.     $y=rand(5, 15);
    $ o- V5 s. k) X0 z! a, ~
  24.     $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';1 v, c/ l6 I4 S
  25.     $string=substr($data,rand(0, strlen($data)),1);
    " R  j, m2 c, ~% |2 b
  26.     $code.=$string;
    2 U* h$ p9 e# P
  27.         // 3.2 加载自定义字体
    # d) S6 V8 [. @( t4 M
  28.         $font_file = '1.ttf';' x3 d/ r  w6 e; U2 H- p
  29.     $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));
    & b1 S  d$ X8 u; n0 B& v
  30.         // 把文字填充到画布
    ' y9 f" c8 m# u( u. }' `! [# f
  31.         //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);* s; {  p/ n$ P  z# Q- V
  32.     imagestring($image, $fontSize, $x, $y, $string, $color);
    + i- M) y9 Q5 s# e
  33. }
    + o* E7 b8 r+ x7 l
  34. $_SESSION['code']=$code;//存储在session里! P+ D. s1 R& A  S+ z8 X' w
  35. for($i=0;$i<200;$i++){
    # ~" X, E& r8 d( Z4 _
  36.     $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));
    8 L$ c! _- Y0 n4 \
  37.     imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);
    ; e7 b% s) v3 X( [
  38. }+ U  g) A' {5 N: J! m$ g! G" Z9 F& N
  39. for($i=0;$i<2;$i++){6 b: u1 h" x# M1 f
  40.     $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));/ R( Q- r. f: |. F# d' p5 z" T, x/ M
  41.     imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);
    " k! d5 D+ q* q3 h
  42. }
    2 L/ t# n8 g  x6 u
  43. imagepng($image);
    ) c$ F" O2 i- y* H; v2 h
  44. imagedestroy($image);8 X2 n$ ?+ R- J+ U/ G& X/ c

  45. % \" H. V4 s2 y+ y2 _& f
  46. ?>
复制代码

: A3 n% I' ?/ Zform.php* k0 H" c0 V  |& o
  1. <?php9 R: T% P* O7 V" H
  2.     if(isset($_REQUEST['code'])){+ z4 W5 D2 K0 y6 r1 q) n) R7 X
  3.          session_start();
    # g. W: o' z# a% l* m4 d$ Y+ Q  V2 X
  4.         if($_REQUEST['code']==$_SESSION['code']){
    - F) P  C9 D3 E6 a: ]" c
  5.             echo "<font color='red'>输入正确</font>";3 T" i1 K& [3 r( i0 o! ?
  6.         }else{
    3 f' Y; P/ f, P& f% A- k) V# `: v
  7.             echo "<font color='red'>输入错误</font>";
    ! u1 F  Z5 i5 S" D) t
  8.         }
    ! ^0 F  f* W: i) p2 z6 C1 `
  9.     }
    6 t, r& D( Q" U% o
  10. ?>
      l- s. Z9 x8 b* T! e9 A( {
  11. <!DOCTYPE html>
    , E% t7 X. |8 N6 a3 q1 j! ?
  12. <html>
    2 x4 _, Q. K" _5 m+ l/ S
  13.     <head>
    4 b( ]& l) {" c' \" e' m
  14.         <meta chartset="UTF-8" />
      C" ~7 i, K4 [! s" h$ H! F4 s4 f7 M- C
  15.         <style type="text/css" rel="stylesheet">
    , Y6 t9 c8 b$ y& O, T: S
  16.             a{
    , X% k/ v8 w* B  v7 Z# O' Q; _, x
  17.                 text-decoration:none;) Q1 L/ G  Y3 Z
  18.                 font-size:30px;
    & P6 h. R0 I$ a7 }
  19.                 color:blue;
      }. K5 o2 O3 M5 l% ], o
  20.             }
    - v* c+ \# c2 J0 C
  21.             a:hover{
    ; \' \2 ?" C6 {$ p* e$ b
  22.                 text-decoration:underline;
    9 P4 h, T# Q0 f8 z: K
  23.             }
    , y7 v# {1 ?! d6 ^+ |* u9 l
  24.         </style>4 |8 Q: D( h+ c/ |7 u  f8 [
  25.     </head>
    , [+ g( R" B4 K% }: ~) X
  26.     <body>% H6 Y1 a/ g, ^
  27.         <form action="form.php" method="get">
      U( l3 \4 Z* K4 Q) E# V- O! ^$ J3 F
  28.             验证码:  <img id="img" src="index.php?" onclick="changeCode()"/>, S& L* d5 m3 n. `
  29.             <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br /># ?) |3 m' x5 `6 \9 e; ^0 L
  30.             请输入验证码: <input name="code" /><br />
    7 Q! a2 f" E3 n9 q7 r
  31.             <input type="submit" value="提交" />0 t1 B$ e7 _0 T5 l3 r
  32.         </form>6 ?" M! O7 y6 z, G7 f& J6 I5 j2 Q7 I
  33.     </body>
    ' O9 J" P! A/ Q0 r
  34.     <script type="text/javascript">7 @# s, _! p+ C
  35.         1 S; s1 a! f' T7 ^6 k! J
  36.         function changeCode(){8 ?' {9 E: S% y  Z- `6 Q
  37.             var img=document.getElementById('img');1 s7 _( f4 c( A$ i# P+ @
  38.             //img.src='index.php?r='+Math.random();% P' }8 H; U4 h7 a  S) u
  39.             img.setAttribute('src','index.php?r='+Math.random());$ M0 [* W3 I, n4 L( j
  40.         }
    % s3 K* L0 v5 l
  41.     </script>  W: A, ]& M; }& w7 D! D
  42. </html>
复制代码
2 x# N) o$ W6 @: T+ {' q

5 z- z5 r, ^! ~& v
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2024-5-19 20:09 , Processed in 0.141353 second(s), 19 queries .

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