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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[php学习资料] 验证码

[复制链接]
跳转到指定楼层
楼主
发表于 2018-12-16 01:00:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
index.php2 ^% F$ l! V! n/ S5 k
  1. <?php
    " z# Z0 n: y- J. I( H
  2. session_start();9 M2 @1 u4 b1 B' e1 j5 o% S; w
  3. // 丢弃输出缓冲区的内容 **
      \7 b& ~4 q1 b
  4. ob_clean();9 }" Y5 I  ]1 L
  5. header ('Content-Type: image/png');+ G7 ]& {. Y/ `" D0 n
  6. // 1 创建画布
    3 G4 O0 M8 A, o: c2 `
  7. $image=imagecreatetruecolor(100, 30);
    / w. ^4 y4 v1 H$ p6 @& \
  8. // 2 画一个矩形并填充背景颜色为白色3 y$ k' \/ x8 |- N
  9. $color=imagecolorallocate($image, 255, 255, 255);
    . t% Y1 s# _, W, @3 i
  10. imagefill($image, 20, 20, $color);
    & {6 I8 X/ F, M  V
  11. // for($i=0;$i<4;$i++){
    8 f$ D5 S: F& G
  12.     // $font=6;$ @& Q. E1 ~8 ]; n
  13.     // $x=rand(5,10)+$i*100/4;5 A! P6 r' W, ^0 m- D
  14.     // $y=rand(8, 15);
    5 Y0 \2 V1 _& o: h2 d. y
  15.     // $string=rand(0, 9);
      s9 p" z+ e2 Y- `: d% o9 I
  16.     // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));* }& j/ P$ x0 V+ v8 c- _
  17.     // imagestring($image, $font, $x, $y, $string, $color);; t& c) N( U) a) }. S3 y5 x$ h
  18. // }
    ( x  [' a& T) @5 p: Y3 G1 J0 P  z$ g
  19. $code='';* @0 a) w* B7 ~0 C2 ~
  20. for($i=0;$i<4;$i++){- m1 d+ y  @) Z* c1 ^$ s# B
  21.     $fontSize=8;
    7 A! b4 R; v$ d/ p( e8 s( w# K
  22.     $x=rand(5,10)+$i*100/4;
    # I+ C& R6 r( [7 S: {! K
  23.     $y=rand(5, 15);
    " V; T; \% N3 g; q$ |+ L2 f
  24.     $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';4 C5 v# Q% b% X# m- z2 v
  25.     $string=substr($data,rand(0, strlen($data)),1);9 L3 x) [; x5 v7 E
  26.     $code.=$string;% h! u" L6 Q3 Y! U2 Z% }, `
  27.         // 3.2 加载自定义字体
    , Z  o- V" A5 F5 g, |0 c
  28.         $font_file = '1.ttf';
    : E! ~6 p- \0 W" q8 H9 \- f
  29.     $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));8 z8 h7 f& `* [, `' L2 y, g+ r
  30.         // 把文字填充到画布
    8 x$ m: l, ?' ]3 @
  31.         //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);
    / M" {7 F% E$ o
  32.     imagestring($image, $fontSize, $x, $y, $string, $color);1 Q8 A" q) Y% g$ u
  33. }
    % O( f3 n- k+ a: K1 |4 a
  34. $_SESSION['code']=$code;//存储在session里! D) `! z. d4 T
  35. for($i=0;$i<200;$i++){1 E* S3 P* {$ d' u
  36.     $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));+ k. R, U8 t* J; M! m; f
  37.     imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);: @! F  @! F" a% C+ V( e7 H
  38. }
    6 H+ l  L7 A$ e) G
  39. for($i=0;$i<2;$i++){
    * |5 ~% U9 ~* h
  40.     $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));
    1 K- ^: [2 O3 V: G4 n" q/ d
  41.     imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);
    3 A9 S* L) n9 s0 u# t
  42. }
    : K" P3 F& v% x6 ~  z( Y. X
  43. imagepng($image);* [6 q7 X% |" S0 r. B+ e. c5 z" w
  44. imagedestroy($image);
    % Z2 w0 y( }% t

  45. ' B- S: Z. ?9 ?7 i
  46. ?>
复制代码
6 l) I' H! P3 @! |2 \$ N- Q
form.php
4 r) T7 m! X. R/ c' L
  1. <?php- e+ B4 m. j: w  w% E- J
  2.     if(isset($_REQUEST['code'])){4 V7 e6 K' b; `$ @% C
  3.          session_start();$ R8 {6 y& N# P. {
  4.         if($_REQUEST['code']==$_SESSION['code']){$ @3 X0 @  B8 }% ]
  5.             echo "<font color='red'>输入正确</font>";. Z; a. g+ f' H/ D" w) Z
  6.         }else{  k! B3 Z8 Q  w
  7.             echo "<font color='red'>输入错误</font>";0 R8 D: W/ Q9 s! f2 A  `# x
  8.         }6 I& S" ^, i  b$ u: X" `
  9.     }  N+ o4 F- ?8 ]6 N3 m
  10. ?>
    ) M. l4 h6 S5 X% L( z! l# q
  11. <!DOCTYPE html>0 V4 c2 J2 ]0 I5 q
  12. <html>
    * c& v- d- I! i- q- G' {8 }- G" I
  13.     <head>
    - f, i% Z3 p! E; W
  14.         <meta chartset="UTF-8" />+ t: y( O8 l) i, I$ |5 A7 X5 ^
  15.         <style type="text/css" rel="stylesheet">" b" s% H! J+ Y; q8 }, f
  16.             a{& v5 a3 w* v$ p. y+ d0 j# h
  17.                 text-decoration:none;3 o& i' t3 A' R- y7 s. c3 ^- \
  18.                 font-size:30px;
    1 G) W$ O% `1 o8 e( i
  19.                 color:blue;
    5 p3 _8 D; q2 e1 c3 E
  20.             }
    + m/ j/ P& V6 ]) Y( h
  21.             a:hover{
    * Q- d, O2 |4 T1 [, a
  22.                 text-decoration:underline;9 }- X* @! j( o$ K" _' C; r, e/ K
  23.             }% H+ _+ ?: u4 D. H
  24.         </style>; c+ n" s3 c$ c$ I% e
  25.     </head>9 T9 e' j$ q/ ], n( b
  26.     <body>! l3 m" U6 _1 U4 \  M- W
  27.         <form action="form.php" method="get">
    # L7 s; I  l  S! i, S! s
  28.             验证码:  <img id="img" src="index.php?" onclick="changeCode()"/>2 }. L% p3 H4 N
  29.             <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />9 }6 W7 h) F% v$ x6 Y
  30.             请输入验证码: <input name="code" /><br />" c2 W) }% _% e7 c- e) J3 |! Y
  31.             <input type="submit" value="提交" />+ n1 F! y' o1 w) l6 [6 N
  32.         </form>
    " y8 G; P1 F, e! s9 l; {9 `
  33.     </body>. R: i  k9 h  q5 Q& k  r7 t+ U
  34.     <script type="text/javascript">  W+ ~* a9 D1 \& Y' n1 X" V
  35.         - x/ ]( {3 q( y  j( j+ Q1 }
  36.         function changeCode(){
    $ ~1 K5 `3 S- w* e
  37.             var img=document.getElementById('img');7 [3 r5 y. @4 G6 P! |$ ?( t
  38.             //img.src='index.php?r='+Math.random();2 C- q1 V* b5 s' n( }$ q9 K, t  z& W
  39.             img.setAttribute('src','index.php?r='+Math.random());3 p0 G0 a+ K2 p- w
  40.         }
    4 ~1 G; q$ }: d
  41.     </script>
    . b& q3 R& J* h1 T5 k0 W) ?, `
  42. </html>
复制代码

3 {: z+ U8 P0 A
/ h  a/ \1 }0 K- I9 \  I' T3 K' }6 }
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-5-2 17:39 , Processed in 0.086480 second(s), 22 queries .

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