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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[php学习资料] 验证码

[复制链接]
跳转到指定楼层
楼主
发表于 2018-12-16 01:00:13 | 只看该作者 回帖奖励 |正序浏览 |阅读模式
index.php
. }' H! {' e& z6 o3 L/ i* F* G
  1. <?php( ?& k4 Z3 `' v) S) }
  2. session_start();0 K- H/ m# D9 }$ X; i5 }; U
  3. // 丢弃输出缓冲区的内容 **
    : ?* H. i: V' m0 U2 n: X6 C2 O
  4. ob_clean();
    8 H+ k0 e: T- j4 k+ F
  5. header ('Content-Type: image/png');; f0 X! D! o5 C7 B1 `0 U1 ~
  6. // 1 创建画布
    ' o% b% g3 z1 ^) \1 e2 N
  7. $image=imagecreatetruecolor(100, 30);0 J5 i  L$ M& L' l5 i/ F
  8. // 2 画一个矩形并填充背景颜色为白色: _4 B, v* k' L- a
  9. $color=imagecolorallocate($image, 255, 255, 255);
    1 l+ l" }( u& Q/ V6 t
  10. imagefill($image, 20, 20, $color);
      m$ A- o6 R( |# p
  11. // for($i=0;$i<4;$i++){
    / M. I* ~  {8 l: C
  12.     // $font=6;* x: ]$ s2 f% H2 Q/ {' Q
  13.     // $x=rand(5,10)+$i*100/4;/ ~' L# K/ a- w% U5 \# Y- y) z% p
  14.     // $y=rand(8, 15);
    $ O0 F( s" u% R' p: _
  15.     // $string=rand(0, 9);/ R! m8 o, c# G! Z/ l5 m# f
  16.     // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));' o: N5 f- R% N( N! V
  17.     // imagestring($image, $font, $x, $y, $string, $color);
      @; `! `6 J) f0 r0 |
  18. // }2 U+ R6 a+ A% X# P  P1 s
  19. $code='';8 N) v1 P- ]1 l- P# {% P- q9 ~
  20. for($i=0;$i<4;$i++){
    8 \1 x( N8 s  K0 i6 y7 w/ `
  21.     $fontSize=8;" B' v- O) t- ]5 F& q
  22.     $x=rand(5,10)+$i*100/4;8 D! F+ _3 |" y# r6 J" q: I0 J6 H
  23.     $y=rand(5, 15);
    : Y: Q; o$ ^% i" o% O
  24.     $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
      A, C& T( b: j
  25.     $string=substr($data,rand(0, strlen($data)),1);& c' V+ b9 G& y  M; c3 E
  26.     $code.=$string;
    7 I! n1 B- Q- A, b
  27.         // 3.2 加载自定义字体
    # N: Q: c7 C/ U
  28.         $font_file = '1.ttf';* Q8 ^+ v  B4 N% k9 D8 b
  29.     $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));
    ' B% Y7 C# F" i1 h" c4 F
  30.         // 把文字填充到画布
    * v; \+ l7 k/ ~
  31.         //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);9 ~0 P7 b, O9 u- Y0 P8 s' W
  32.     imagestring($image, $fontSize, $x, $y, $string, $color);
    " W& \6 }+ h5 ]+ `8 c) b+ ~
  33. }
    * S' b/ f' E) y
  34. $_SESSION['code']=$code;//存储在session里
    7 i' Q. g" @/ n( X
  35. for($i=0;$i<200;$i++){
    ! v3 k- I" G' C1 h8 P: b( I
  36.     $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));
    9 f5 t1 Z. ]$ w
  37.     imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);% A' K2 n4 c2 f3 x4 ]/ V; v) _
  38. }+ b1 H1 b% `: p8 B3 s' f
  39. for($i=0;$i<2;$i++){
    0 |( D; h& X$ M# C$ X! P
  40.     $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));1 D& ?/ s3 q5 D5 `- n" p: r
  41.     imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);0 t  ]" m% ]( S7 y, M( T7 J* \
  42. }
    * {  b1 v4 ~2 @  Y0 ?: v& c, a
  43. imagepng($image);6 f5 T  u& F6 l, v7 B: q: R
  44. imagedestroy($image);' O& q5 @. e/ [- v

  45. % ]1 R( V9 n+ k0 t, p
  46. ?>
复制代码
) I, g5 v; R4 \: p
form.php
- n( i8 \3 S! l
  1. <?php% S) Y' K& X2 ]# Q$ d
  2.     if(isset($_REQUEST['code'])){
    ; l( u3 _* r0 Z" v( O3 W$ u
  3.          session_start();
    - i5 i8 l  Y6 o
  4.         if($_REQUEST['code']==$_SESSION['code']){
    $ R5 G; {6 t. A% M6 e0 t
  5.             echo "<font color='red'>输入正确</font>";
    8 p+ Z& M: [& c; m8 u1 |; N: q
  6.         }else{* v/ t" O1 {: `
  7.             echo "<font color='red'>输入错误</font>";7 A$ F4 u9 h0 ?' P2 B
  8.         }
    9 c# ~, R" _, ]2 Z" e- \" E
  9.     }
    & h: r% j' p/ x
  10. ?>
    7 {8 Q9 a, p. `
  11. <!DOCTYPE html>* o# G0 Z0 Z; ~; f8 X- Y
  12. <html>
    0 U% L$ o- |- j3 i! T, Z- I
  13.     <head>
    # o' d: ]4 j4 l9 A3 X
  14.         <meta chartset="UTF-8" />3 F) {7 p7 R6 A0 M" W) r
  15.         <style type="text/css" rel="stylesheet">8 ?8 c% d) m3 u$ m: B, X9 P
  16.             a{$ c( F5 ]5 ]6 [& q: ~
  17.                 text-decoration:none;4 W5 t/ d% R3 r8 u. m' f
  18.                 font-size:30px;1 w% N/ w, }' Y9 Y- N0 t8 o* R( D
  19.                 color:blue;' f" @: ]9 q8 I
  20.             }1 \1 [; `3 m8 e9 G
  21.             a:hover{5 ?) ~3 {+ w8 a2 f; l
  22.                 text-decoration:underline;
    : J  z+ K0 E9 T, x" K8 m0 X
  23.             }
    ; p2 ~5 A9 Y; E& k, H% F  L4 S
  24.         </style>
    ! J( }( P; h$ p; q4 `6 S/ Q
  25.     </head>) j5 h! \; f1 O3 ]0 N
  26.     <body>
    % ~6 l. ?7 `! G8 z
  27.         <form action="form.php" method="get">- q* o/ f. G/ x4 N! |: u8 Q
  28.             验证码:  <img id="img" src="index.php?" onclick="changeCode()"/>/ a2 V0 V& a8 `/ @0 ~
  29.             <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />
    3 V7 D; s9 n/ o/ @1 A
  30.             请输入验证码: <input name="code" /><br />
    5 m/ `2 }; Q7 H" V* K- C
  31.             <input type="submit" value="提交" />
    " ^4 V8 ~( J/ l- V  @: y/ |
  32.         </form>% E) g% [' `5 O" |; h  E! j' D
  33.     </body>/ d6 N! b2 F0 N9 S9 H, m
  34.     <script type="text/javascript">
    ; c: W+ ]: |5 l
  35.         $ D5 i. P; }' I, X) X# O
  36.         function changeCode(){
    ! ]  M! D0 Y/ `: v: J
  37.             var img=document.getElementById('img');1 I7 [! l( i# e4 q3 H% g) s
  38.             //img.src='index.php?r='+Math.random();
    6 U- Y" r5 L0 ^# H( h! g
  39.             img.setAttribute('src','index.php?r='+Math.random());
    - M) k9 T; W% Y: H. O) M
  40.         }
    $ A0 l/ L$ D0 }  o, m
  41.     </script>& V4 V8 z2 z+ q7 |) R  d( G
  42. </html>
复制代码
2 r0 ]  ^7 F& B6 R
" Z9 y3 Q& u( S% Q* w& ?  D
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2024-6-29 06:06 , Processed in 0.101861 second(s), 20 queries .

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