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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[php学习资料] 验证码

[复制链接]
跳转到指定楼层
楼主
发表于 2018-12-16 01:00:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
index.php
5 S, J  f9 n; Z7 j. b
  1. <?php: o5 f, @* g4 n- l! v. K8 C
  2. session_start();
    * `5 J1 Q. J- \( z' D
  3. // 丢弃输出缓冲区的内容 **
    ! ?$ M" N8 m! v* ~
  4. ob_clean();
    1 }2 @+ ]5 }5 M4 e! P
  5. header ('Content-Type: image/png');
    3 Q: z2 g9 `0 z8 d7 R
  6. // 1 创建画布; N; E8 h/ W: R* i9 h6 K  y' C* q' @
  7. $image=imagecreatetruecolor(100, 30);: U7 l) {/ S) |9 a4 ^
  8. // 2 画一个矩形并填充背景颜色为白色
    2 }$ I" A) Z1 u
  9. $color=imagecolorallocate($image, 255, 255, 255);" ^4 J4 q  t. D9 N0 b# g
  10. imagefill($image, 20, 20, $color);& F0 @0 c$ G5 J3 K, l) m# N1 y
  11. // for($i=0;$i<4;$i++){
    3 U$ `, h) e/ W/ }- E* D' f
  12.     // $font=6;
    0 X6 d/ f# T. g& l& ]5 K
  13.     // $x=rand(5,10)+$i*100/4;
    - X& K( k1 \9 J/ h; u4 G# `
  14.     // $y=rand(8, 15);( Y; n- B3 I# s8 g
  15.     // $string=rand(0, 9);  h2 R7 x! a/ l" _8 x
  16.     // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));
    4 C% ^3 M. W7 Y
  17.     // imagestring($image, $font, $x, $y, $string, $color);% L4 V" K- d4 f/ M8 i- ]
  18. // }
    % C% X. Z$ t1 {4 c; o
  19. $code='';7 Q4 ^" B. P6 a4 x+ R$ ?
  20. for($i=0;$i<4;$i++){
    7 b$ m2 l1 L* C7 Y
  21.     $fontSize=8;
    7 g4 D) |4 ]1 D0 E9 T9 U
  22.     $x=rand(5,10)+$i*100/4;
    ; K( U6 ~5 M6 w8 _0 A8 \! P
  23.     $y=rand(5, 15);
    5 N% v" y$ B! d+ P
  24.     $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
    ) y3 d5 N- n# a! ~2 H' i7 |
  25.     $string=substr($data,rand(0, strlen($data)),1);
    ( ?, I* p, f8 \2 q( \
  26.     $code.=$string;$ Y- d! w* k7 i& w4 j
  27.         // 3.2 加载自定义字体' O) o) q/ e$ C( x
  28.         $font_file = '1.ttf';
    $ n" l: `6 I5 X8 x
  29.     $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));
    , p4 z( Z6 C3 ~* D' ^
  30.         // 把文字填充到画布8 E; K5 S. X+ A+ P( K9 P" i/ l2 b4 V
  31.         //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);) j+ l4 H3 m$ ~3 S8 q9 i, E* ]: K; ^3 q
  32.     imagestring($image, $fontSize, $x, $y, $string, $color);1 H0 W3 j3 c9 r  Z" a. k
  33. }
    " A$ j: a2 v9 }
  34. $_SESSION['code']=$code;//存储在session里
    " v/ F7 `% e- a
  35. for($i=0;$i<200;$i++){! `4 u3 ]4 w. f$ j7 C0 {
  36.     $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));: m/ z! D( f8 b
  37.     imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);
    1 W9 {/ x( w% e) P2 H5 k
  38. }
    5 ~1 R. T3 E1 @! e* w
  39. for($i=0;$i<2;$i++){
    : Y2 `7 I" \$ u0 {3 H( i, d
  40.     $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));
    . F2 x" T, f2 J) S
  41.     imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);
    ( n2 U1 }5 g) [1 W4 R+ A0 u
  42. }6 {* c# U3 \3 @
  43. imagepng($image);
    * ^6 @( Z1 `4 V& t  z6 f
  44. imagedestroy($image);+ [, A7 u; s1 t$ b& g& q: k
  45. & B8 i0 q0 H. r$ y6 m) N
  46. ?>
复制代码
5 |0 m( c& ^  u
form.php
' I8 p7 [7 \7 N/ @0 v5 ^, n
  1. <?php
    / o$ Y' C5 `9 Q4 b
  2.     if(isset($_REQUEST['code'])){2 G) {3 q6 S$ o
  3.          session_start();6 ?' s1 L9 D; R- y
  4.         if($_REQUEST['code']==$_SESSION['code']){- ^9 M4 L* Z; i- ]/ Y
  5.             echo "<font color='red'>输入正确</font>";
    $ M$ v3 H$ n2 j2 s/ H! ?4 x
  6.         }else{' _, R+ k( |; K" B: q# ?
  7.             echo "<font color='red'>输入错误</font>";
    3 i  v9 u0 A3 J) {. _" K: Z
  8.         }
    9 {: s6 t/ N2 v; F/ P4 Q$ {2 z
  9.     }1 `9 T; S; \6 [7 _: d& u
  10. ?>2 Y0 |. h2 |" G- m; ]
  11. <!DOCTYPE html>
    5 P4 M  a; l5 z* E* c
  12. <html>
    : ~2 S' B2 t: |, o+ {& \; S: H) ]
  13.     <head>- |7 H- U* ^2 [2 N$ m& U
  14.         <meta chartset="UTF-8" />
    " h( Z7 F; S- J* `* O" B/ g
  15.         <style type="text/css" rel="stylesheet">
    3 ]. u5 ?' D, b  [
  16.             a{
    ! T/ Z- S* y! [' D2 K% n3 {
  17.                 text-decoration:none;& |) r7 j& u8 A3 b$ A2 ^2 _
  18.                 font-size:30px;
    ' U' c% u8 I6 `
  19.                 color:blue;+ ?. a% J0 ], E; v3 ?) V) t7 X
  20.             }4 x" i# ]  n3 ?* f
  21.             a:hover{4 U6 I4 U! h. a' F$ E
  22.                 text-decoration:underline;3 r" C# R# f) q1 W
  23.             }+ A1 Q& P# G% u3 u1 ^% c# x, }
  24.         </style>; B1 a3 T( B# @- t
  25.     </head>
    0 }% T# I+ v) y( m9 Q5 B9 R6 H
  26.     <body>+ X3 `6 f  }5 f
  27.         <form action="form.php" method="get">
    ; D0 Y  r) [9 W
  28.             验证码:  <img id="img" src="index.php?" onclick="changeCode()"/>. i% A5 |4 L0 b( ^6 a
  29.             <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />
    - M" ~, |  _, R0 L
  30.             请输入验证码: <input name="code" /><br />3 x7 ?& }7 ]0 C
  31.             <input type="submit" value="提交" />" @5 _6 L5 K4 K( i7 \; C* B+ e+ Z
  32.         </form>& }8 |* B. j) n8 m* m. j
  33.     </body>/ g+ T# x% w; y  e$ }/ _5 v0 e
  34.     <script type="text/javascript">1 @. o) ]8 E7 T4 k. P6 o
  35.         
      z  L3 x! y" Y; ^+ n/ p  r
  36.         function changeCode(){! W3 j+ V9 K6 F2 w, G8 J9 Y: j7 x, B
  37.             var img=document.getElementById('img');
    ! v7 J% Z' z/ Q7 g& l
  38.             //img.src='index.php?r='+Math.random();( a- E/ _6 T. B1 w2 F  e
  39.             img.setAttribute('src','index.php?r='+Math.random());" z' e9 [1 i1 N
  40.         }. [) N1 l. ]: m6 v% D  r
  41.     </script>1 u$ i1 M1 w. g
  42. </html>
复制代码

/ o! K& A0 Q6 t( _+ w5 K+ w0 e
: h. R- I9 {" G1 {+ S4 @
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-3-16 18:11 , Processed in 0.084529 second(s), 20 queries .

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