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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[php学习资料] 验证码

[复制链接]
跳转到指定楼层
楼主
发表于 2018-12-16 01:00:13 | 只看该作者 回帖奖励 |正序浏览 |阅读模式
index.php% K5 A# c, N& ]$ k# E$ X  n) ^, L
  1. <?php
    " ^2 ~6 @0 a1 A
  2. session_start();
    7 v' n& q& t/ O" }; I" J9 U
  3. // 丢弃输出缓冲区的内容 **, l( ^& n! N* ?1 L" s  i+ z
  4. ob_clean();
    ; q) a1 ]1 i6 ~, p
  5. header ('Content-Type: image/png');
    " M- l- g5 A& x
  6. // 1 创建画布
    5 S& E, H& z0 @. Z- K* u
  7. $image=imagecreatetruecolor(100, 30);; n2 Q& {; H: z* I- c
  8. // 2 画一个矩形并填充背景颜色为白色# ?+ U! c' t: ~" t4 }
  9. $color=imagecolorallocate($image, 255, 255, 255);
    / a, m! ^! r+ m( z% T
  10. imagefill($image, 20, 20, $color);
    7 A& ]! l& `. V0 E; u$ z0 H& H
  11. // for($i=0;$i<4;$i++){6 {; x. d6 z7 A1 C3 {, Y; w
  12.     // $font=6;0 V: l/ z" D) e9 P6 u# ^8 G% l
  13.     // $x=rand(5,10)+$i*100/4;* E9 O! B2 r. B" \! V! _, ?2 J
  14.     // $y=rand(8, 15);  U% M& `- R4 g( m
  15.     // $string=rand(0, 9);3 c0 w. T5 p  \8 s5 }
  16.     // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));
    . ]" `8 g% _; p  ]. y
  17.     // imagestring($image, $font, $x, $y, $string, $color);- b, e8 L% y: a! V2 [
  18. // }
    . ]* K: h- I( S, ?- t
  19. $code='';
    ! a: ^# W  M. P8 P/ N0 Q
  20. for($i=0;$i<4;$i++){
    ) B7 ~. v! {# z8 |
  21.     $fontSize=8;
    - ^& \+ n2 Z$ _+ c
  22.     $x=rand(5,10)+$i*100/4;2 Q0 N. a# ?; S) n2 a% r
  23.     $y=rand(5, 15);/ A5 i" R( h) F! ~9 a1 @; @
  24.     $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
    3 r7 G& `; e- @  a$ w+ E
  25.     $string=substr($data,rand(0, strlen($data)),1);9 h' `0 r0 m4 b- ?% x) `6 h
  26.     $code.=$string;
      `- ~- [; p* K
  27.         // 3.2 加载自定义字体9 r* s# s5 ?  Q. L
  28.         $font_file = '1.ttf';
    % o' `5 r' Z) D/ W* V7 u9 N
  29.     $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));
    : B* ~- L+ d; y. W+ T' X
  30.         // 把文字填充到画布) V- s& J4 n% B# G* Q: M4 g
  31.         //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);& E7 \( n6 J  e0 i" P7 L, E
  32.     imagestring($image, $fontSize, $x, $y, $string, $color);
    6 T8 Z& H# j  y6 _6 Q
  33. }
    5 D; \% y! b5 W1 f4 z  e
  34. $_SESSION['code']=$code;//存储在session里
    / H' B( x, D% s5 e
  35. for($i=0;$i<200;$i++){% g! I  @* J5 t8 b+ p! Q# T
  36.     $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));
    / \! J6 `; d1 J$ Z
  37.     imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);
    6 y9 m6 Z2 o. T+ W
  38. }/ d; P# c; k7 u' E! j4 Z
  39. for($i=0;$i<2;$i++){
    1 C. C5 j# q" o: L
  40.     $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));+ I* l. D7 \; I8 G6 x& D) n
  41.     imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);* D3 l/ s" U3 I  u& D
  42. }
    ' g5 p+ Z/ r- y- ~4 Y% ^
  43. imagepng($image);
    ' c4 s; o- B9 v. O( m7 `4 F* x
  44. imagedestroy($image);; L. {, c& _& J4 U

  45. / f( i' o' g' d* o
  46. ?>
复制代码
: M2 z" a0 s' E. d4 d) l* O: C
form.php
: m/ G; l* A% j; k% m& o+ U2 t9 O1 t
  1. <?php7 t" E3 N! a" I2 Z9 n- D/ O5 h
  2.     if(isset($_REQUEST['code'])){
    ! Z$ Q1 s6 n7 Y, s( Y8 d3 i
  3.          session_start();; {8 h) ~/ b6 {. K
  4.         if($_REQUEST['code']==$_SESSION['code']){0 {. V* k6 X# \- p: e9 T5 r, M3 {
  5.             echo "<font color='red'>输入正确</font>";$ y! A7 G9 I9 |; ~% ?
  6.         }else{( i& j# p+ T$ G( {" E) N+ P# E+ G
  7.             echo "<font color='red'>输入错误</font>";7 y% r+ X" Z& g
  8.         }
      ?* z/ a! u' w+ h  G6 G
  9.     }- n- }. ]. w! L) G" X( h. _+ m
  10. ?>  Z: d) N! h" M5 |! }, l& s  J/ I
  11. <!DOCTYPE html>( H4 w; B2 p3 Z
  12. <html>
    8 `( M, c0 Q) H
  13.     <head>
    1 B  m0 D7 J/ F+ Q# R9 y
  14.         <meta chartset="UTF-8" />, B- J* w/ p9 z0 z0 n- x
  15.         <style type="text/css" rel="stylesheet">
    . \5 J% i2 g1 j6 c- `
  16.             a{
    ) ^, d. u3 K0 i' M
  17.                 text-decoration:none;# N2 E- |! t$ P3 b! ~
  18.                 font-size:30px;# F, Y$ o/ s/ B1 x) v* V
  19.                 color:blue;# o0 m% m( s% l' ?
  20.             }
    8 D2 {/ y& J; ^3 O: N8 f) [4 f, x
  21.             a:hover{
      B6 t8 S$ d2 J3 K
  22.                 text-decoration:underline;
    ( T! C) ^( o/ \! m
  23.             }. h7 i# ]# I+ [1 E, r
  24.         </style>$ }7 y! @% m, W* I; _
  25.     </head>
    . R! n5 V9 x  t+ T$ H0 d% h
  26.     <body>5 z% R/ L6 P+ n0 Y* m: X
  27.         <form action="form.php" method="get">
    / a" A4 v& t! j8 l
  28.             验证码:  <img id="img" src="index.php?" onclick="changeCode()"/>* b+ _/ C, e/ ]6 a! @. [
  29.             <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />
    . D1 c' d- m* C( F
  30.             请输入验证码: <input name="code" /><br />! B1 D& Z+ W$ [
  31.             <input type="submit" value="提交" />: h* l2 g3 I) d0 h/ u* W. |+ [
  32.         </form>! b& J+ E: Y" w3 C( G& m% _6 t
  33.     </body>
    - {) n0 `7 l) W) q1 k
  34.     <script type="text/javascript">
    2 P, W  t, l* _
  35.         
    % h2 i1 `' V+ Q1 \- d
  36.         function changeCode(){% M9 m, p& B1 C" j  a* y; J
  37.             var img=document.getElementById('img');  h* }: }. n% i0 W/ U8 W* r
  38.             //img.src='index.php?r='+Math.random();1 J$ B9 o/ B9 |% C, m' M7 W2 I0 F5 l! S
  39.             img.setAttribute('src','index.php?r='+Math.random());/ ~" Y4 K: i( @: G( v7 v/ @
  40.         }: c& m/ i# N3 f* B, Y3 P
  41.     </script>5 W3 K  I9 q: L
  42. </html>
复制代码

1 w* U$ P& D6 A+ J7 t' g
% c3 T3 U8 ~9 r) F7 ~) `
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-5-2 18:27 , Processed in 0.062115 second(s), 22 queries .

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