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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[php学习资料] 验证码

[复制链接]
跳转到指定楼层
楼主
发表于 2018-12-16 01:00:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
index.php1 F0 u0 u: [' n
  1. <?php0 q, C$ Q8 x. J
  2. session_start();# d, B" d# q$ q1 j+ Z$ @1 N' y
  3. // 丢弃输出缓冲区的内容 **7 S$ d. Y) k; y. v, d5 m6 N
  4. ob_clean();: f# ^7 q8 B; F+ G5 c
  5. header ('Content-Type: image/png');
    * i1 L: v9 P! {+ j7 Y, M
  6. // 1 创建画布
    ' x+ g- C! g+ v8 R4 f/ |3 Q
  7. $image=imagecreatetruecolor(100, 30);: X. _+ O4 F2 [% u. F2 ?
  8. // 2 画一个矩形并填充背景颜色为白色3 s: [7 G0 r/ c  N
  9. $color=imagecolorallocate($image, 255, 255, 255);
    4 L$ K# w7 J: \$ G5 X( }
  10. imagefill($image, 20, 20, $color);
    7 C7 x" J8 G! v) O. a
  11. // for($i=0;$i<4;$i++){+ @3 A8 h0 U$ X) _
  12.     // $font=6;% w8 _8 A) N- o& x
  13.     // $x=rand(5,10)+$i*100/4;! L; c! a+ {0 Q$ h1 r3 H
  14.     // $y=rand(8, 15);0 i4 t) ]  b, c. g3 x
  15.     // $string=rand(0, 9);
    3 r* L; l$ ]7 i) e9 n
  16.     // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));+ n- M& s! ^( S6 S
  17.     // imagestring($image, $font, $x, $y, $string, $color);
    # q2 P: _$ c$ s" S
  18. // }* p/ n- ]0 n7 p
  19. $code='';
    $ y. L1 {7 G+ ]2 w4 U  ~- T
  20. for($i=0;$i<4;$i++){
    7 X! ?( O* ^0 z
  21.     $fontSize=8;
    6 x9 X1 K# ~8 l
  22.     $x=rand(5,10)+$i*100/4;
    ! }; l7 W7 p0 I( A
  23.     $y=rand(5, 15);5 k- O1 ~& v( F5 Y7 {7 H: l
  24.     $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';5 s% v. H0 l& @! ]
  25.     $string=substr($data,rand(0, strlen($data)),1);
    - w  J0 t# P& H" }% {
  26.     $code.=$string;) P; o0 I; I/ J2 `  g; z8 i, }
  27.         // 3.2 加载自定义字体" L) B6 j% K: u  R& N. Y7 Y6 A' x- i
  28.         $font_file = '1.ttf';
    , |2 D3 R' O+ D  `5 b
  29.     $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));0 k( Q# j; M7 L$ M# w: N; }
  30.         // 把文字填充到画布, h7 v- R( ^; W; |% e5 L) a
  31.         //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);' s  s4 a5 [4 G# p5 y# p
  32.     imagestring($image, $fontSize, $x, $y, $string, $color);% V; L% w6 J; F3 K9 g
  33. }/ `  y: L- h. I' h
  34. $_SESSION['code']=$code;//存储在session里
    , d9 T* A! U) B; I
  35. for($i=0;$i<200;$i++){
    8 O0 \& S$ u$ G  |2 _- v1 B  E
  36.     $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));5 \  Y1 q- Q; p6 ^4 P4 E! W% K
  37.     imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);
    4 a2 M1 p( y2 A1 y
  38. }) G: I+ w% Z6 K5 q9 l( ?
  39. for($i=0;$i<2;$i++){
    7 a2 D5 O& E  W' T0 y- m
  40.     $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));& x+ k) @7 e& p+ z) M5 r, a
  41.     imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);) n0 g5 A, |; K* u
  42. }0 Y; n/ H$ x7 B; x4 e; a7 m
  43. imagepng($image);
    ' K3 G, A+ W# V' s0 Y2 [
  44. imagedestroy($image);" x+ G8 h9 G; e! z1 \& n; J
  45. + ~0 Z: n4 [1 g5 V' f( k# {
  46. ?>
复制代码

/ ~$ s* [. U& yform.php
) A& J: T8 }; K( y& M
  1. <?php4 y( h- Z7 k) j9 W
  2.     if(isset($_REQUEST['code'])){" i+ ~3 p( z2 H! m& \
  3.          session_start();0 a  |# R; F# W! K" q
  4.         if($_REQUEST['code']==$_SESSION['code']){4 T2 r  R9 L' q9 `6 d
  5.             echo "<font color='red'>输入正确</font>";
      o# _* a# T% Q: Y; k  l( D. P
  6.         }else{* z* D6 N. Q8 }. Y4 Z6 Q) H8 l
  7.             echo "<font color='red'>输入错误</font>";6 l/ J( C, t8 W5 o7 |) p
  8.         }
    - I( u' V& y( ?" ]: U* ]3 O# i
  9.     }8 j7 k; ]3 N  b
  10. ?>
    6 U$ T! e1 S) Y3 {
  11. <!DOCTYPE html>
    2 G0 y# n1 B! K; }
  12. <html>
    & U) a3 f$ z. k; N
  13.     <head>2 ^. G3 L0 @+ c" I! Y- s
  14.         <meta chartset="UTF-8" />
    ! o. J. @5 _4 B, g0 F" B
  15.         <style type="text/css" rel="stylesheet">5 g" ^" s; A# L) |8 ^
  16.             a{/ |( \/ ?9 y7 l
  17.                 text-decoration:none;
    0 t# o/ c- M6 W- ]
  18.                 font-size:30px;
    6 r* ^9 T$ y) e: M. A2 ?, a. e
  19.                 color:blue;5 b7 p! b" H" ^
  20.             }
    9 y" W$ z. D7 b+ y8 k
  21.             a:hover{4 p! k5 E) u' ^5 d3 m" e4 o! m
  22.                 text-decoration:underline;
    " ~4 ^# L: I) U% R. Y% d. R, A
  23.             }1 P, x9 r& Z' T
  24.         </style>2 Z2 G5 Q- \) B' `. |* X
  25.     </head>9 w8 Y. F, J9 ?! Q9 |$ ~
  26.     <body>
    ( R: b* w9 Q4 ^* v7 X1 w5 c3 N: E
  27.         <form action="form.php" method="get">4 \, v6 t7 u5 H( O! p
  28.             验证码:  <img id="img" src="index.php?" onclick="changeCode()"/>
    # G8 x7 y% [( o6 ~; m* P3 {
  29.             <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />
    4 q4 k: [( s+ R2 ^3 s& a8 R- w2 F
  30.             请输入验证码: <input name="code" /><br />
    " ~' m+ z7 E6 Z0 T7 Y. K5 H) N- J
  31.             <input type="submit" value="提交" />
    ; Q$ @7 p7 V! o7 v3 ]
  32.         </form>5 i. K+ ], ]. ^+ p( R
  33.     </body>. `  W0 S2 r. Q; V( }; J
  34.     <script type="text/javascript">3 O* C% G! @5 l9 y
  35.         , X4 m; ^+ t5 I7 v% M
  36.         function changeCode(){8 T+ m. f; w5 h) [5 `8 ?2 [- c: K$ @& m
  37.             var img=document.getElementById('img');
    + C! m. x1 ^2 q' Q; ?+ Q
  38.             //img.src='index.php?r='+Math.random();) i) |9 n; x: d% {0 K" A  o
  39.             img.setAttribute('src','index.php?r='+Math.random());5 O! W2 {7 I) o, h
  40.         }
    ( ~8 D3 a! `' p- q
  41.     </script>
    " a( ?$ H0 g3 V: W, T
  42. </html>
复制代码
5 {$ L' X" U. o

) K& V* T; K  ?- a% {% H
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-8-4 14:08 , Processed in 0.056241 second(s), 22 queries .

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