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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[php学习资料] 验证码

[复制链接]
跳转到指定楼层
楼主
发表于 2018-12-16 01:00:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
index.php
; U1 W# z0 K6 L5 B8 _
  1. <?php4 v( M) u! s  f$ o/ g
  2. session_start();
    ! c# j) r9 t( ^1 |9 [2 D6 U
  3. // 丢弃输出缓冲区的内容 **
    ' ]9 T' r1 v3 R, y; z$ f
  4. ob_clean();
    6 _8 O7 A: u2 z+ s+ r5 H& p
  5. header ('Content-Type: image/png');0 y+ l; j2 b$ k) y
  6. // 1 创建画布
    + `" s1 V0 y; X: Q
  7. $image=imagecreatetruecolor(100, 30);7 a$ i# M/ n' h
  8. // 2 画一个矩形并填充背景颜色为白色
    ) Y/ A# s- C6 A6 ^& K  D6 v! E8 K3 w, z: t
  9. $color=imagecolorallocate($image, 255, 255, 255);
    ; E8 _$ a: K) p6 U+ D
  10. imagefill($image, 20, 20, $color);7 U4 X$ f( Z( P) [7 i
  11. // for($i=0;$i<4;$i++){
    ' m& g8 k3 i0 j7 K
  12.     // $font=6;
    1 k$ Z9 z' Z3 E* r4 c" a
  13.     // $x=rand(5,10)+$i*100/4;3 i; n5 F8 Q  ]4 |% I' w- z6 G
  14.     // $y=rand(8, 15);
    4 r# f1 L/ \" M  `% H
  15.     // $string=rand(0, 9);2 j5 b5 B# w9 S  x8 a$ D
  16.     // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));
    6 `/ E& E1 c# C% v# |' p; P& U
  17.     // imagestring($image, $font, $x, $y, $string, $color);0 D# ~/ ]' @9 S% s% [) {
  18. // }
    2 J$ H0 h3 b( B" l5 O% c
  19. $code='';
    5 H1 t. t3 ~. U0 T; a# e
  20. for($i=0;$i<4;$i++){
    $ T( i8 {7 r/ ~4 L
  21.     $fontSize=8;. V: z4 |, Q, J) g
  22.     $x=rand(5,10)+$i*100/4;" [$ z" a% t7 q: [5 b2 F
  23.     $y=rand(5, 15);
    , ?  u* L% b% k  M2 D. D) \2 @  c
  24.     $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
    $ o' @2 G8 Y2 x5 @% \9 [: }  |
  25.     $string=substr($data,rand(0, strlen($data)),1);
    & m; e- e0 Q: o  _- R1 n
  26.     $code.=$string;: A+ m6 s' h3 q/ M2 s( ]
  27.         // 3.2 加载自定义字体$ @" N1 a+ F8 U- {) k; C* a
  28.         $font_file = '1.ttf';
    6 I' l5 O- T, W. u9 \, p5 q
  29.     $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));( d% N7 Z; A4 v2 B6 _
  30.         // 把文字填充到画布6 k+ l/ t, H+ ?  d. E. |
  31.         //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);
    ; V) h3 x! @$ G8 x( f$ q, q
  32.     imagestring($image, $fontSize, $x, $y, $string, $color);( z$ T9 h0 n) o/ h
  33. }. h, a! _. p- A- k; W9 T
  34. $_SESSION['code']=$code;//存储在session里
    ' u" o' g7 I* V
  35. for($i=0;$i<200;$i++){
    4 ^. W7 N/ }8 k- O
  36.     $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));
    9 v! i- ~# \  h; o  C% R2 x
  37.     imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);
    ; K, P; d2 F( I( @2 M
  38. }
    5 g" ^7 [4 v+ d4 D' G
  39. for($i=0;$i<2;$i++){
    - G  o5 Z4 ~3 B! V+ J/ T3 ?
  40.     $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));
    * g+ ?. Z) ^- B, b) K( i
  41.     imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);/ M9 u3 L+ H0 Q( |3 P* b% H7 O
  42. }
    7 r0 X1 {, B! {7 ?' I5 K
  43. imagepng($image);
    # N  N3 @4 k& f7 }9 r- k
  44. imagedestroy($image);
    5 h7 l' ~3 J* K
  45. : e/ r" D, o2 _9 a4 R
  46. ?>
复制代码

  W7 }' d: N. U( U- Fform.php/ e+ K4 [* d' L; e- p
  1. <?php
    8 H7 G2 m# F  t8 \2 J+ Y
  2.     if(isset($_REQUEST['code'])){5 b9 H5 s  W' N4 H. _
  3.          session_start();
    1 E! c5 r1 a; U" B
  4.         if($_REQUEST['code']==$_SESSION['code']){. k$ a7 _/ {% x5 A
  5.             echo "<font color='red'>输入正确</font>";  ?3 J7 R( D5 X/ W6 ]% B
  6.         }else{! h- h2 N, \4 v; l- A! C- i7 V- c
  7.             echo "<font color='red'>输入错误</font>";4 E6 l+ S5 Z9 c! |( j( `1 K: J/ x
  8.         }+ y) k. }! f. Y( a1 ?
  9.     }
    ) ~( F' k( \4 v( W+ n4 \
  10. ?>
    / x2 `/ l; l0 t. r: f  f/ q
  11. <!DOCTYPE html>+ \" h* e, G( z* I5 y
  12. <html>
    , v  h7 E, X- ^* y
  13.     <head>* L1 P# a1 U6 h0 ?# Y$ E7 Y
  14.         <meta chartset="UTF-8" />
    $ f# Y4 \% a+ B3 b% g- I
  15.         <style type="text/css" rel="stylesheet">0 r& T4 s4 b+ f* ^. G5 B4 k
  16.             a{
    & K# j! {, q! d. N4 u) ]. N1 w
  17.                 text-decoration:none;' f; x! v+ B4 J; E9 J* a2 V
  18.                 font-size:30px;
    " L9 t# ?; m9 Z. z- Y/ I
  19.                 color:blue;( g0 M2 O3 V: o8 z, A' G& D1 p
  20.             }
    % [, T2 Y4 z& m& k% c% F1 E
  21.             a:hover{& o$ n8 @/ Z3 f$ }- p( o2 B. V
  22.                 text-decoration:underline;
    * X$ U+ v, w2 l6 w
  23.             }$ `2 w6 J& U3 B: c4 M& N
  24.         </style>6 E6 `2 y- D: s
  25.     </head>
    0 m- H9 ~' p, s! s1 }( M# o/ o6 S2 g
  26.     <body>
    5 m1 K) [0 x; M2 l" k
  27.         <form action="form.php" method="get">
    * r) o' y5 {% J/ Z. m
  28.             验证码:  <img id="img" src="index.php?" onclick="changeCode()"/>+ `5 x# Q# K# B, Y) ~- z" l
  29.             <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />: v5 d& a( t2 w0 N- \2 _8 s
  30.             请输入验证码: <input name="code" /><br />( |* \0 H1 T+ C: e' Z) J
  31.             <input type="submit" value="提交" />0 e, |/ [4 _9 i; M6 e0 _
  32.         </form>3 \# x% `3 z1 m' t5 |7 b' ]  U
  33.     </body>5 C) k( Q# x( b2 O+ `/ l
  34.     <script type="text/javascript">
    6 Y9 {! m1 `# ~6 p) H, T, l
  35.         
    # p2 i8 ^; [/ J2 S7 g
  36.         function changeCode(){
      b7 S/ I; N2 k7 y  Z& U  l
  37.             var img=document.getElementById('img');- D# L) J/ m  T- v* u/ ^
  38.             //img.src='index.php?r='+Math.random();. m$ l) b+ W- q0 r
  39.             img.setAttribute('src','index.php?r='+Math.random());
    1 a* D/ _, c& \: V* h
  40.         }
    7 b. p9 g1 l/ \3 N- n
  41.     </script>
    # y( O- Q7 L" H% K4 V8 G/ \
  42. </html>
复制代码
' X9 V# w+ i9 g5 ^

- L! X. {  @0 E% y
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-1-30 17:34 , Processed in 0.062519 second(s), 19 queries .

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