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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[php学习资料] 验证码

[复制链接]
跳转到指定楼层
楼主
发表于 2018-12-16 01:00:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
index.php- s7 q  A0 e$ h; g6 }" L2 `2 [' i
  1. <?php, P3 |/ t& h! v
  2. session_start();  J5 r! g+ }& I. w1 L
  3. // 丢弃输出缓冲区的内容 **
    . E# n0 }, M9 B. r, M1 J
  4. ob_clean();
    / O- G/ p& E" |# ^0 S) m
  5. header ('Content-Type: image/png');! H- m8 s. z0 ^9 c
  6. // 1 创建画布8 O; o3 A9 N! ?: Q7 x6 D' `) j7 l% A
  7. $image=imagecreatetruecolor(100, 30);8 m( s% M1 H& z: e) w3 q
  8. // 2 画一个矩形并填充背景颜色为白色- B% `- ^# W3 g1 }9 a
  9. $color=imagecolorallocate($image, 255, 255, 255);( i/ K8 o; r0 z
  10. imagefill($image, 20, 20, $color);- i3 w+ B  z% ~9 M
  11. // for($i=0;$i<4;$i++){  R) C8 b# n/ G5 f& o4 n6 j
  12.     // $font=6;8 [  c' b/ ?, f* p% K
  13.     // $x=rand(5,10)+$i*100/4;
    5 |$ I7 w8 L: u0 T
  14.     // $y=rand(8, 15);! E8 f7 O) F# R& v4 Y/ Q
  15.     // $string=rand(0, 9);
    - E  R5 k: M6 q/ Z6 Y, J0 n" [
  16.     // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));" H; w& @% M" W
  17.     // imagestring($image, $font, $x, $y, $string, $color);6 _" j7 k0 o, t4 u) v/ o" q0 W! o
  18. // }
    8 o, {, }0 H- |( s' ]
  19. $code='';
    4 i% Q% N6 s& N$ i
  20. for($i=0;$i<4;$i++){4 w) X$ ~7 G. ^. k' [( u
  21.     $fontSize=8;
    $ Y# c, u( m( i' N" h2 g& V
  22.     $x=rand(5,10)+$i*100/4;
    4 m) ?. `7 ^) a" ~$ p4 @
  23.     $y=rand(5, 15);
    1 Y  a6 Y# u0 N8 Z/ j) S
  24.     $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
    2 u6 h  n# B% n$ w) }
  25.     $string=substr($data,rand(0, strlen($data)),1);
      Z4 n! I% ^  X, R+ n& J3 \) F. L
  26.     $code.=$string;. ]4 k+ |  F3 r) B7 ^
  27.         // 3.2 加载自定义字体
    ' e2 Y4 k! z( f- a9 H/ h8 X3 B
  28.         $font_file = '1.ttf';! }% W0 W' T: k0 D( [, U. u
  29.     $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));
    ! P7 _- y" i6 [+ ^/ p' }
  30.         // 把文字填充到画布
    0 y9 `2 ~+ ~7 B$ u
  31.         //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);8 f5 ?" X6 D5 J1 h7 c5 s! i3 Q8 v
  32.     imagestring($image, $fontSize, $x, $y, $string, $color);
    4 Y7 y  F# @+ a1 n" @
  33. }& S( a, Y, i6 p$ _8 v0 u
  34. $_SESSION['code']=$code;//存储在session里4 ~0 {1 b1 G6 q! q: t- g* ]
  35. for($i=0;$i<200;$i++){
    - V2 t5 w' E4 p2 K/ v6 ]$ N4 R+ U
  36.     $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));. t4 z: e- w8 _) t7 D/ Q" _
  37.     imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);
    - Z; V1 F' A) Y1 M
  38. }1 i0 r& K# J* z7 o- d
  39. for($i=0;$i<2;$i++){
    " S  t  K/ |9 ~- g$ o
  40.     $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));8 A! Y( q7 Z9 N/ M* p% f
  41.     imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);
    / a- K1 Q# p. e, Y4 i( c
  42. }" M4 s# w0 I- ?2 ^/ Q
  43. imagepng($image);2 h0 W# h! A6 I& U# k: D7 _
  44. imagedestroy($image);2 c2 J- v9 \7 U/ y

  45. + B2 W6 B+ K- {
  46. ?>
复制代码
9 S0 n8 V. D9 r6 Q. E; ?
form.php
1 B( O' K( ]: k
  1. <?php; h  P9 Y" R& A9 v8 v% D/ M1 H) e5 l: ]
  2.     if(isset($_REQUEST['code'])){
    : g2 K. O+ M/ Y2 @$ s
  3.          session_start();  P2 O  k8 ~" J) t. @5 [
  4.         if($_REQUEST['code']==$_SESSION['code']){
    ! p* S1 m# q/ m' j4 K2 X/ `
  5.             echo "<font color='red'>输入正确</font>";
    & X$ q% i/ D6 N& R, P
  6.         }else{" u" d) R  f* b
  7.             echo "<font color='red'>输入错误</font>";6 ?5 Z1 x/ I5 u3 m1 `) s
  8.         }9 u# ]4 a- M6 I$ K6 g
  9.     }8 e7 h- x+ n; g& F$ Z
  10. ?>
    7 p$ p2 T" t& u$ I5 W; b* h' `3 q+ V
  11. <!DOCTYPE html>- Z: E7 {! Q0 K3 d- W8 H& Z0 ]3 ~' M
  12. <html>3 g( ]; z# F+ D+ l
  13.     <head>) K" ]  t- c' s: b8 m
  14.         <meta chartset="UTF-8" />
    ! w  f0 E' P4 {7 H0 B
  15.         <style type="text/css" rel="stylesheet">' u( n: m8 `; w1 a2 r
  16.             a{! z" z- d  |2 M
  17.                 text-decoration:none;- i% \! o0 h' B4 o+ W; b' Y, K
  18.                 font-size:30px;( r: p& X: {/ A* |( m
  19.                 color:blue;1 t6 ^7 _/ s3 H" M$ L( e$ o
  20.             }
    0 b& k3 t! h9 |/ }9 N( r  k- l
  21.             a:hover{; w: T( [. |9 ^' q; G
  22.                 text-decoration:underline;: E% [1 j! q; Y4 v
  23.             }1 F2 e: @0 G3 b
  24.         </style>- j0 G1 [  \& U% I
  25.     </head>
    4 O+ X6 }- N, n; {9 k& h
  26.     <body>
    ) N2 i4 G% \" F' M( u' |
  27.         <form action="form.php" method="get">
    + `  a6 M  G7 I& o" t0 f5 M$ t  @2 Q
  28.             验证码:  <img id="img" src="index.php?" onclick="changeCode()"/>- \# I& Z; M- W- R2 j9 g
  29.             <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />8 G4 b( C' o8 {' q8 m7 P
  30.             请输入验证码: <input name="code" /><br />
    ( `1 L4 T+ ^" z0 Y
  31.             <input type="submit" value="提交" />2 i- @) c2 \' a0 u6 a- [: [
  32.         </form># @: j! t0 Z7 s. }* l4 i
  33.     </body>$ G/ o! D& [& |0 P) Q# V7 J
  34.     <script type="text/javascript">
    8 `9 c, Q* ^0 c+ G# R, ^* [* U8 J
  35.           T7 ~$ |# j+ J
  36.         function changeCode(){
    - q5 ]( f% X& L' z1 h" W1 w
  37.             var img=document.getElementById('img');% c* T& v' x( s6 w3 ~
  38.             //img.src='index.php?r='+Math.random();
    # e3 w" A6 x! I7 K! X$ b& @( s
  39.             img.setAttribute('src','index.php?r='+Math.random());
    2 m3 l) t) w$ S! G4 A
  40.         }
    / f+ H1 D- }/ J) g6 h  ]
  41.     </script>( `1 p2 w# @: A; J5 V
  42. </html>
复制代码

  }8 O! ]0 ]2 P2 a' U8 ~: f) N- |/ s4 ^
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-3-16 17:20 , Processed in 0.066060 second(s), 21 queries .

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