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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[php学习资料] 验证码

[复制链接]
跳转到指定楼层
楼主
发表于 2018-12-16 01:00:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
index.php4 [1 n/ f; j8 l. n/ B1 j
  1. <?php/ W( y6 i  I6 p
  2. session_start();
    / N% e9 a0 I$ s: O, b. R
  3. // 丢弃输出缓冲区的内容 **
    - z/ A' S# S" @
  4. ob_clean();% M9 T4 {/ j. ~8 p2 _: }
  5. header ('Content-Type: image/png');
    3 ^! e/ p- Q  T1 K2 Q# V
  6. // 1 创建画布' |4 B) Q2 i) M8 K  _
  7. $image=imagecreatetruecolor(100, 30);$ F3 e8 g* Z, J7 c/ H# }: z
  8. // 2 画一个矩形并填充背景颜色为白色
    % ^, |0 t* z1 b6 Y
  9. $color=imagecolorallocate($image, 255, 255, 255);
    & t' S6 @; P7 k4 N
  10. imagefill($image, 20, 20, $color);+ o  C3 d, x! \; s# v5 q9 ]
  11. // for($i=0;$i<4;$i++){$ F2 @; ]4 ?9 L( q! V; N5 t8 [
  12.     // $font=6;
    4 S8 @$ C; O) w  P" _
  13.     // $x=rand(5,10)+$i*100/4;2 J7 n' M2 ?9 z% w; s9 X4 U% a5 u" [
  14.     // $y=rand(8, 15);. C( j/ e- D8 K3 g1 \
  15.     // $string=rand(0, 9);3 v! W* x$ o+ s' D3 f" m
  16.     // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));0 R% ~9 m0 ?, j, b0 _1 N$ v8 E
  17.     // imagestring($image, $font, $x, $y, $string, $color);4 \! X. y. }0 h7 O) @3 J7 ?
  18. // }7 L: m8 S# S' {7 x0 I
  19. $code='';3 t( U7 R" e% n1 b+ @
  20. for($i=0;$i<4;$i++){
    9 z) K3 u7 ?. l7 M1 ^  S- p
  21.     $fontSize=8;
    6 A; X; h  I: O8 |
  22.     $x=rand(5,10)+$i*100/4;
    . Q* C+ K1 o3 S) p
  23.     $y=rand(5, 15);
    + W" N/ I4 }# F1 Y7 b9 `: h( P7 u" y
  24.     $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';
    4 g+ P8 T" I6 t. ]
  25.     $string=substr($data,rand(0, strlen($data)),1);
    0 f+ Q# |5 e& ~) J4 N
  26.     $code.=$string;
    8 H8 s7 p! l4 ~% m
  27.         // 3.2 加载自定义字体- J3 {* w/ P+ @
  28.         $font_file = '1.ttf';$ y, h; v, u( ~+ H' |( g/ g, V7 s+ h
  29.     $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));
    : N9 S% H& a. i+ A) ?" P6 V+ J* E6 l
  30.         // 把文字填充到画布
    - W% P, x" b! z
  31.         //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);3 x3 b( @1 O* {1 i
  32.     imagestring($image, $fontSize, $x, $y, $string, $color);
    % b& a/ I) C# k$ a: C9 p/ N
  33. }. x9 S2 c9 a% N, t' l6 P
  34. $_SESSION['code']=$code;//存储在session里( ]2 |' n9 j4 A% s3 r' k
  35. for($i=0;$i<200;$i++){1 D; O- A# c; k, \% b! N/ F' k
  36.     $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));9 [, ~. a9 R3 M; X" ^& I, n
  37.     imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);9 r, I6 Y8 Y6 e# d
  38. }
    3 c2 Z( C! D& M& ]. N
  39. for($i=0;$i<2;$i++){
    % N; P' B' q6 j3 q
  40.     $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));
    ! m) p% N1 M" W! O" e
  41.     imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);
    & R2 u* L1 }" T' N" T
  42. }
    ( z6 s2 q5 u& x- m& f( {3 M
  43. imagepng($image);; B. S. [% F2 d  u, d& l% S4 }
  44. imagedestroy($image);: v' o, ?) d3 @
  45. % s- x% O$ h! P3 `, Y3 a9 T
  46. ?>
复制代码
5 ]3 Q! `6 ?, n
form.php
9 o8 e; x: G: D, @% E
  1. <?php( E( h) w7 G' H, B) G* h
  2.     if(isset($_REQUEST['code'])){3 z* S2 `7 Y% f8 K# S% C
  3.          session_start();& c* f4 y/ [! N- @
  4.         if($_REQUEST['code']==$_SESSION['code']){
    ( u! l  s0 T( Z( e. {+ ]+ s
  5.             echo "<font color='red'>输入正确</font>";
    $ Y! [# R  E  _+ z# X2 V- S5 z
  6.         }else{: |, r" ^# f: X! |# I( h
  7.             echo "<font color='red'>输入错误</font>";
    7 |- g! q3 f3 j
  8.         }- I0 y2 `/ |9 t2 g) K
  9.     }
    * R- P( X+ t' |% s: ]" i0 k7 D
  10. ?>6 |9 u+ k' t. ~7 Z( [$ m
  11. <!DOCTYPE html>
    4 o2 u' a/ b7 w& P2 A# ^, o+ {
  12. <html>, D) L1 q- j! E, M  P/ h) T( k
  13.     <head>
    ) `7 K6 L$ ^; R" Q& V2 [
  14.         <meta chartset="UTF-8" />) R/ d1 Z9 A) b/ E
  15.         <style type="text/css" rel="stylesheet">
    , c* q2 J' o$ S) c5 F; d
  16.             a{
    " ~0 x9 \+ n: u8 c1 e5 n
  17.                 text-decoration:none;7 U7 l6 Y% {' T6 y3 t. B  G* Q
  18.                 font-size:30px;
    . z5 @! C8 x5 l) u: o% j( @# T( Q+ g
  19.                 color:blue;, q7 K% k9 J& G; `# r+ r8 U4 r; M
  20.             }
    ( g. m5 T% T( j! U
  21.             a:hover{! |; g! S1 ~4 Y5 V4 D5 F- y) ?
  22.                 text-decoration:underline;
    9 T2 m" U, `' B8 F0 v
  23.             }
    $ Z1 j. a. K! J1 X. N2 i
  24.         </style>- o4 `0 F8 r3 M1 r7 e- ~
  25.     </head>7 k& S, r3 b& }! C  y
  26.     <body>3 |' C5 f- J! q) `* ?: o
  27.         <form action="form.php" method="get">; N5 g- V& H  N% R3 W2 O
  28.             验证码:  <img id="img" src="index.php?" onclick="changeCode()"/>1 G, ~" Z/ W- P
  29.             <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />- }5 e) ]4 ^1 b" ^, T- f
  30.             请输入验证码: <input name="code" /><br />/ ^- D1 u# \2 P' g8 u, _% i
  31.             <input type="submit" value="提交" />1 p+ K6 q4 p* L  p! {: A$ B
  32.         </form>3 {- F* Z+ u. Q) @9 s4 W
  33.     </body>5 R6 a; C' _7 @* j0 m# K
  34.     <script type="text/javascript">: v+ N5 I# t* Y* A; {3 k
  35.         1 W% C8 v0 R8 G: E
  36.         function changeCode(){+ u4 ~" `# Q$ q: D. f
  37.             var img=document.getElementById('img');
    3 h, c0 p) b# J! U
  38.             //img.src='index.php?r='+Math.random();& }4 s  u! P5 X- u' G
  39.             img.setAttribute('src','index.php?r='+Math.random());' s6 E1 H; i6 N6 d& H; J3 f+ q8 c
  40.         }
    9 \& k, K( X0 P# G
  41.     </script>* ?( i' }' j" |7 ~1 t
  42. </html>
复制代码
7 `+ o5 {; ]5 Q3 n8 Z3 |, a7 @3 ~

2 G/ m( Q. K+ \8 v
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2026-3-16 20:19 , Processed in 0.065754 second(s), 19 queries .

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