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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[php学习资料] 验证码

[复制链接]
跳转到指定楼层
楼主
发表于 2018-12-16 01:00:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
index.php6 U! z, o0 E! s6 Z0 B
  1. <?php
    . ^9 M1 k( u9 P1 ?8 p+ I. k, v2 D
  2. session_start();/ e0 F+ N5 u! B0 m( h3 J: r. ^
  3. // 丢弃输出缓冲区的内容 **' J1 _9 l) @6 \# Y
  4. ob_clean();
    - h& l4 B  g- D$ _+ T0 T
  5. header ('Content-Type: image/png');  C9 C7 W( F! }5 O3 f
  6. // 1 创建画布
    2 u% j6 A& S  j: e- ^
  7. $image=imagecreatetruecolor(100, 30);  A5 s& n' E: ?+ J
  8. // 2 画一个矩形并填充背景颜色为白色6 I% G" E, p( K3 \4 n( d4 M: D' V
  9. $color=imagecolorallocate($image, 255, 255, 255);
    + d4 j" z/ Q6 W+ _
  10. imagefill($image, 20, 20, $color);
    4 {0 h; i8 ?/ w' {0 c: Q
  11. // for($i=0;$i<4;$i++){
    : V9 {& M$ d# Q8 _3 o" i* H3 Z
  12.     // $font=6;
    ( Z' e' E, G/ E/ x& |
  13.     // $x=rand(5,10)+$i*100/4;
    / E; A9 s! E; R" H( L3 W
  14.     // $y=rand(8, 15);
    6 W6 Y% t- K, Q. Q
  15.     // $string=rand(0, 9);
    % U) y- j+ s7 h- @7 C9 w+ k
  16.     // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));, N) {4 A. w/ K: v' p
  17.     // imagestring($image, $font, $x, $y, $string, $color);
    6 K+ q1 V9 U0 K" A! c, n5 u
  18. // }6 _) s5 J' Y# _* E% Q9 J
  19. $code='';
    : u  r1 m2 {; N  A
  20. for($i=0;$i<4;$i++){
    6 J4 U; b0 R" U- R; o) e  d4 @4 A
  21.     $fontSize=8;$ x  K5 t( j6 M$ B& U
  22.     $x=rand(5,10)+$i*100/4;% X5 h0 g. I: G& l2 C5 T& w
  23.     $y=rand(5, 15);
    $ H& w' P' t  B9 b# J6 e6 F
  24.     $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';; _# M) Q/ `5 n5 d/ H7 g
  25.     $string=substr($data,rand(0, strlen($data)),1);! q; k# _, Z" ^# I
  26.     $code.=$string;! K' ?( T4 {  Q  r! Q' d
  27.         // 3.2 加载自定义字体
    5 z5 s1 e- O( _  |4 e
  28.         $font_file = '1.ttf';
      d3 V& j3 _1 ]! C1 v; V
  29.     $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));
    # B' F( l8 w3 K. o* a' s  T7 A4 T
  30.         // 把文字填充到画布. L7 M# ]$ p* b* r' M0 ^! V( ^
  31.         //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);! F, }! z% e: N7 r6 W; e( c2 g
  32.     imagestring($image, $fontSize, $x, $y, $string, $color);8 z1 w! w, t, g  j
  33. }5 k6 e( |# x- L0 G& x6 U5 w5 L
  34. $_SESSION['code']=$code;//存储在session里6 z" G" o8 [) @5 l, j4 f. W
  35. for($i=0;$i<200;$i++){, F/ i. y6 k" X7 [! g5 }, `  [& O
  36.     $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));
    & l/ Z. j4 L- Q9 H! `9 Y
  37.     imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);
    0 h3 I, f* g$ P( z" O$ |: J
  38. }" i8 T, K& M) k/ M  L
  39. for($i=0;$i<2;$i++){
    7 {/ I: B; l! V; p  Y
  40.     $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));
    9 d2 l; Z/ ~# `6 q9 w9 S+ Y
  41.     imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);
    8 s3 {$ C5 u# o8 i( P6 Z
  42. }, \& Z( V8 ^2 ^4 e7 R
  43. imagepng($image);0 ^8 n1 w; y6 d& ^1 {
  44. imagedestroy($image);
    # d6 @& m$ o, i' t7 v% a) I; W

  45.   ]( b/ F* T! `  s
  46. ?>
复制代码

9 Y; |1 D9 ]0 n; z3 pform.php, m" l6 b4 W9 c6 [
  1. <?php9 V) `" |% H, z, b! c
  2.     if(isset($_REQUEST['code'])){
    4 i  j; V: q: a2 B3 Y- G2 D- h
  3.          session_start();  N2 H! Q4 _8 b, b# Q; W$ P
  4.         if($_REQUEST['code']==$_SESSION['code']){) V' p) i  z6 N, a* ?6 D# T2 |3 h) n
  5.             echo "<font color='red'>输入正确</font>";
    3 |( a1 o, S- w$ F
  6.         }else{8 m2 S# k' e( ?  U+ `) e  [. T0 ]
  7.             echo "<font color='red'>输入错误</font>";% J9 Q+ b& C. i* j0 [! o
  8.         }8 Q. {% I- h6 P5 _1 {8 M% i9 l4 I
  9.     }8 ~' O' g: J7 b( L
  10. ?>% q, D6 _2 v( k- I1 ^, _( k
  11. <!DOCTYPE html>3 Z( Z8 B! k: |. H' ?
  12. <html>
    # n$ T" y; g6 r. j
  13.     <head>
    + d  a. p) ~. T% C% Q9 D4 G% R0 s& w
  14.         <meta chartset="UTF-8" />8 j7 x9 H- ^* u/ Y, n/ }
  15.         <style type="text/css" rel="stylesheet">
    + Z' [% _, p& G$ y; ]$ o' B6 I
  16.             a{  x1 B$ C  ^5 c2 }/ t0 X" y* x$ M* K
  17.                 text-decoration:none;
    # O# w( n" Z( B  F% k
  18.                 font-size:30px;9 T/ W9 |/ r$ }* X
  19.                 color:blue;
    * B4 |- N, L" w* V' B
  20.             }. {2 [% W( l8 U+ g. S
  21.             a:hover{  @( G2 O# v5 ~) K, k
  22.                 text-decoration:underline;
    7 z7 K9 a% H% O1 a6 P6 a2 Y' ~
  23.             }' \7 {  @  Z" o* N0 w( f- y
  24.         </style>  j& _1 |7 m+ ]) N
  25.     </head>' Q! V+ Z7 H- u7 W
  26.     <body>& E  k1 T. f# R% ^$ ?: U. E/ z! E- C
  27.         <form action="form.php" method="get">9 X% f+ @" |6 J7 Y- F7 L
  28.             验证码:  <img id="img" src="index.php?" onclick="changeCode()"/>
    : p- t, L# v9 M, H1 e/ B( h
  29.             <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />; o$ T6 s8 e& M1 r
  30.             请输入验证码: <input name="code" /><br />
    2 V& E: _# j: S+ |; K* U) x( q
  31.             <input type="submit" value="提交" />
    # l* n* j5 [8 Q. a. S5 N9 O
  32.         </form>7 Y$ g& x7 L9 S: R
  33.     </body>
    1 o/ }9 m/ X( c6 Q% _. Q
  34.     <script type="text/javascript"># r5 {9 J9 z4 h" [: K
  35.         . b0 ~# U6 ?7 s
  36.         function changeCode(){
    % S5 D8 c% o* O5 @  H- v* S9 t
  37.             var img=document.getElementById('img');) X& J4 w) w9 Z, Z7 F. @* P
  38.             //img.src='index.php?r='+Math.random();% B) l) ^9 ]% x! D/ w8 R: {
  39.             img.setAttribute('src','index.php?r='+Math.random());
    0 r/ ]4 ~+ q) B% R4 N0 R; C  s
  40.         }, Z5 `0 a+ f' \3 \& X1 R
  41.     </script>
    8 Y$ ]: e; E6 d3 o' o
  42. </html>
复制代码

: X4 u$ q7 ^9 y! s# j
2 z& `3 B- {8 F" x$ B3 J
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-4-30 19:58 , Processed in 0.073221 second(s), 20 queries .

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