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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[php学习资料] 验证码

[复制链接]
跳转到指定楼层
楼主
发表于 2018-12-16 01:00:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
index.php3 S: y: |$ W6 K/ y; e
  1. <?php5 M7 J1 k2 u6 Z/ C
  2. session_start();1 U: [+ e, t3 a) W  I% @. f: o7 [
  3. // 丢弃输出缓冲区的内容 **
    ' _0 r" ^8 Y, [' @7 j7 l
  4. ob_clean();
    3 b1 s) l; I5 G) g
  5. header ('Content-Type: image/png');
    0 Z" k/ \! C* }- I9 T. `
  6. // 1 创建画布/ H' E& E9 j) O
  7. $image=imagecreatetruecolor(100, 30);4 s* j7 ^% j( U5 S
  8. // 2 画一个矩形并填充背景颜色为白色
    + j$ m/ U. f5 E$ A8 t5 H" @
  9. $color=imagecolorallocate($image, 255, 255, 255);
    * G6 J+ r9 D/ e' A+ }
  10. imagefill($image, 20, 20, $color);
    % n/ @; O! {+ a) G
  11. // for($i=0;$i<4;$i++){
      X& p; s, ^; A( {# f2 I
  12.     // $font=6;7 E$ e8 e  n$ V+ s& I
  13.     // $x=rand(5,10)+$i*100/4;, }5 `" N. }" n( D( U, y& ]
  14.     // $y=rand(8, 15);
    & s& k$ V2 ?: _4 r0 u/ E0 t( {4 }
  15.     // $string=rand(0, 9);0 Y9 j' x* B/ d# m. ?
  16.     // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));7 V* Y* w+ x6 s2 @, E3 q% C
  17.     // imagestring($image, $font, $x, $y, $string, $color);; l1 D, g* J  \2 U3 w6 m
  18. // }" |* D1 F# N+ C, R
  19. $code='';
    / Q5 g( ]8 P7 d, `6 y4 r: X
  20. for($i=0;$i<4;$i++){$ w, D  g/ T' K5 D' ]- d
  21.     $fontSize=8;9 G& }& D: |/ G; [( x$ M
  22.     $x=rand(5,10)+$i*100/4;* K/ {1 o6 x" D5 l* o) w
  23.     $y=rand(5, 15);1 i! n( I( o2 u% K7 c" D' |' r, Q) Y, W
  24.     $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';* U! ~% p) U; |# d$ c
  25.     $string=substr($data,rand(0, strlen($data)),1);+ X" l* `. l# d5 z& d+ \
  26.     $code.=$string;
    & o- b& X+ {; V' u* p( H' L/ O
  27.         // 3.2 加载自定义字体
    4 ?. {" B  D5 L2 H. Y4 s' b
  28.         $font_file = '1.ttf';
    3 [# m% N( i' Q( m
  29.     $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));
    - [- a" k$ l. `, \/ P1 K3 o
  30.         // 把文字填充到画布
    " P3 n7 N3 g9 ^; b6 [( B& ~% x
  31.         //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);. }% u9 }9 p" \* i, K3 N4 C
  32.     imagestring($image, $fontSize, $x, $y, $string, $color);  {7 z% y, {+ _  V; }
  33. }# W8 t, d6 B; I2 k2 x6 |5 Q2 N0 p
  34. $_SESSION['code']=$code;//存储在session里+ D! _6 G9 b$ |, x' `% g
  35. for($i=0;$i<200;$i++){
    2 Q; T6 g  p% j- U% @
  36.     $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));0 N, i! I+ x0 y1 W
  37.     imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);: \& a' U9 W. h
  38. }# g1 X  E6 n# Y& K+ _7 O' J
  39. for($i=0;$i<2;$i++){1 L( S( c0 b) p/ n1 d* G
  40.     $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));* i+ h3 V) ]" Y/ f  Q- f
  41.     imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);
    3 c) j7 ]6 H. @% ~
  42. }- A, A9 K. l. g* v' J3 r! m" o, h: ]
  43. imagepng($image);
    4 W0 t( {; w0 E  M- G8 W7 p
  44. imagedestroy($image);
    6 I9 X$ i( w# T
  45. 1 _3 u: y9 n6 M% E, n, u
  46. ?>
复制代码

* T0 \) s5 C; E" I3 O+ W/ H" j7 |form.php7 s. j* f+ ~5 X% K# X; D
  1. <?php  z( p& O( s  L3 P
  2.     if(isset($_REQUEST['code'])){
    3 G7 T: p* {6 _  h" i) \2 K  w1 p
  3.          session_start();' p5 B) K7 A5 E; b' v% g
  4.         if($_REQUEST['code']==$_SESSION['code']){3 P) m- z+ D6 A/ u% J
  5.             echo "<font color='red'>输入正确</font>";
    3 c' |6 V9 J. A+ O+ _: s
  6.         }else{
    / z! D1 M- b# Q8 I8 O
  7.             echo "<font color='red'>输入错误</font>";
    / n* _1 s8 B3 X. i  x+ F
  8.         }
    # k) r" ^9 R& Q5 D) \
  9.     }4 J' j  P1 v9 B+ L% }
  10. ?>' z; }3 c9 O# W/ o& T
  11. <!DOCTYPE html>
    0 @8 ?1 y" K  H" s% v1 M4 n
  12. <html>
    ) W5 Q3 [+ s% D
  13.     <head>
    & i+ _/ {; b% G
  14.         <meta chartset="UTF-8" />' `5 {+ N/ i- O1 S6 `
  15.         <style type="text/css" rel="stylesheet">$ k: L$ `+ [8 u# J
  16.             a{
    ( W: G/ S2 ~9 b/ \/ @$ Q# @
  17.                 text-decoration:none;/ H+ p6 o/ T  E; w: @) {
  18.                 font-size:30px;
    5 O; r) F5 a4 N0 Q
  19.                 color:blue;5 ?% ~" j4 ]2 C7 `/ G+ v3 g
  20.             }% w+ {: R4 a4 @9 C
  21.             a:hover{
    0 \$ f- M+ d  q! q; G
  22.                 text-decoration:underline;
    6 p5 @/ O' v( [' f7 Z
  23.             }
    : k: Q/ I5 X3 N- }9 x
  24.         </style>
    & v2 l- }- D' r# d& \
  25.     </head>
    : e$ w2 _" h7 W: N! p0 L3 h
  26.     <body>( K- F4 X; P" X2 h
  27.         <form action="form.php" method="get">
    9 {4 p, J8 |8 Y
  28.             验证码:  <img id="img" src="index.php?" onclick="changeCode()"/>+ l8 s. a6 d' p9 x& p: v3 P
  29.             <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />
    " M' _1 y% _5 L* ~
  30.             请输入验证码: <input name="code" /><br />
    & v! S: G$ y. C
  31.             <input type="submit" value="提交" />
    8 \* N; j+ a( x0 s4 {2 [: o
  32.         </form>8 c6 r- m. s" @
  33.     </body>$ Q! o' Y; W/ Q0 h# m5 v
  34.     <script type="text/javascript">
    : S1 K, q4 U" t; N
  35.         
    " T, D6 e" u( [5 n3 X. U' `3 {- y  G
  36.         function changeCode(){
    9 z/ A9 p7 S5 E" Y4 c, J3 N
  37.             var img=document.getElementById('img');. g/ }, Z" _' i, `( x: Y0 m) `. @
  38.             //img.src='index.php?r='+Math.random();: Q% F8 y0 f. ]6 s# Z1 s0 M
  39.             img.setAttribute('src','index.php?r='+Math.random());# |) I! C) q8 I) o/ m. D7 z
  40.         }, A& N3 x3 [( e0 w# B; R
  41.     </script>5 A( D) c# F1 D: y4 n5 ~6 h
  42. </html>
复制代码

7 ?( t0 t. B  ?" M3 {& u: P* Z' d5 g2 |6 h! q& P: v4 V" z
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2024-5-20 05:40 , Processed in 0.142571 second(s), 19 queries .

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