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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[php学习资料] 验证码

[复制链接]
跳转到指定楼层
楼主
发表于 2018-12-16 01:00:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
index.php
6 N: z3 d& ~" u( I
  1. <?php
    ' C: e  v5 l# ^, M# |
  2. session_start();8 F) Z4 m3 R+ {& ]7 u" c% n
  3. // 丢弃输出缓冲区的内容 **
    ' o) r) `+ Z7 @+ @9 T( S, _6 S
  4. ob_clean();* Q$ \- S# t. i( l( W
  5. header ('Content-Type: image/png');
    + q8 O4 k( @* h4 X& t# i9 {
  6. // 1 创建画布
    3 S: F' h) P( a, M% H
  7. $image=imagecreatetruecolor(100, 30);# y6 {7 m5 N/ O! v& j
  8. // 2 画一个矩形并填充背景颜色为白色& b, h2 g1 `  t( w
  9. $color=imagecolorallocate($image, 255, 255, 255);6 C8 _5 @. w8 e, y  m% o
  10. imagefill($image, 20, 20, $color);
    : o4 _1 @- {" B& T6 {# i
  11. // for($i=0;$i<4;$i++){
    + ?% M- z' h( d5 O- d' I( J
  12.     // $font=6;
    , B0 R7 ]/ [2 J( |3 @, W2 z- t+ @
  13.     // $x=rand(5,10)+$i*100/4;
    ) }# ^, K: z  I% {! v- q. z7 ?7 e
  14.     // $y=rand(8, 15);
    ! R; M$ _- }& n9 g0 _+ b
  15.     // $string=rand(0, 9);/ l" {0 ~  w  o9 n3 b5 k
  16.     // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));
    5 a1 K" O0 \$ T! L
  17.     // imagestring($image, $font, $x, $y, $string, $color);
    + n# N$ ]  Q$ N: o8 @6 v$ M! D4 }
  18. // }
    9 P0 l, {0 G9 ?! S, M. k: d
  19. $code='';
    3 s* D! n% B( h3 j
  20. for($i=0;$i<4;$i++){
    + V/ _6 U8 H3 l$ q
  21.     $fontSize=8;
    . E1 F" ?' ~1 E
  22.     $x=rand(5,10)+$i*100/4;
    0 X- g4 Z/ u8 x4 A$ H
  23.     $y=rand(5, 15);$ x) G$ H; x- m! T3 N" Q% G2 N( V
  24.     $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
    $ N$ D1 s, ]/ L" O
  25.     $string=substr($data,rand(0, strlen($data)),1);
    6 e$ S6 U3 i) Z3 h! G. `1 ^$ P
  26.     $code.=$string;
    ) c4 _* _2 B2 {' K
  27.         // 3.2 加载自定义字体7 x, H$ Z: d2 S) t% P
  28.         $font_file = '1.ttf';! |9 J% U! {% ]" H. b# j( t% x
  29.     $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));
    " s% F% {  a3 H" H( \
  30.         // 把文字填充到画布
    - Z8 K( x2 j4 @+ R! @
  31.         //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);
    3 ^, h5 M( D& u$ V& i6 J6 ]
  32.     imagestring($image, $fontSize, $x, $y, $string, $color);
    2 e/ A- ]& K$ E6 l3 B  N
  33. }
    . I& @  y  _3 g7 P" D( x! Y* o% B
  34. $_SESSION['code']=$code;//存储在session里
    3 J4 S/ C' F, k& v0 T$ p
  35. for($i=0;$i<200;$i++){0 h: U* E' V4 O& ~) Q; T$ q0 h5 x4 h
  36.     $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));. o- u' y. K7 ?
  37.     imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);. C+ P) T# E- Q7 S8 [
  38. }2 g* a8 G5 X: p& V5 F( E* G: J
  39. for($i=0;$i<2;$i++){+ V' D1 {( q/ K
  40.     $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));9 u7 T) m2 ^) P+ I
  41.     imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);
    & Y, G' r8 N: x% l1 O
  42. }
    . G; k1 Q5 c0 K) d$ X( g
  43. imagepng($image);. \  c! ?! P6 Y
  44. imagedestroy($image);
    . I% ^2 d7 ~# I7 T' k
  45. 7 `4 B) l2 d4 z; c9 y
  46. ?>
复制代码

7 ~6 p2 h* ^% e( Oform.php- S7 v6 K5 a9 L- w* q
  1. <?php; v' k: e" o5 [( G. B# @: s
  2.     if(isset($_REQUEST['code'])){
    ( \- R: K, U' \+ @4 Q( _
  3.          session_start();
    / K8 D5 F" S4 k# ?7 o9 |2 ?1 c4 w
  4.         if($_REQUEST['code']==$_SESSION['code']){" N$ X6 Z' T5 A/ X, \8 n. D6 R
  5.             echo "<font color='red'>输入正确</font>";( A; |# e- m2 A4 W
  6.         }else{
    # D% i! E  H3 C+ @: x2 ~
  7.             echo "<font color='red'>输入错误</font>";4 D# O6 F3 u8 L
  8.         }  H5 E- }  l/ I# R3 n  L
  9.     }
    5 l+ U7 V( ]! Z6 H& z, B# _
  10. ?>1 t+ p3 _, u& C; ^3 l; u
  11. <!DOCTYPE html>+ x% w, L; ]) N1 T2 Y
  12. <html>( R+ w" r% ^- u; j: e4 u  J
  13.     <head>
    5 z& w7 E) q$ O4 x
  14.         <meta chartset="UTF-8" />
    " ?! R1 r7 x7 f/ s' E; ~
  15.         <style type="text/css" rel="stylesheet">" ?0 _! k/ c3 m6 K5 I+ z( A6 @
  16.             a{
    ( q- _( e3 n$ M
  17.                 text-decoration:none;2 R/ l( v* H" a0 `2 n: j7 ~
  18.                 font-size:30px;
    # A( x4 Q, q1 s8 }* p0 L8 d+ ^
  19.                 color:blue;9 C2 u% @1 z; e' T- P
  20.             }2 P: \- p: b) f. o2 @. l/ B
  21.             a:hover{
    + Q) C4 g' S4 i& D' F
  22.                 text-decoration:underline;
    6 j5 C3 }& k) s- }! e  S  A  H# {
  23.             }
    5 R, |( _# j4 ?! C# P$ s' J. C
  24.         </style>
    * u" m8 H/ z8 E, H
  25.     </head>" f0 I- Y2 c6 @2 p  Y4 M
  26.     <body>0 E+ O; p1 e: ~& O( c* {& f
  27.         <form action="form.php" method="get">
    - E* _1 s+ s) D/ u  `. z, a  k
  28.             验证码:  <img id="img" src="index.php?" onclick="changeCode()"/>
    ( l. Z) n- ^. Q6 S+ U5 Q
  29.             <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />
    % L$ D4 [* H) j9 p8 b. `; v
  30.             请输入验证码: <input name="code" /><br />' \4 P( i3 V! R. [
  31.             <input type="submit" value="提交" />
    9 ]9 f* r( k* X  L! f" V- e5 N
  32.         </form>
    7 L) F* L" u/ M1 S% R4 o
  33.     </body>
    . F1 Y  g& g7 T8 o) r& U) X
  34.     <script type="text/javascript">
    : @, H( N3 F1 n0 @! _
  35.         
    0 I  }/ V4 P8 @: W& y4 q" a6 s
  36.         function changeCode(){
    1 b  B! m# ~# v" p* g* F
  37.             var img=document.getElementById('img');
    5 H( a% v5 x1 F( h% T( n
  38.             //img.src='index.php?r='+Math.random();; ?! u5 ]/ i- l" y$ }: H  P, M9 u
  39.             img.setAttribute('src','index.php?r='+Math.random());* f' k8 x3 K; i% W( G( i3 t  Z
  40.         }
    : X( {2 N& _& w# L' v( [- Y
  41.     </script>
    : I; _( x& T6 h: q1 }
  42. </html>
复制代码
& {; {- R' `0 j$ h+ t

; Y; a. g( Q3 x6 t; O8 t
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2024-9-29 07:21 , Processed in 0.111086 second(s), 20 queries .

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