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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[php学习资料] 验证码

[复制链接]
跳转到指定楼层
楼主
发表于 2018-12-16 01:00:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
index.php
' H# r) K2 S& ?7 Y9 e: q/ G/ B
  1. <?php- Q6 X- u* X+ ~2 S$ a# j  F5 _! x
  2. session_start();4 W+ ?  F- e# \6 U
  3. // 丢弃输出缓冲区的内容 **# g7 K: P9 k/ N
  4. ob_clean();
    ; F7 q& g$ B/ I+ H/ Y; x& Q. A
  5. header ('Content-Type: image/png');( h" j  ]* Z8 [# f' S$ c! K' S& p
  6. // 1 创建画布; ~4 a& F. z) p  c" {
  7. $image=imagecreatetruecolor(100, 30);
    . `+ @1 W4 b8 [
  8. // 2 画一个矩形并填充背景颜色为白色/ a8 }, O$ j$ S; D/ f1 B
  9. $color=imagecolorallocate($image, 255, 255, 255);: j7 s7 p  W& f! y
  10. imagefill($image, 20, 20, $color);) G' Q* G" y# m; R
  11. // for($i=0;$i<4;$i++){
    ( B2 s# r$ ~1 x3 A8 o, x" X
  12.     // $font=6;+ L+ n: K9 N9 e0 v. L% }9 _
  13.     // $x=rand(5,10)+$i*100/4;
    7 u& @) q7 M4 M6 U( [& M% a# _4 {; P# K9 n
  14.     // $y=rand(8, 15);/ F/ ^$ z/ K5 S% V
  15.     // $string=rand(0, 9);, g8 \8 w( m# a8 }4 X
  16.     // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));* |* a: [1 S% j, ^: n% u% b& W
  17.     // imagestring($image, $font, $x, $y, $string, $color);# q- W6 ?; \: {! `- n
  18. // }/ f  |- e0 Y, u& Z) V$ o* ~2 u
  19. $code='';5 {, b6 }) ]0 d/ e( Y) i
  20. for($i=0;$i<4;$i++){+ u0 |. Y9 t/ S
  21.     $fontSize=8;! ~9 V6 }6 c" ^
  22.     $x=rand(5,10)+$i*100/4;
    9 R/ M- x& }/ l- L# G! X  e
  23.     $y=rand(5, 15);
    + n2 t+ z" x2 e" E
  24.     $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
    , A7 t4 U9 K" U7 h; U$ M0 v
  25.     $string=substr($data,rand(0, strlen($data)),1);! {4 Y  o  R# @1 `4 C
  26.     $code.=$string;8 l$ |  [2 c1 G0 H
  27.         // 3.2 加载自定义字体
    ) v) g" B8 J# f5 T% ~8 W$ [9 e
  28.         $font_file = '1.ttf';- }6 {7 m( Z  r- K+ K& n4 T
  29.     $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));
    ; |3 [0 z- T7 B0 `
  30.         // 把文字填充到画布
    7 s2 K& I8 g, t/ A" G5 l
  31.         //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);. s4 A1 E0 j6 |4 f
  32.     imagestring($image, $fontSize, $x, $y, $string, $color);4 o' D' S8 E5 k
  33. }7 I# L; o' t" _' V8 [0 l
  34. $_SESSION['code']=$code;//存储在session里# f5 P% _5 Y8 ^  K
  35. for($i=0;$i<200;$i++){$ l" _) a9 k5 r
  36.     $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));
    : v  ^  k$ `) y$ C/ p- A7 J3 i- j6 g: N
  37.     imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);
    " ~9 ~. I% [" K1 O2 T( D" c
  38. }& j: d' z. P, [
  39. for($i=0;$i<2;$i++){7 O: \3 J( z# F7 ~$ }: z, B8 G
  40.     $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));
    8 B4 Q5 q- A7 N( B8 K
  41.     imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);
    3 T/ U- A# m) K) j* c4 o
  42. }
    $ ?$ [2 z* ~! J* R! M
  43. imagepng($image);
    . u% e" Q8 w. t8 m( j  N) d
  44. imagedestroy($image);% s& r' P: c0 K! c, S7 A

  45. 8 U. Y7 x& R7 q% i- e, ]
  46. ?>
复制代码
/ o! Q4 A8 f' J, M+ H, \: q
form.php
: c; h6 h9 K& a8 ~
  1. <?php
    6 l" k" q& |2 m$ T
  2.     if(isset($_REQUEST['code'])){
    + C: i# }# E. T
  3.          session_start();
    $ X) p( b/ H" z8 O. k
  4.         if($_REQUEST['code']==$_SESSION['code']){
    3 I5 S: e" ?- s+ _7 |/ `) ^" g9 b
  5.             echo "<font color='red'>输入正确</font>";0 A+ L, b4 k# g8 s( S1 o
  6.         }else{
    9 R! |/ o1 Q! N' {7 Q/ d, R5 U
  7.             echo "<font color='red'>输入错误</font>";0 m: j; S- `5 {
  8.         }4 q- J9 E% P  Z% g
  9.     }) @1 U* E9 P. l2 q
  10. ?>
    - d! w4 b+ D: E2 ^3 l
  11. <!DOCTYPE html>
    - i1 ]( P$ d2 ^& \6 @
  12. <html>
    , \& N5 K9 K) G- Y0 `) a$ R$ A
  13.     <head>- {+ H7 @: x0 O
  14.         <meta chartset="UTF-8" />
    % P- E6 ^* q4 ^5 A' k) z
  15.         <style type="text/css" rel="stylesheet">
    ! r) C0 F  E% X1 y9 S) b
  16.             a{  q! T; n$ \- E9 ^2 X+ v1 R
  17.                 text-decoration:none;
    " l: U  _. K9 q8 L
  18.                 font-size:30px;0 y7 N; g7 x/ b5 u
  19.                 color:blue;1 w& h  e! H5 p/ A5 S: O6 G5 \
  20.             }  N( {$ L5 ]7 U% V' c6 G- f8 A
  21.             a:hover{) E% b' i, e% R
  22.                 text-decoration:underline;
    4 R, l. S& j7 |/ j! H- b% D
  23.             }
    ! x+ @& y9 ?* J& K/ c
  24.         </style>
    % J0 [5 `" t' V' y% R3 T2 a& ]
  25.     </head>( ]1 Z  _) e  b/ D' n' T
  26.     <body>
    9 r6 T- ~2 a# A/ ]- O$ N
  27.         <form action="form.php" method="get">" e9 m2 q  F+ Y; }3 P0 X4 _/ n
  28.             验证码:  <img id="img" src="index.php?" onclick="changeCode()"/>' D' ~) @$ t) {' O. ]( o
  29.             <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />
    * s2 x" T$ _% N0 I- ]6 t
  30.             请输入验证码: <input name="code" /><br />
    8 l9 [, c' S) O! W" G. v/ `- w6 Z  e+ I
  31.             <input type="submit" value="提交" />, W( S! [  Z+ j& H
  32.         </form>* w. x/ G. B" g
  33.     </body>
    2 m% j. z) N5 t
  34.     <script type="text/javascript">' I# B2 ^3 N- V2 L/ Z) E- w
  35.         - H- q# p+ ^, p7 B
  36.         function changeCode(){1 O8 ?3 w: o6 r9 O9 U. z
  37.             var img=document.getElementById('img');/ @" u) Z- R4 D
  38.             //img.src='index.php?r='+Math.random();
    % s) B5 W1 \3 _
  39.             img.setAttribute('src','index.php?r='+Math.random());0 e- [' `+ v: Y
  40.         }
    7 H% ]3 l6 J9 L$ {* I' z8 \
  41.     </script>
    $ f" ^  _! l& ?8 B
  42. </html>
复制代码

& c1 g$ @4 p. }( d$ P2 v3 x3 E" f6 Z+ k9 c4 o
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-1-30 16:32 , Processed in 0.049517 second(s), 20 queries .

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