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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[php学习资料] 验证码

[复制链接]
跳转到指定楼层
楼主
发表于 2018-12-16 01:00:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
index.php1 t2 S' y) R% r! S
  1. <?php. q% u' t. g7 u8 m9 Y( ~# z
  2. session_start();
    ; a# h) Y- O' M) H7 N$ G: [* i
  3. // 丢弃输出缓冲区的内容 **6 O* S+ C; U# ^7 s# q6 y
  4. ob_clean();
    4 @; i$ r% h$ ?; l8 W! c0 _
  5. header ('Content-Type: image/png');) Q3 b6 H& J. e: `7 H) H
  6. // 1 创建画布
    " d1 }. i8 E5 R$ u  i3 X6 ~
  7. $image=imagecreatetruecolor(100, 30);3 j3 A3 N" l. O6 O' c# }
  8. // 2 画一个矩形并填充背景颜色为白色2 f2 ]  E( ]2 U0 j" Q8 |" ^7 a
  9. $color=imagecolorallocate($image, 255, 255, 255);/ q! ~  p- I5 J: p( _6 @
  10. imagefill($image, 20, 20, $color);
    . d8 F* ?9 }  l1 c/ `
  11. // for($i=0;$i<4;$i++){4 `- r" N' G/ l3 @+ ^
  12.     // $font=6;
    ; D' S" D( B, u0 U. v+ A3 J
  13.     // $x=rand(5,10)+$i*100/4;- Z: V9 t9 x9 U$ l# V3 Z
  14.     // $y=rand(8, 15);4 e3 U1 u; \' B7 Q0 \. x
  15.     // $string=rand(0, 9);
    8 I- ?! |2 w/ C1 c0 H
  16.     // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));
    * C' L* z& a( l3 x9 d9 v# q2 x7 k# V
  17.     // imagestring($image, $font, $x, $y, $string, $color);
      E$ s2 C0 N: X
  18. // }
    % b' p& R8 W' I
  19. $code='';
    7 b  k& K5 N& ~7 d' q
  20. for($i=0;$i<4;$i++){; `/ d0 `& \# o7 B1 J
  21.     $fontSize=8;
    " I7 h( p: Y, a5 f1 U% a6 A" q' G$ c/ i
  22.     $x=rand(5,10)+$i*100/4;
    . |4 i' [- h$ @9 G5 k/ t
  23.     $y=rand(5, 15);: ^7 I: N4 P; O# s  [' i
  24.     $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';; ~. ]& r( K. F
  25.     $string=substr($data,rand(0, strlen($data)),1);8 i& C$ `9 `( S" a& k. h, j* u9 a
  26.     $code.=$string;
      F4 I* e- z( h8 O
  27.         // 3.2 加载自定义字体  I- ?5 Y7 X: c5 ~
  28.         $font_file = '1.ttf';
    $ t8 z* s; R6 y; f( n  O- W) h
  29.     $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));
    + c' E& {& V/ P, i8 ~; X7 g, p6 ~
  30.         // 把文字填充到画布
    ' K7 G& g4 ~* k/ r$ W7 [0 i
  31.         //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);3 m8 L6 j& ^1 h& m
  32.     imagestring($image, $fontSize, $x, $y, $string, $color);
    5 e9 Q$ \& T/ B0 L9 m( k
  33. }( O% U! H; o- S& }* b
  34. $_SESSION['code']=$code;//存储在session里( {# U5 z. V! E
  35. for($i=0;$i<200;$i++){
    ; G! O+ [# W' t6 B$ H5 j8 M
  36.     $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));7 k+ E# `+ o6 s3 }( ?4 Z
  37.     imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);9 B* ~5 F* j) z
  38. }  O3 M4 n) e! f0 C) C9 ~3 E% i8 q
  39. for($i=0;$i<2;$i++){
    " u3 z' N- z* n. g0 i! J- {
  40.     $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));9 v+ T( P* P) `% h$ P5 ~+ N
  41.     imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);6 u1 s0 Q/ Q  `+ A' L
  42. }
    0 Z8 A2 d" t) Z7 q$ m
  43. imagepng($image);
    * W! ]! P" B5 Y
  44. imagedestroy($image);
    : X+ o1 G/ \9 D

  45. & E6 @0 ~6 y& Q9 y6 T# i
  46. ?>
复制代码

/ |) B+ F# u' T5 a! Xform.php
" |* X4 W$ L0 h, g% [9 Z3 c, L+ R" j
  1. <?php
    - a* x: L( t) ^' m0 {! J  s. X) J
  2.     if(isset($_REQUEST['code'])){# \; K) s2 E2 l1 m" a* I
  3.          session_start();
    / {& ?$ r3 p, _
  4.         if($_REQUEST['code']==$_SESSION['code']){
    : e1 g- M+ l; ^* ?* l( y2 E; u- F1 L
  5.             echo "<font color='red'>输入正确</font>";
    - z) Y+ k; c8 [3 c" U
  6.         }else{) S2 d" ?6 R: O( W" C' Z; n: D# y
  7.             echo "<font color='red'>输入错误</font>";! ^! W% C# e: [0 l5 f2 {% H2 P" P
  8.         }6 O' j, _4 |6 h7 Z& u2 p
  9.     }
    , L8 [4 d0 u: C2 `$ s: ^
  10. ?>. z. y) `0 ?9 D
  11. <!DOCTYPE html>
    / `  ]. Y# [" \# H7 x' R0 c) Y+ X
  12. <html>6 H! v0 Y) X$ V- e) p8 e: x# R
  13.     <head>
    1 m1 x; h* `  O2 N# [/ a
  14.         <meta chartset="UTF-8" />% M$ v5 `1 h! t( _, K  S  G! [- L
  15.         <style type="text/css" rel="stylesheet">- r+ G$ k0 t2 V7 x+ h) `' B* V, ^
  16.             a{9 H  ?9 f  g. H6 \7 Z' T
  17.                 text-decoration:none;
    * G, d! ~2 _+ z) f
  18.                 font-size:30px;
    . W: S9 l% e7 B. ]9 x1 R
  19.                 color:blue;
    * P2 \! i! L* J3 }% x
  20.             }
    3 v$ `8 m: b  ?- p- }
  21.             a:hover{
    7 n, P& E$ G% N
  22.                 text-decoration:underline;
    2 ^7 G7 ?" g6 O' p7 N* V
  23.             }
    ; @- L7 s/ W: J( F* x
  24.         </style>4 i% t& I5 K5 G: Z7 z
  25.     </head>9 w0 `4 ^9 t: }5 n& f3 i$ m* Z
  26.     <body>" L$ J0 ^( `5 s2 X7 i* c: m$ c
  27.         <form action="form.php" method="get">, T! [5 Y6 C4 l
  28.             验证码:  <img id="img" src="index.php?" onclick="changeCode()"/>, N* z; S, c9 T# ~* S
  29.             <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />* |+ D: r* b/ y0 V4 L) ~' s' ^) w
  30.             请输入验证码: <input name="code" /><br />
    5 R) ~5 |' f- v2 o
  31.             <input type="submit" value="提交" />) @; L+ y' p7 s; u% m
  32.         </form>2 ~2 T) X6 {8 I( @9 o
  33.     </body>1 @) C7 J: U- w
  34.     <script type="text/javascript">
    2 i  \6 O$ |7 v& X
  35.         
    " w6 M$ j% Q) T5 K! t/ [4 w. W4 F
  36.         function changeCode(){
    ( F! Y' h, A% V; y: l- u" ]# x
  37.             var img=document.getElementById('img');1 W6 z) b! u- x3 E) o
  38.             //img.src='index.php?r='+Math.random();7 w8 l! N7 X: c
  39.             img.setAttribute('src','index.php?r='+Math.random());
    6 h' w2 F/ a" a: z3 |0 @
  40.         }
    8 N+ d* t& E1 N5 u8 l3 e9 `0 P: q
  41.     </script>* r3 v: u& B* t1 N! Q4 o' l
  42. </html>
复制代码

/ j$ _; G4 c  {; R+ Y* L% |# E/ w' t. q1 R/ G5 x' e, e9 p- c4 P* J
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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