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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[php学习资料] 验证码

[复制链接]
跳转到指定楼层
楼主
发表于 2018-12-16 01:00:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
index.php; O' ?1 K$ M- K* P
  1. <?php
    " y% ?& @; R7 o- w) @9 F. a
  2. session_start();
    . p. K( O2 F0 `1 E8 v/ ~, s* o1 o% Z
  3. // 丢弃输出缓冲区的内容 **
    1 _% k- l8 @0 C' A$ T2 d2 A
  4. ob_clean();
    & g6 Y- N2 ]- F% r, O
  5. header ('Content-Type: image/png');
      d. x/ ~8 D" y" n2 X6 f' T1 i
  6. // 1 创建画布
    % Q8 E$ V& l- O
  7. $image=imagecreatetruecolor(100, 30);
    ) q; y) Z. z, X6 S; A
  8. // 2 画一个矩形并填充背景颜色为白色2 G- _5 E7 }9 C% D6 c7 e1 m# F7 H) u
  9. $color=imagecolorallocate($image, 255, 255, 255);
    6 R  s' i/ i4 ^8 v; X+ A9 A% k
  10. imagefill($image, 20, 20, $color);
    ) Z' U1 a# V8 f
  11. // for($i=0;$i<4;$i++){$ M- c) ^, l0 Q3 ~# A  t
  12.     // $font=6;
    ( j' e  T5 c' T+ u
  13.     // $x=rand(5,10)+$i*100/4;
    * {! s1 W; C3 Y' V" v
  14.     // $y=rand(8, 15);
    - F( s; R0 V" O8 i
  15.     // $string=rand(0, 9);+ _2 m0 u$ m; n; ]9 a8 B6 q+ c
  16.     // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));
    * L/ q; ?3 w  O. o
  17.     // imagestring($image, $font, $x, $y, $string, $color);
    % [* t; D+ D% k' Q
  18. // }- ~' g3 F0 l/ I; D+ o9 I
  19. $code='';
    ) T" w; E7 p: Q; t
  20. for($i=0;$i<4;$i++){. V) P. l4 Y0 K/ g: m
  21.     $fontSize=8;
    0 ?$ u# {  _/ S$ Z! G
  22.     $x=rand(5,10)+$i*100/4;; ]- Q5 v. q4 _# `% u( o( Y
  23.     $y=rand(5, 15);! [* w- n6 P' D1 Q/ t
  24.     $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
    : H) j* p7 k/ @: h
  25.     $string=substr($data,rand(0, strlen($data)),1);
    : h' }0 u3 @  b5 {4 J& y# y
  26.     $code.=$string;
    8 |8 P- F% k1 @& F% @1 u
  27.         // 3.2 加载自定义字体8 `. q$ V% v$ z8 w* Q/ x
  28.         $font_file = '1.ttf';1 v2 \) `8 Y; b: |8 f
  29.     $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));" h8 m; G/ E. `0 X% P) }' D
  30.         // 把文字填充到画布; w( V7 U) S' l& ^
  31.         //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);
    % `7 l% \  A, j
  32.     imagestring($image, $fontSize, $x, $y, $string, $color);" k% _5 u6 r5 X; \& U- R5 W
  33. }0 N- x6 D* C4 x' E7 y3 z
  34. $_SESSION['code']=$code;//存储在session里4 \, D- ~5 ?) b
  35. for($i=0;$i<200;$i++){
    7 H# x$ p1 R. @1 v% r5 s
  36.     $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));. S$ u# _: N: r2 O3 v8 m
  37.     imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);& c" D/ ~1 @6 O
  38. }
    9 i7 [. Y! h& Y" k
  39. for($i=0;$i<2;$i++){
    5 ]2 I, K" h. v# D
  40.     $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));$ p4 D+ D; O7 D# w0 t0 G
  41.     imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);( x  V5 i/ ~( P  j4 h4 |+ S" r. M
  42. }
    & n$ b8 M/ b9 w4 A$ R
  43. imagepng($image);
    5 j0 y! Q7 E: L
  44. imagedestroy($image);
    * _; [  x6 `; F/ b1 F4 k: M1 [

  45. % y8 X: n2 Q' P5 D
  46. ?>
复制代码

4 h* ^$ t  F2 Zform.php
7 U8 E7 o( P/ @& C: W
  1. <?php% j, J( O7 H, ~5 J
  2.     if(isset($_REQUEST['code'])){
    2 N7 o9 [  n$ o8 L* [6 E, H, v
  3.          session_start();0 Q( W6 F, W7 N, T
  4.         if($_REQUEST['code']==$_SESSION['code']){. @0 b8 O% q! K7 C6 I% u
  5.             echo "<font color='red'>输入正确</font>";. D3 Y) g) M4 }; Z% v; n1 I6 Z
  6.         }else{
    2 l; r- _8 g- S' Q8 g" |$ ^
  7.             echo "<font color='red'>输入错误</font>";, A  F& _8 K4 h5 X; \0 j0 m
  8.         }
    * \8 B. H1 V9 Q
  9.     }% U! q1 K1 X4 u& P( j0 d" X
  10. ?>3 Z( T  f. B/ A2 j- Y) ^% A
  11. <!DOCTYPE html>+ p& A1 I- V% g& H9 S5 K. v  a& W; l8 H
  12. <html>
    $ t) u3 i( Z4 K, Y
  13.     <head>4 j9 |' e) v; d3 I8 [+ {2 ]5 J% u
  14.         <meta chartset="UTF-8" />
    & O1 b8 }. b% ^$ h) X
  15.         <style type="text/css" rel="stylesheet">/ o% ?, _5 w6 F8 s
  16.             a{
    + J+ r- B2 O/ G9 Z' |
  17.                 text-decoration:none;# m- l2 `1 o8 G$ g( o8 x9 k( t
  18.                 font-size:30px;
    : W; w* E7 t0 z& Q8 F3 v
  19.                 color:blue;4 a* C& [9 `! f, M$ O6 e4 Q9 j" j
  20.             }
    9 f. C* }, C2 D$ D! w- v* _
  21.             a:hover{
    - j. J# G3 V* ~
  22.                 text-decoration:underline;8 g3 M# {0 h7 @# K& l& U$ P
  23.             }. @# }2 J! e$ M. C: H: ]2 c) V
  24.         </style>' _( e# x% z' o8 t1 U
  25.     </head>  M! E' b6 {5 y; V  G% I
  26.     <body>
      G0 F& X* e$ A# [% k8 P" Z7 g
  27.         <form action="form.php" method="get">
    3 G) \+ b, Q8 i6 `
  28.             验证码:  <img id="img" src="index.php?" onclick="changeCode()"/>
    6 [( }  l7 B0 H/ w5 O
  29.             <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />
    ! {+ r( d  s" N
  30.             请输入验证码: <input name="code" /><br />0 l; n8 k" g7 |; S7 u4 h# `$ W# T
  31.             <input type="submit" value="提交" />. X; n# d' F' V9 s
  32.         </form>
    5 L7 Z8 A( ]0 h5 Y
  33.     </body>
    % `  Y' q( F4 Q3 {7 ~% s: Z
  34.     <script type="text/javascript">
    5 i- Z& |! Z2 z4 y# b
  35.         . N4 R/ h9 x/ X: M1 N
  36.         function changeCode(){
    8 E) `& ~8 P8 d0 L' V
  37.             var img=document.getElementById('img');0 k  C5 ?2 j/ @
  38.             //img.src='index.php?r='+Math.random();
    ; Q& M2 u6 S0 f6 e
  39.             img.setAttribute('src','index.php?r='+Math.random());& r: s5 ]+ F6 s! ]! ~: i- S# \
  40.         }
    8 J  g% `, F9 _0 o
  41.     </script>
    % {" O0 {8 A- _7 [2 E2 u
  42. </html>
复制代码

! N3 L, S7 P1 ?2 F! @0 ~1 `; x0 E* m6 S; L8 l7 y. J% d
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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