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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[php学习资料] 验证码

[复制链接]
跳转到指定楼层
楼主
发表于 2018-12-16 01:00:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
index.php# m2 Q  F9 z5 j) j0 ]5 _' q- \" k( O
  1. <?php
    - O; N5 Q6 \2 d) g
  2. session_start();
    # }# }& P) s2 p5 S+ p5 t* ^
  3. // 丢弃输出缓冲区的内容 **" }! h( U+ e5 N7 X) C" G) B0 j
  4. ob_clean();, y/ Q" c2 k# `' b# E4 i7 Q
  5. header ('Content-Type: image/png');
    1 F5 I* m! n  e' D/ M' x/ @
  6. // 1 创建画布! l; N1 y4 U, ~# c
  7. $image=imagecreatetruecolor(100, 30);
    3 t/ u1 w# {& R8 _7 f; A5 p) |) _
  8. // 2 画一个矩形并填充背景颜色为白色
    ( P# A$ H/ G1 I
  9. $color=imagecolorallocate($image, 255, 255, 255);
    $ J9 _/ l  D% W9 f+ r3 U+ k
  10. imagefill($image, 20, 20, $color);
    $ v% |, I2 ~9 s2 ?
  11. // for($i=0;$i<4;$i++){
    8 }" ^6 f, ^; M
  12.     // $font=6;
    ' ^2 _9 i0 C$ r& i+ {% L. a) t
  13.     // $x=rand(5,10)+$i*100/4;  i0 A) ]6 r# r6 H9 g4 o: ?( G
  14.     // $y=rand(8, 15);& r! H& f/ t2 s: R7 u2 O2 Z
  15.     // $string=rand(0, 9);3 W# B) D2 W/ K: h+ ]0 n/ S
  16.     // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));4 G- T; ]' q9 c( \
  17.     // imagestring($image, $font, $x, $y, $string, $color);
    7 y6 B4 m5 p6 e
  18. // }
    2 ?" c- s$ o9 ?! W# L
  19. $code='';) E! b5 S/ O- A/ i# D
  20. for($i=0;$i<4;$i++){' E1 G& K, x0 u/ x) U6 w- }( G
  21.     $fontSize=8;" K) y. w. {1 D6 R6 \/ @
  22.     $x=rand(5,10)+$i*100/4;. Y/ x5 b+ i" a( E
  23.     $y=rand(5, 15);, N5 v) g- `$ K% B# M
  24.     $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';) D6 D) |" A* n( s
  25.     $string=substr($data,rand(0, strlen($data)),1);  i7 o3 C1 _& L$ Y+ o  \1 O" T+ O
  26.     $code.=$string;! _; N% ~* d( Z2 T
  27.         // 3.2 加载自定义字体$ c# a1 n  L4 \4 P# `( h
  28.         $font_file = '1.ttf';8 Y1 K$ s; e% {! Q+ f
  29.     $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));
    2 i2 z* }! N+ a7 f8 J; U  Y* X& ^
  30.         // 把文字填充到画布
    , ]# l, O3 j% b' e( p0 ^, V
  31.         //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);# F: R" T$ G3 Y2 i5 }* c. K
  32.     imagestring($image, $fontSize, $x, $y, $string, $color);
    , F$ x/ I5 Z  m6 u' D5 t
  33. }
    ' @' `" _7 ~" Y& m9 V! c; X6 j3 v
  34. $_SESSION['code']=$code;//存储在session里6 t, C% l: E( U4 ?2 _  R+ d/ O
  35. for($i=0;$i<200;$i++){
    # h0 l$ ?; {3 Y
  36.     $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));
    8 n- q) s( l4 q1 G6 u
  37.     imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);3 x4 c/ H1 D! J
  38. }7 h" V- \1 |/ _" Z5 f
  39. for($i=0;$i<2;$i++){
    / ?0 ]: p( Y% ]! q
  40.     $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));
    ! {2 U7 I' D$ x5 ~
  41.     imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);0 N* G/ v2 j: b7 ~5 h
  42. }
      P1 N* r% X- ~2 C/ n
  43. imagepng($image);6 \  ~+ K- g! C5 a9 w
  44. imagedestroy($image);
    ' N% v8 L1 z, A3 k9 ]! ?
  45. 6 p7 _. A3 i% r% v; N8 T( J& Y
  46. ?>
复制代码
$ g+ }8 @% E; F+ _$ b
form.php& O0 q# z9 _( h5 N
  1. <?php2 N# P- z; b& n6 i" m! R6 ]" d* j
  2.     if(isset($_REQUEST['code'])){  L* j" b' G: @( B
  3.          session_start();
    " B; U0 I; g0 b
  4.         if($_REQUEST['code']==$_SESSION['code']){
    ! a- C- l5 ~7 a/ k! W/ t
  5.             echo "<font color='red'>输入正确</font>";+ T; C7 p. W* i+ v7 {0 I8 V
  6.         }else{, c: M* a: P; f3 C& }$ C' c7 G
  7.             echo "<font color='red'>输入错误</font>";0 e2 _$ [$ ?5 u# \1 W
  8.         }, a/ `( e1 z6 Q( W; W6 M- U0 z* u
  9.     }
    ; F, `4 Q1 @# y1 K/ q! e
  10. ?>% {, r6 f9 y8 J6 y# p6 Y* W3 Y+ c
  11. <!DOCTYPE html>1 z; a6 m  n" Y: X' p7 {5 e
  12. <html>
    ) s9 }$ Z$ ~* f+ i$ r' Y
  13.     <head>
    : B4 Q9 w) `" \" R" [3 |
  14.         <meta chartset="UTF-8" />
    , L% d6 y1 r' Y
  15.         <style type="text/css" rel="stylesheet">
    , d/ V  F/ l; ?& e: ?
  16.             a{
    6 l! \( F5 j0 {0 f
  17.                 text-decoration:none;1 u5 C' j8 a) H0 N/ A) w
  18.                 font-size:30px;
    ' z4 N# f! w. ?9 f; K; Z
  19.                 color:blue;
    " Q* N( o0 Q; @) c1 r! y" w7 T6 y
  20.             }
    6 I5 c1 {) j+ L9 ^9 b, r1 s# \
  21.             a:hover{% b* v  m" m; h: w. y
  22.                 text-decoration:underline;
    4 u7 p' x* L0 g1 _
  23.             }! h2 u7 U6 `- Y
  24.         </style>' A* d/ [8 M9 D
  25.     </head>
    , @5 _: Y# ?* N$ j' `
  26.     <body>- \+ a  V  Q7 X3 r4 k2 h" Q
  27.         <form action="form.php" method="get">
    5 g; `0 j9 @$ n1 |0 {0 K9 h$ d& A
  28.             验证码:  <img id="img" src="index.php?" onclick="changeCode()"/>9 A) F/ e# v! T4 E
  29.             <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />
    4 s9 @/ a4 b# i! R; n
  30.             请输入验证码: <input name="code" /><br />2 w7 C3 Q& ^% }
  31.             <input type="submit" value="提交" />) Y& W$ S' W* R$ b: O
  32.         </form>
    9 e, B  O+ k) v8 h; G
  33.     </body>0 [* x  U5 h6 X5 i1 ]7 D& I3 ^3 V" v
  34.     <script type="text/javascript">
    + e! V9 [, e* f) c( s, Y
  35.           W' s8 r5 S7 g6 K. j  n, f
  36.         function changeCode(){
    ! ~* G- G" w6 d+ U- X3 q: I2 u
  37.             var img=document.getElementById('img');7 u: ]9 I4 g4 W% c7 c
  38.             //img.src='index.php?r='+Math.random();
    $ t4 ?$ t# P' |
  39.             img.setAttribute('src','index.php?r='+Math.random());* J- n) B1 R3 K, ~* c
  40.         }
    3 }. I5 W# {' A) L3 [) ~5 O/ r
  41.     </script>6 Z7 T- ]7 @4 H2 W6 Y
  42. </html>
复制代码

7 M' r* X" O  B  H. v/ Q$ _4 B  C2 P  ~
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-1-30 14:28 , Processed in 0.065288 second(s), 20 queries .

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