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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[php学习资料] 验证码

[复制链接]
跳转到指定楼层
楼主
发表于 2018-12-16 01:00:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
index.php. j1 d2 [1 w/ W1 s$ i: J
  1. <?php
    ; b4 F6 R" \: l- y
  2. session_start();. Y1 p. r. d  Q, m
  3. // 丢弃输出缓冲区的内容 **  D5 j" [: j% v$ G! _4 b: l& Q
  4. ob_clean();
    . g; X3 C1 f: ]- N- Z9 @" P5 @( Q
  5. header ('Content-Type: image/png');% ?2 r* F; g9 p2 u* q
  6. // 1 创建画布
    / `. F# F" e( ?* k) D. h
  7. $image=imagecreatetruecolor(100, 30);
    * K3 @* u( |1 @7 w1 B% ~2 a
  8. // 2 画一个矩形并填充背景颜色为白色
    - S8 m2 E. s' }/ v2 p
  9. $color=imagecolorallocate($image, 255, 255, 255);
    1 k, ?+ {$ U! F$ V) n( [
  10. imagefill($image, 20, 20, $color);+ Z3 a- F1 f7 x" f% b
  11. // for($i=0;$i<4;$i++){
    0 Q6 a7 f& }/ }2 t. I# Z- m0 e
  12.     // $font=6;
    1 p; {- ]' g/ B% G1 L0 K; n8 m1 ?- L
  13.     // $x=rand(5,10)+$i*100/4;
    ' U9 k) Q) c2 ?( w) `" r4 m3 ?3 z
  14.     // $y=rand(8, 15);
    , a! U2 ]4 h- U% _
  15.     // $string=rand(0, 9);; B# L9 N- I8 I$ u8 a. `
  16.     // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));1 L0 a% S, i0 W( S
  17.     // imagestring($image, $font, $x, $y, $string, $color);! X$ S: p# K; Q
  18. // }
    ( F1 _* z  v- c2 J% X
  19. $code='';; c& M& |+ P% O4 y
  20. for($i=0;$i<4;$i++){
      D% ~6 C4 M! T( Q; f# c
  21.     $fontSize=8;4 o. m  C/ P' K
  22.     $x=rand(5,10)+$i*100/4;
    8 a, x9 S3 ?* z6 `. M
  23.     $y=rand(5, 15);
    # w2 U8 p& e/ t& x) A
  24.     $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';' \' M+ l4 T; f* g5 j9 I  j  z
  25.     $string=substr($data,rand(0, strlen($data)),1);
    ; N. E4 `/ \0 \4 {2 X: R" p- e8 G
  26.     $code.=$string;
    0 o: l7 z& N/ j6 l2 Q+ S, n9 h
  27.         // 3.2 加载自定义字体
    ! G( V+ n1 M$ N/ g0 z% E2 }
  28.         $font_file = '1.ttf';
    ; ~  w" A. E5 o( W' k0 P
  29.     $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));
    % m/ s( F) M! h3 y5 [
  30.         // 把文字填充到画布
    / G# o5 t; S, y; e' R5 i) _" h. q
  31.         //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);
    # f' k9 o9 O% S) s6 Z
  32.     imagestring($image, $fontSize, $x, $y, $string, $color);
    5 ?$ {* f$ Y4 E; l9 [: G
  33. }
    9 a% K% @" c! D) ]
  34. $_SESSION['code']=$code;//存储在session里9 J/ g4 T. n% z8 E, L, q
  35. for($i=0;$i<200;$i++){5 B# S; {7 j' `) o
  36.     $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));
    ) l, w! K+ a* F  _8 g; U, R
  37.     imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);/ c: ?5 V8 g+ u1 a- Q: }
  38. }
      ^2 C$ I( i3 d+ S0 e6 H
  39. for($i=0;$i<2;$i++){( V) i. M, X, n$ [1 z
  40.     $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));' ^: c9 o2 d' Z# _
  41.     imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);/ N+ w% @7 v" u; I, n3 I
  42. }
    1 l& V4 i$ l* K6 w  j% U7 x* X
  43. imagepng($image);
    " M9 V+ A- B! b3 ]6 u! o( |
  44. imagedestroy($image);
    ; v/ d, g) x- j! s

  45. ; s' ?! X1 w) o2 ]7 p- [
  46. ?>
复制代码
, {& j5 ]& S! i1 ]
form.php
% t( W8 g' h5 p. P* U: _
  1. <?php
    ; ^9 ^4 i6 K# p, z. i& Q
  2.     if(isset($_REQUEST['code'])){0 r; T5 d+ W4 d4 [3 o
  3.          session_start();" B: O7 M, S, `( B7 S9 F; b+ M; g) H0 l
  4.         if($_REQUEST['code']==$_SESSION['code']){, }1 ~6 x8 q+ }2 D" ^% ^
  5.             echo "<font color='red'>输入正确</font>";8 d* ?$ Y) Z2 J0 q2 X0 C
  6.         }else{& t: W# G' i; ~
  7.             echo "<font color='red'>输入错误</font>";
    " ?1 z9 }/ P9 F& V7 B! Q0 w
  8.         }" w8 m5 W4 b) o7 i
  9.     }
    5 O# E0 j9 q; Y/ I4 C% j$ M
  10. ?>
    6 ~5 F4 a( U# |9 x& S4 t1 ?0 a( v2 m1 R
  11. <!DOCTYPE html>
    4 E: U+ u/ h( }+ [8 {
  12. <html>0 v" U7 G, p$ S/ T3 q; g# q' w
  13.     <head>
    4 s  j' b% T: W- {. ~
  14.         <meta chartset="UTF-8" />2 X0 v; e. O. z6 y
  15.         <style type="text/css" rel="stylesheet">
    ! f* |' F: I; v, B
  16.             a{4 p: G8 \% n" d; W0 Z
  17.                 text-decoration:none;
    " \/ D2 O9 b7 D# ], s
  18.                 font-size:30px;/ l8 N3 n  U9 x) `
  19.                 color:blue;
    9 |6 R8 S( X; Q8 M
  20.             }5 }) D% ~4 p5 k4 R8 J
  21.             a:hover{
    - e+ Y: @- D" O/ a2 ], ]4 q
  22.                 text-decoration:underline;
      z' U" I0 z- M& V9 w; [' L
  23.             }
    ! D8 E) Q% P+ X7 c$ v
  24.         </style>( R0 i" x( \. s
  25.     </head>: }! I3 D# }4 M6 Q
  26.     <body>- K* A& d3 d; n  S: {! Y8 f5 R$ J3 s) Q
  27.         <form action="form.php" method="get">
    " G3 }/ s4 s) g( {5 l7 Y) |
  28.             验证码:  <img id="img" src="index.php?" onclick="changeCode()"/>
    # R/ b( s* s' ]5 z3 p4 K
  29.             <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />
    . c# s& T3 T5 U$ b5 `
  30.             请输入验证码: <input name="code" /><br />
    * f8 ]1 N/ L& N7 ^* Y5 e; b7 w+ h
  31.             <input type="submit" value="提交" />
    2 c$ ?; ^- b0 c3 X1 p
  32.         </form>3 u0 x% i/ d! |" h, v- S; x
  33.     </body>2 L5 G) k: w+ }" A* l2 K
  34.     <script type="text/javascript">" z6 i9 n% m" Z2 y6 ?
  35.         
    5 A- Y" ]" O: N% ~% H
  36.         function changeCode(){1 B$ O" f: o6 R2 w5 R+ ]4 D8 V8 Y2 E2 {
  37.             var img=document.getElementById('img');# y/ I7 |' g& v4 r6 h" {, T/ g: |. y
  38.             //img.src='index.php?r='+Math.random();8 f  a; S$ e$ f% j+ j. X% n, ^
  39.             img.setAttribute('src','index.php?r='+Math.random());
    # ]8 R, s" o5 [, y
  40.         }# e8 c2 w0 U" G$ h. ^! `3 \: \8 I
  41.     </script>- X9 k7 w/ w, o4 p
  42. </html>
复制代码
: T) {% \5 e) \8 H

- D$ j7 i- ?8 f( m! O
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-8-4 13:12 , Processed in 0.054219 second(s), 19 queries .

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