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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[php学习资料] 验证码

[复制链接]
跳转到指定楼层
楼主
发表于 2018-12-16 01:00:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
index.php
. e# n3 @$ K+ z7 h4 m. P+ O
  1. <?php
    4 w/ s$ N+ d, n. y; C
  2. session_start();
    ' U; L* K3 d+ o, J$ w0 ^7 c
  3. // 丢弃输出缓冲区的内容 **
    . L: s) B0 j, @
  4. ob_clean();( n8 D8 P: A! _9 G- R- ]4 T; H% r
  5. header ('Content-Type: image/png');
    ( D; M: T( ?$ o4 a1 G6 v
  6. // 1 创建画布; ?# F  G$ L: a* t
  7. $image=imagecreatetruecolor(100, 30);
    " I2 v% j; _* \  R
  8. // 2 画一个矩形并填充背景颜色为白色
    ( w; f" {- l' |: W& v8 ^. _
  9. $color=imagecolorallocate($image, 255, 255, 255);2 R4 s9 P" r; I8 l
  10. imagefill($image, 20, 20, $color);
      n/ g6 R% g7 @7 x% X
  11. // for($i=0;$i<4;$i++){
    # x) y6 W; o3 ?0 G
  12.     // $font=6;
    " L( P6 _, Z4 f$ B
  13.     // $x=rand(5,10)+$i*100/4;
    ) u1 t; p  j" T! P2 P/ t# s
  14.     // $y=rand(8, 15);0 E% Y1 w# Y1 b  I5 L; P6 H
  15.     // $string=rand(0, 9);
    9 p4 {8 X- L/ _
  16.     // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));
      D  Z2 j# o4 |8 {7 R3 h( ]; }
  17.     // imagestring($image, $font, $x, $y, $string, $color);
    4 B, r1 s" y. t% v7 `& x- Z
  18. // }6 d1 U: e  M: |3 t
  19. $code='';: x: c. Q9 g, K3 {1 w2 D5 b9 w
  20. for($i=0;$i<4;$i++){
    ' W; X/ x8 J, z+ J
  21.     $fontSize=8;! f! Z% L3 h, T- w3 \! Y/ T8 N
  22.     $x=rand(5,10)+$i*100/4;. r- j# Y/ M$ b6 x9 }
  23.     $y=rand(5, 15);: d2 B4 e( o' i- r
  24.     $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
    ! A, [( j1 }  e! r8 [- I
  25.     $string=substr($data,rand(0, strlen($data)),1);
    7 `5 \! w  h: Z
  26.     $code.=$string;/ c1 r# G0 x' I) D  A* t1 G  n
  27.         // 3.2 加载自定义字体
    ! ^2 O; t% v  T( Y4 ^& k
  28.         $font_file = '1.ttf';
    $ K, t) t; H0 C# t; d1 o* Z
  29.     $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));
    " `+ {. Q4 |; ^: ]$ }6 H
  30.         // 把文字填充到画布1 a& q, z- a: F) U" s) E
  31.         //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);
      Q7 i1 j/ S  A2 I) ~  G
  32.     imagestring($image, $fontSize, $x, $y, $string, $color);
      q) E  ]1 n; x/ \
  33. }4 i; T) I& F- M! [8 n0 ], V
  34. $_SESSION['code']=$code;//存储在session里/ j7 G# i; W* r' r
  35. for($i=0;$i<200;$i++){5 b! p: ]$ r6 I8 q% E
  36.     $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));, ^  v$ U5 L, G' Q$ N$ h" h
  37.     imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);; n# h! r% r4 x+ w
  38. }
    # l1 |4 `+ D' h, q) I3 V
  39. for($i=0;$i<2;$i++){
    8 H/ K2 @" {% ]* Q! h( `' G& u
  40.     $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));; W, M6 p3 L$ F+ Y
  41.     imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);7 a3 v; P8 l5 N7 E) o, n; G
  42. }: z7 a* z/ \7 V% q
  43. imagepng($image);
    9 H+ K* x4 O8 T/ }6 x3 f4 Q
  44. imagedestroy($image);3 }& C& }0 v& k; _$ I8 x& M

  45. ( _4 v. V0 Z2 a) g) W
  46. ?>
复制代码

0 Z) j9 s# n" p: P, x  uform.php
/ @  |5 c2 t6 E0 i) T7 J& x
  1. <?php5 W$ d. ?0 W1 x: {2 k
  2.     if(isset($_REQUEST['code'])){
    3 B! p/ V  V, }. W
  3.          session_start();
    ) M# H/ z" l1 q8 X
  4.         if($_REQUEST['code']==$_SESSION['code']){
    ' p$ ]5 o, R' ?; j! G* @
  5.             echo "<font color='red'>输入正确</font>";1 p8 L, S2 w6 j) Q+ s2 Z
  6.         }else{
    - w9 D$ U, f3 o; L
  7.             echo "<font color='red'>输入错误</font>";
    8 ]6 d# q/ I/ J) A: ?7 S
  8.         }
    - R: w9 r6 T9 ~; @
  9.     }- b* i) p+ G1 ^6 |% I( q1 {3 j
  10. ?>
    $ `5 D& l. F' Z$ N9 `% ]. f2 w
  11. <!DOCTYPE html>
    % `  P: X: }' N$ i- F9 F5 Z  `
  12. <html>3 g: s! R' A* z9 N& u, Q* u1 y
  13.     <head>7 M; C( R: V# A" ?5 N  N/ r, ~1 P
  14.         <meta chartset="UTF-8" />
    6 D! ]: Z9 v  d% b& U
  15.         <style type="text/css" rel="stylesheet">
    ; Z0 S& J( ^6 d8 d1 h) C% {
  16.             a{8 \* Z* u  `7 V  B' e% {  o# s
  17.                 text-decoration:none;0 z+ s& @: O: Q" \. |# R# p
  18.                 font-size:30px;
    8 C2 B1 ]. O7 f# w/ s5 _8 a/ d5 T  }
  19.                 color:blue;+ A/ H; ~  l4 I  w
  20.             }
    ( N$ P" a7 W6 r0 @
  21.             a:hover{
    1 s- B! e1 P1 `7 W& h3 Q0 ]7 {
  22.                 text-decoration:underline;
    ' p7 ?, f$ n; s- \
  23.             }
    & M$ A" ]$ J, G6 V8 A/ \/ B; y
  24.         </style>
    5 k3 B3 H8 L  D7 c: e
  25.     </head>
    + X  M& D) {$ M, P
  26.     <body>
    % Z% S" k' Y" I6 _
  27.         <form action="form.php" method="get">
    1 e+ Q0 s+ C  q/ y0 c
  28.             验证码:  <img id="img" src="index.php?" onclick="changeCode()"/>
    6 O! e6 ^9 B, O& ]+ p
  29.             <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />
    ! l" Y. s  O9 c1 L
  30.             请输入验证码: <input name="code" /><br />
    - e2 S' k) }0 @: u0 w' d
  31.             <input type="submit" value="提交" />. L( v" m% B* Y" }+ M
  32.         </form>: l( W( g# o9 }" V
  33.     </body>
    $ D. Q% i0 }9 O) D+ Q% I" q
  34.     <script type="text/javascript">
    $ r- D6 R/ c- e
  35.         
    " G; ~* ?3 u' a! m1 }0 R6 ]
  36.         function changeCode(){6 E' z6 F. J' @' o
  37.             var img=document.getElementById('img');
    ) U7 F0 `. ]! c3 o5 I* q
  38.             //img.src='index.php?r='+Math.random();1 C. w' g2 L0 X9 m! Q) g; \
  39.             img.setAttribute('src','index.php?r='+Math.random());6 O' t' S5 g& j
  40.         }  v  r7 y: s4 I& C
  41.     </script>
    3 R- L. F1 W  P7 d
  42. </html>
复制代码

" Q- I) _2 q' \9 i
' d/ @" G7 |4 ?9 A/ O6 e% I- E' H
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-5-2 19:25 , Processed in 0.057159 second(s), 21 queries .

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