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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[php学习资料] 验证码

[复制链接]
跳转到指定楼层
楼主
发表于 2018-12-16 01:00:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
index.php% X8 ~' V+ t1 _( e7 c3 k: R
  1. <?php; z  o* T  g0 \  O, d+ c
  2. session_start();: W* O- [! p9 t3 @( o# l* R  x
  3. // 丢弃输出缓冲区的内容 **, q. ?: p  z% o( z$ \: B
  4. ob_clean();2 Z7 T3 u3 l9 C. j2 ~# Y
  5. header ('Content-Type: image/png');( [0 i& w: h' D6 O5 `
  6. // 1 创建画布
    ; C1 p- [. b0 M) q1 U- B2 J: o
  7. $image=imagecreatetruecolor(100, 30);
    & p% ?% L& B3 ?( W5 U
  8. // 2 画一个矩形并填充背景颜色为白色, B6 `* c4 I- R. x( ?2 P
  9. $color=imagecolorallocate($image, 255, 255, 255);
    + I; \9 m. X$ a
  10. imagefill($image, 20, 20, $color);
    * q* O) E$ G) b4 f1 @0 D! a
  11. // for($i=0;$i<4;$i++){
    ; f, o% _6 R: Z
  12.     // $font=6;
    4 s# A5 a1 C; b5 P# B, t5 I% n
  13.     // $x=rand(5,10)+$i*100/4;  x: V4 Y# h. X( _0 v' z
  14.     // $y=rand(8, 15);. Q/ G# B* I' ^
  15.     // $string=rand(0, 9);
    $ s( b, q, ]" ~2 c
  16.     // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));
    + T. z4 Z1 j* b5 f# ^
  17.     // imagestring($image, $font, $x, $y, $string, $color);; g( S/ S  S1 t/ n
  18. // }! i1 A  l9 x4 O: |4 E/ M
  19. $code='';
      P  w4 m* @" h+ a$ H$ ~
  20. for($i=0;$i<4;$i++){
    # D) [2 I3 i0 A' M" r7 f; e
  21.     $fontSize=8;3 K9 J1 u6 r: P) f
  22.     $x=rand(5,10)+$i*100/4;
    0 u+ D# o6 x" ~) y. v! o/ K
  23.     $y=rand(5, 15);! `. B$ f, i, V) v" h. z- e
  24.     $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
    % l  h, s/ D1 P1 i
  25.     $string=substr($data,rand(0, strlen($data)),1);# h5 j* K, W1 a/ j/ y
  26.     $code.=$string;
      _' W5 C9 ~: q5 _
  27.         // 3.2 加载自定义字体( ]6 y) f! o/ U. L) \
  28.         $font_file = '1.ttf';( r- I, I2 K9 T: |1 N) I5 B% [" J/ |
  29.     $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));
    , G  C& j2 S+ G7 Q! C, Z6 G
  30.         // 把文字填充到画布
    ) _6 f# b; B% n, i/ G. u( [$ d( E
  31.         //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);
    9 w. }2 d# ~% i
  32.     imagestring($image, $fontSize, $x, $y, $string, $color);
    & X7 J. C& ~& v3 ]) p; V
  33. }! G6 x( C( }6 m, C) y+ W
  34. $_SESSION['code']=$code;//存储在session里) @) T7 W) {# \2 Z  I7 O4 F  \' [5 L
  35. for($i=0;$i<200;$i++){% ^/ h1 x& \, U) T, R
  36.     $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));
    : D* I+ a- y, G) t
  37.     imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);
    9 l3 b5 t+ u' t9 d, }
  38. }
    3 f( M' H2 `) r4 I6 `8 G9 x
  39. for($i=0;$i<2;$i++){+ M# y% J9 T2 h. x* }) Q
  40.     $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));
    3 L; m$ p# G% O$ p6 g: m2 @7 U
  41.     imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);
    + u* u! n$ i+ N) I, p/ r$ m- b
  42. }6 I& u1 i6 ?/ q- t. ]/ ]* I' I( }
  43. imagepng($image);% r- W# z/ E7 ?9 V- y3 J& ?
  44. imagedestroy($image);
    ; M3 U" L4 _3 U' J
  45. 7 N3 A1 I" P+ U: x
  46. ?>
复制代码

9 s0 L8 x5 T) @4 Q- tform.php7 y/ o, ~. [7 d
  1. <?php7 `6 w# j0 n/ O7 G
  2.     if(isset($_REQUEST['code'])){
    ) Z$ P) ]4 c" F
  3.          session_start();! O- r. `+ j0 v  j
  4.         if($_REQUEST['code']==$_SESSION['code']){6 b9 g; X2 ^% Y2 ]
  5.             echo "<font color='red'>输入正确</font>";$ B) f8 t* V! @+ Q3 K
  6.         }else{
    " l; r& m+ K' y; \+ p
  7.             echo "<font color='red'>输入错误</font>";
    5 K" Y/ p! o& F( i' u* ~
  8.         }
    9 M  C% c6 I$ e+ _1 ?
  9.     }1 ~9 P+ Z4 p- R  V2 a! W* @. ?
  10. ?>
    4 ~$ E4 t& o) c+ v
  11. <!DOCTYPE html>
    4 I3 `; A8 ?( B/ l# J# O5 Y/ {
  12. <html>6 W- G& _/ e2 \9 x3 A: e# Q' T
  13.     <head>6 O5 `: i! y8 m3 ?. B+ {* P
  14.         <meta chartset="UTF-8" />
    / ?% l2 H1 W! B1 J/ L
  15.         <style type="text/css" rel="stylesheet">1 Q. i& G* t* Z. _
  16.             a{7 u& c% V# g7 {6 _
  17.                 text-decoration:none;5 e# ^* {8 d4 @' m6 u( v4 i
  18.                 font-size:30px;
    : I2 X* B. A) _$ n4 I2 }: E7 B
  19.                 color:blue;) y9 ^8 C: \  @# ]* i7 d/ u) D
  20.             }
    ( E/ N6 S1 o) S7 B+ \' Q
  21.             a:hover{! X. Y# U( Z  ]6 }* n0 t2 U) [6 D
  22.                 text-decoration:underline;
    9 A; I4 o4 Y! b7 r! G
  23.             }8 F' n8 P6 f5 ]% E
  24.         </style>
    9 D4 w& N- Z; c6 p
  25.     </head>
    # l* f" r4 J! [4 Y
  26.     <body>
    7 R% t4 A3 \% ]! T* z# @
  27.         <form action="form.php" method="get">
    2 X! U7 o5 z( ~: @
  28.             验证码:  <img id="img" src="index.php?" onclick="changeCode()"/>% d: U0 [2 m/ k
  29.             <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />3 i; b5 v" X# {  e
  30.             请输入验证码: <input name="code" /><br />
    0 ]5 w1 N* X8 D# g: }
  31.             <input type="submit" value="提交" />
    $ h) x7 b3 f7 V0 v' _. x4 E9 F
  32.         </form>* S/ F: l$ q2 R1 V6 z  ?
  33.     </body>) Q1 y1 V1 r% Z$ B; {8 E
  34.     <script type="text/javascript">% B5 V) I8 J) k& G( `9 i
  35.         
    , E! H, x( `$ j
  36.         function changeCode(){
    0 ?+ p) {% V% _1 R  @2 W
  37.             var img=document.getElementById('img');
    , q1 t5 a2 J, {
  38.             //img.src='index.php?r='+Math.random();7 Y* ~2 h1 _4 V; P1 T- N2 E
  39.             img.setAttribute('src','index.php?r='+Math.random());: _$ }4 C" p( s6 {$ A
  40.         }5 }9 V+ V! O0 Z) ^$ N
  41.     </script>( D% s  J' |, \$ s0 G" Z7 }- M" N! g; O
  42. </html>
复制代码

' {: ?' c2 m" p! i6 N
$ D$ l! J. |" Y8 w% o/ N) c
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-8-4 10:51 , Processed in 0.049929 second(s), 20 queries .

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