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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[php学习资料] 验证码

[复制链接]
跳转到指定楼层
楼主
发表于 2018-12-16 01:00:13 | 显示全部楼层 回帖奖励 |倒序浏览 |阅读模式
index.php$ Z( P( ^- y! `! g, {. s
  1. <?php2 ]* @, n/ n  b9 n4 L/ C
  2. session_start();
    + P- j7 N. ], j& z. A
  3. // 丢弃输出缓冲区的内容 **
    3 ~* [# {- c) x% J" ?
  4. ob_clean();
    % z( T2 J6 m9 g& F( E" x- U2 U
  5. header ('Content-Type: image/png');  {! l7 N  j) h) g& p5 `9 E( h
  6. // 1 创建画布
    & E* n( F- C3 S, R
  7. $image=imagecreatetruecolor(100, 30);7 W* Z1 ~; H, O) W# {( A, S/ \( ~
  8. // 2 画一个矩形并填充背景颜色为白色- ~" |# F! l, w+ n, T( W: M( g
  9. $color=imagecolorallocate($image, 255, 255, 255);
    " s% S3 Z$ e1 K# z, U2 j! n. x
  10. imagefill($image, 20, 20, $color);
    5 Q* e! X4 P& i* G. a
  11. // for($i=0;$i<4;$i++){, P5 B" @+ B0 l+ Q8 m% Z6 g
  12.     // $font=6;
    7 T/ t: Q% R' ^! f- \8 k2 t$ l
  13.     // $x=rand(5,10)+$i*100/4;
    / u5 G5 ]$ f6 J- _
  14.     // $y=rand(8, 15);- m; d( [4 U2 X3 J
  15.     // $string=rand(0, 9);1 y: E" |. O0 P
  16.     // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));
    5 N0 T! W, d# H+ s+ K8 o; G
  17.     // imagestring($image, $font, $x, $y, $string, $color);
    1 m+ G, V7 o; I: t0 t1 P
  18. // }
    8 m3 _+ Y7 c$ s2 z, }
  19. $code='';' a0 ^0 ]$ z6 Z$ I5 Q' b
  20. for($i=0;$i<4;$i++){9 a8 Y- D7 Z, h) M+ j
  21.     $fontSize=8;2 d0 J% v& s. B; F6 D1 g) j
  22.     $x=rand(5,10)+$i*100/4;0 [. w2 H7 T" j
  23.     $y=rand(5, 15);( \' N4 @4 G. U7 t" I; G5 b
  24.     $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
    3 C6 \% O; g" p9 T; a! X
  25.     $string=substr($data,rand(0, strlen($data)),1);
    & r" ]" C, z. ]- w, i
  26.     $code.=$string;. @9 ]; b. v5 G
  27.         // 3.2 加载自定义字体
    . U$ A, U- o2 n/ C/ Q5 w
  28.         $font_file = '1.ttf';
    - f# ^$ @3 K0 W+ N; |3 ^
  29.     $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));
    + C+ w# b* I. P" f
  30.         // 把文字填充到画布
    ! r' i( }' q# w" G5 e, J: F" m
  31.         //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);
    $ {' k7 y5 }; h$ M1 {3 O
  32.     imagestring($image, $fontSize, $x, $y, $string, $color);
    . N) k% j  `" g! D$ q' l* g
  33. }
    ; B! J- H7 l! O# _
  34. $_SESSION['code']=$code;//存储在session里/ T: R+ d& E: F& U$ t% Z
  35. for($i=0;$i<200;$i++){
    0 x3 z. y, S3 c' F$ B! E
  36.     $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));. G+ J* D$ W0 O
  37.     imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);
    + h/ F" P4 ?( G: x4 S+ y: {
  38. }
    8 x9 N' u& C" E* G) U  s, _/ D
  39. for($i=0;$i<2;$i++){
    % P# P. m0 |3 M( }5 O
  40.     $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));2 k$ Q$ J1 b/ t
  41.     imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);6 |6 j1 }0 Q" p9 g8 F
  42. }
    9 l1 A6 o* Y9 B8 `
  43. imagepng($image);& m9 U% h. C& Q3 j
  44. imagedestroy($image);
    1 h4 O# K$ m" F9 r/ l* W
  45. 0 ]6 v5 x" D- V" y0 Q* ^
  46. ?>
复制代码
* M6 j8 A; g* g4 I0 i
form.php7 H: t. t, f6 H# b7 _% H) f3 r8 [
  1. <?php
    0 u. r  a' R* T" j
  2.     if(isset($_REQUEST['code'])){9 w) `; L8 ^, V! ?; R
  3.          session_start();! q! Z0 u; ^) y4 Y' q/ `2 C
  4.         if($_REQUEST['code']==$_SESSION['code']){& Z. x/ |0 Y2 x4 e, Z' q
  5.             echo "<font color='red'>输入正确</font>";
    - v1 d0 g  [; |3 |' f; O; s% g
  6.         }else{/ ~* V( |% w- s) u8 \" J2 _3 x
  7.             echo "<font color='red'>输入错误</font>";
    - C# E& p, b* S! y1 x! e* O
  8.         }+ {  _& i- Y/ k9 [+ `+ D2 t. V
  9.     }
    0 J. _* O2 M- i' J2 ^! N6 Q
  10. ?>1 l5 |3 g8 J1 g: ]; l# H( ^
  11. <!DOCTYPE html>, h( [0 e+ Z9 c& ]% n
  12. <html>1 n5 Z6 ~& k! J
  13.     <head>+ a8 H; l( m0 E+ q8 ?7 X" W
  14.         <meta chartset="UTF-8" />2 p  _7 S% j. p- I7 h: S- o
  15.         <style type="text/css" rel="stylesheet">
    " A+ U# W) p8 i+ r  X3 j* z* `# `, r
  16.             a{4 b4 V( K) \* \7 ?! J: J7 ~; E; F
  17.                 text-decoration:none;* e8 e. W* W3 @8 n5 c+ q! |
  18.                 font-size:30px;& @/ ~9 K  o. a# u( e6 T8 t! |
  19.                 color:blue;
    ' s3 P% \* Y" [: z8 ]
  20.             }- `) }9 a' w1 f4 d: I6 i" v$ S
  21.             a:hover{  r( P6 B0 k- t
  22.                 text-decoration:underline;
    8 ]# S) M8 Y/ |+ `
  23.             }
    ( n, D+ u( |$ c" `: ?2 |' |
  24.         </style>: K# m( B- _; D- x9 q
  25.     </head>
    ! J0 D' M5 ?- x, e
  26.     <body>$ N" u: P- Q$ o' N3 u+ t# m- q# M
  27.         <form action="form.php" method="get">
    ) O& U# @2 t* Z+ |* b" |' a5 X
  28.             验证码:  <img id="img" src="index.php?" onclick="changeCode()"/>/ I0 M6 f( a7 r5 Q6 R. s/ V5 i) h6 g
  29.             <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />) t, N7 q  A/ w+ B$ b
  30.             请输入验证码: <input name="code" /><br />: Z; O3 d5 f3 ]# W" A( y: v
  31.             <input type="submit" value="提交" />
    ( h9 A- Y4 T2 Z) M) E; b1 o$ I1 y
  32.         </form>9 y, v3 o6 A1 m9 p: z
  33.     </body>
    + h" e8 R. Y& o9 m$ o5 z# c
  34.     <script type="text/javascript">+ Q  y  i( m" N, p% M  h
  35.         / W( g/ D/ e6 e9 e
  36.         function changeCode(){
    & d% l+ ~+ B' ]
  37.             var img=document.getElementById('img');
    , K+ Z) k4 r; w+ c' R  K9 B' z2 }
  38.             //img.src='index.php?r='+Math.random();! _: Z9 e! a* {$ J# ]
  39.             img.setAttribute('src','index.php?r='+Math.random());
    / U" m  H! x+ _* \; b1 C. H
  40.         }
    ! q& J7 U+ w$ J6 n7 \
  41.     </script>
    / j" ]  D  M+ Y, z3 ^
  42. </html>
复制代码
# G, K8 f5 ]/ n0 d9 P
5 I- v- s3 w" U
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2024-5-20 00:32 , Processed in 0.107693 second(s), 19 queries .

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