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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[php学习资料] 验证码

[复制链接]
跳转到指定楼层
楼主
发表于 2018-12-16 01:00:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
index.php
- R( j/ S% E& n+ W. k  K8 O3 F
  1. <?php
    9 o4 s: t" F9 q2 Y0 S
  2. session_start();6 ~* [: h4 D# J
  3. // 丢弃输出缓冲区的内容 **
      ~2 G9 {, M! G
  4. ob_clean();
    ' a; T1 M; |8 z, C2 a4 p3 E- t
  5. header ('Content-Type: image/png');5 C$ g, q/ L$ @! @: f1 c
  6. // 1 创建画布
    ( F/ r# c- x. ^* Y' l4 F9 Y: Y
  7. $image=imagecreatetruecolor(100, 30);% I) }. n  R0 i9 H  F
  8. // 2 画一个矩形并填充背景颜色为白色
    , R9 K$ G2 |* p+ [7 }
  9. $color=imagecolorallocate($image, 255, 255, 255);$ u0 j5 z4 F0 Z$ M( X
  10. imagefill($image, 20, 20, $color);9 U. k# ]7 H, v7 F2 ^
  11. // for($i=0;$i<4;$i++){' C7 H8 I! t6 Z4 R8 S& q* ^
  12.     // $font=6;
    1 T$ d4 T* M, m* h* R
  13.     // $x=rand(5,10)+$i*100/4;* B# Y) u, P. l
  14.     // $y=rand(8, 15);. T4 P3 \. {0 D$ j: U& _9 F1 i
  15.     // $string=rand(0, 9);7 ?. q5 z6 V. L1 X
  16.     // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));) [% v  o9 V" w0 c% k: Z7 h4 U
  17.     // imagestring($image, $font, $x, $y, $string, $color);
    + }$ B4 u& F9 r
  18. // }
    6 r: `& S) C. H2 X1 M# H. D3 T
  19. $code='';
    ) F+ y8 g  _- s& g! D) e5 z
  20. for($i=0;$i<4;$i++){. y1 `$ x8 d' c/ ^) V, p: w
  21.     $fontSize=8;, j3 H0 U1 G( ^; K/ u
  22.     $x=rand(5,10)+$i*100/4;! B. \& L5 \* L% M8 P' ]" ]+ g8 r; R: p
  23.     $y=rand(5, 15);
    : Q2 b! @' m9 u+ m" r+ i* ?
  24.     $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';) _/ G. R) S, e0 H+ t
  25.     $string=substr($data,rand(0, strlen($data)),1);) J/ H( H  T; J0 H9 ~- O
  26.     $code.=$string;
    ) Z* @/ P. P7 k$ \  w
  27.         // 3.2 加载自定义字体
    $ c& j8 H& w, a
  28.         $font_file = '1.ttf';. y4 V+ N/ i1 K( P" ~
  29.     $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));0 A" g, |; r% K2 P" `% ~
  30.         // 把文字填充到画布8 ^* c. ]& ?9 n! W# a* X
  31.         //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);6 }' e4 q9 z6 R6 f1 v# O
  32.     imagestring($image, $fontSize, $x, $y, $string, $color);
    , k' `1 X6 ?, j" N4 e
  33. }2 z% F4 P/ p8 A/ ]( o
  34. $_SESSION['code']=$code;//存储在session里4 F! y6 i& g: [9 @' j3 u4 k* N
  35. for($i=0;$i<200;$i++){
    6 f+ b3 f4 C, P) B
  36.     $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));
    8 v) O# ^3 t6 c& x+ Y! y: x
  37.     imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);
    0 o  \6 G1 n* k" ^, p$ j
  38. }- J) r4 _9 q' H5 y- F3 p6 O
  39. for($i=0;$i<2;$i++){
    - G& w5 s: y" p; s3 y' A' c5 p
  40.     $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));" S9 a! {" u7 S' ~, T/ H( s1 v! W2 c* B$ n
  41.     imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);
    " k  |4 z: u( \5 Y
  42. }
    # I6 A/ O( c4 F0 Q7 i7 I
  43. imagepng($image);
    4 M0 h/ R6 g- x5 ^2 M3 p
  44. imagedestroy($image);( z+ Y) B9 `/ E' H) l/ ^
  45. * `! E# F7 r3 _; S# Y$ Z) `
  46. ?>
复制代码
' c: e: N8 }" I; f/ V0 A$ H
form.php* O, o) n9 o/ A5 t  ?9 L$ k2 m3 N
  1. <?php
    0 e. S/ G& |, x, E6 v$ H# ^
  2.     if(isset($_REQUEST['code'])){
    0 L& l* v4 P  W/ u: i3 d- h) W: V
  3.          session_start();
    + L! D* c* ^9 N9 R& y3 O$ l
  4.         if($_REQUEST['code']==$_SESSION['code']){
    2 ^7 K- z( w5 B/ n+ _) C
  5.             echo "<font color='red'>输入正确</font>";
    1 q( Z" ~; p+ ]) a( H3 M4 x. B
  6.         }else{
    # n0 E4 x  P: Z  Y
  7.             echo "<font color='red'>输入错误</font>";
    + l+ h  ]  H7 X: X' H
  8.         }
    " E6 z1 y) G0 G$ v: {" N8 }
  9.     }8 u* m2 l, v+ g% `
  10. ?>
    3 H2 @* l9 C6 ^4 V
  11. <!DOCTYPE html>
    2 O1 B& }3 N3 v9 W9 o
  12. <html>' E) q4 F0 O& `
  13.     <head>
    7 V3 r/ Q5 Y$ F; L1 B) E
  14.         <meta chartset="UTF-8" />- V- q: u0 {# Q  a- ], S( D0 t" D
  15.         <style type="text/css" rel="stylesheet"># ~. t) ~7 L& r8 o
  16.             a{, R/ O' j3 q" r1 h, h, `9 J
  17.                 text-decoration:none;6 }' j/ i; ~6 U2 \. Y6 D' h4 u' M
  18.                 font-size:30px;
    ; b9 `) w" b$ B# m& Q
  19.                 color:blue;3 X! l( G  l4 c4 E
  20.             }2 w0 G7 Q! }. M; U/ R! H
  21.             a:hover{, y: E4 o  _. q( C4 M7 e
  22.                 text-decoration:underline;
    + |$ @- e  }2 E; ]; n4 g0 [( p
  23.             }
    9 ?' o9 d" I( f
  24.         </style>1 |" Q% X8 e' {: a
  25.     </head>
    ! f/ Q' |& ^4 H* I4 S7 c0 ]
  26.     <body>
    * [* B) K- `' M8 |: b( ~1 Y/ J
  27.         <form action="form.php" method="get">
    - z$ w' C2 W* k! c
  28.             验证码:  <img id="img" src="index.php?" onclick="changeCode()"/>- L! E1 e  R" {' v: g# p
  29.             <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />9 R: g7 W( Z% D% J7 R) c8 K
  30.             请输入验证码: <input name="code" /><br />
    " c1 h! w9 o( k' b% w% F
  31.             <input type="submit" value="提交" />
    9 J" C* \! Q; C4 i; Z1 B
  32.         </form>
    - Z$ O2 S/ n- C
  33.     </body>, @; O9 t/ \) M" ~: `- E0 W! _3 o
  34.     <script type="text/javascript">
    4 I9 R7 _: J, b: [4 U4 N% m) F. O8 Y
  35.         ( G: {) r. ?- I2 |
  36.         function changeCode(){3 L1 ]/ u7 W8 M
  37.             var img=document.getElementById('img');9 {0 w; t" C7 Q3 u" }
  38.             //img.src='index.php?r='+Math.random();7 T. a  z9 P, M! j/ F3 L$ E
  39.             img.setAttribute('src','index.php?r='+Math.random());
    3 \/ u' m* c3 ^7 j
  40.         }
    # n0 [/ `; |( l
  41.     </script>
    % F6 C$ H' a5 F4 w
  42. </html>
复制代码

! z. e2 B" S: p; `, y  D% M! |. |) ?4 m4 _& F
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-3-17 23:26 , Processed in 0.068691 second(s), 19 queries .

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