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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[php学习资料] 验证码

[复制链接]
跳转到指定楼层
楼主
发表于 2018-12-16 01:00:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
index.php
0 v2 {( M7 e  s4 {2 f* E
  1. <?php
    5 S# s( [; R! G# I  O8 E8 Z
  2. session_start();3 \9 {( \7 f0 s1 p7 \; _+ u
  3. // 丢弃输出缓冲区的内容 **+ K2 G+ Q' K7 J& S1 o' q& h
  4. ob_clean();
    9 v) C% B8 ~+ C3 Y
  5. header ('Content-Type: image/png');9 r4 O+ q4 {9 v8 r/ g
  6. // 1 创建画布
    3 o3 H% r& w* _
  7. $image=imagecreatetruecolor(100, 30);9 {0 l6 |" E, W; a
  8. // 2 画一个矩形并填充背景颜色为白色
    : I: t' T: X, B- Z3 i! z, _1 H  n. h
  9. $color=imagecolorallocate($image, 255, 255, 255);, k" j" m* o% E+ g- L
  10. imagefill($image, 20, 20, $color);
    & T# v. h. i" F$ e2 `
  11. // for($i=0;$i<4;$i++){! l# a2 a6 m9 f
  12.     // $font=6;
    : B- _; b" u- y4 X7 F6 ~
  13.     // $x=rand(5,10)+$i*100/4;1 G( P. N+ [: ~% F# d" j
  14.     // $y=rand(8, 15);+ L! J1 ^) z! v( M5 \  q
  15.     // $string=rand(0, 9);5 y# {8 a% A# I+ H* l- Z/ H
  16.     // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));
    ' o" F4 u0 o% r3 T
  17.     // imagestring($image, $font, $x, $y, $string, $color);
    ; t' |2 m0 s7 k0 @: A% I
  18. // }
    % o4 U8 p/ y) ?' r
  19. $code='';8 m- L6 s! `, M! P, o" x1 c2 h
  20. for($i=0;$i<4;$i++){  ~; K5 Z$ X4 m! \/ h+ K
  21.     $fontSize=8;
    % Q; e1 Z" x& M9 ]
  22.     $x=rand(5,10)+$i*100/4;8 e0 V# l( e: x# c. U
  23.     $y=rand(5, 15);
    9 E, u& b9 w2 b3 l: X0 A- L
  24.     $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';* }  }- h, r! q4 _- [# _/ P
  25.     $string=substr($data,rand(0, strlen($data)),1);8 \9 [! ^$ z  T* ?
  26.     $code.=$string;
    * ?3 E. v$ i9 v
  27.         // 3.2 加载自定义字体2 o; j2 `$ K- n3 a6 [5 u
  28.         $font_file = '1.ttf';
    ' v5 |( ?' d; F7 N! A
  29.     $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));
    & x5 S7 A! m1 W) L% j8 ~# k
  30.         // 把文字填充到画布
    6 }, D8 }6 _% ?# g/ U
  31.         //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);: k9 V. Y+ r) ~& L
  32.     imagestring($image, $fontSize, $x, $y, $string, $color);
      k. i1 c4 e; ?- _9 E; S$ ]
  33. }
    3 D2 J$ Z* |1 L7 k% M9 ?2 q( V8 t
  34. $_SESSION['code']=$code;//存储在session里
    4 Y: e# F0 ]" m& f3 F. ~
  35. for($i=0;$i<200;$i++){
    & r3 r: W; k9 C+ B) x/ U$ P. Y) Z7 Z
  36.     $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));
    " U8 Z: f  E3 r# a0 S. [$ F
  37.     imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);
    " l& K" |% g/ k- a
  38. }
    5 r) i; H& v' p0 C  P4 q
  39. for($i=0;$i<2;$i++){
    + @: B+ z8 O2 r9 r3 o6 q
  40.     $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));
    # c( l0 z" q) w; j
  41.     imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);
    ! p& C! O2 t* h7 u
  42. }7 {! a! j7 T3 d
  43. imagepng($image);' Z+ F) p/ m. j5 J
  44. imagedestroy($image);
    . d7 i# v1 R( G  a5 t0 ]5 u+ }
  45. 7 P' Y; {. b! c3 U, f
  46. ?>
复制代码

- J5 E, @2 e& i' J! }' B2 w1 jform.php( T, q1 X4 H5 C
  1. <?php
    $ s( u- f$ V1 ?2 A9 z; k
  2.     if(isset($_REQUEST['code'])){9 l* j- b2 }' R- C. U8 N" }
  3.          session_start();) ^" Y% l- o* F
  4.         if($_REQUEST['code']==$_SESSION['code']){
    1 v; k3 O1 [  G% A0 T, O& y" b
  5.             echo "<font color='red'>输入正确</font>";  C1 C3 |) m4 p/ }0 A( H
  6.         }else{! g; B  Y9 V5 T
  7.             echo "<font color='red'>输入错误</font>";4 \4 m4 Y) ]' p4 o5 x& D6 R
  8.         }( j8 T# N/ n, M9 Y# B% J9 ~
  9.     }4 G7 z& K' {" Q7 P# E9 j9 U
  10. ?>( e2 |2 a. j% N# c" ?8 d) M2 e
  11. <!DOCTYPE html>
    / h. j3 U8 m1 x5 c! s1 r
  12. <html>4 @7 y. q- h6 ?
  13.     <head>
    5 }2 D0 ~  h1 Z  T) R/ Z& c& W
  14.         <meta chartset="UTF-8" />8 d7 F9 A4 z& ?, W$ y  N4 @' g# U, }
  15.         <style type="text/css" rel="stylesheet"># T- e7 o  d% S$ r4 ]
  16.             a{
    0 w( p% O0 ?9 D
  17.                 text-decoration:none;/ p; n, y- h; f8 O; D1 D
  18.                 font-size:30px;- {$ i9 h6 D7 n& v& ]$ K2 h3 P
  19.                 color:blue;# ^, p0 J: x2 C) E) w9 l  a5 i, d( L
  20.             }
    ( A3 c' X* D5 a( j6 d6 w
  21.             a:hover{5 d8 z3 R6 x% _7 d; V0 s; t6 ?
  22.                 text-decoration:underline;, Q# K9 R: }, z$ ^3 {( i4 @
  23.             }
      L, r1 _7 c1 T+ X6 W2 }5 `: S( z
  24.         </style>
    . \$ W9 |/ A" `3 P
  25.     </head>7 B4 O" w- F. C$ ^* G0 U
  26.     <body>
    $ {8 r' p1 Y) e# k+ q8 H
  27.         <form action="form.php" method="get">& y  t# l" d  p, o
  28.             验证码:  <img id="img" src="index.php?" onclick="changeCode()"/>; k" c2 S0 q) M1 x* @
  29.             <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />9 K$ k- ^: |8 c7 H9 ~" o. k
  30.             请输入验证码: <input name="code" /><br />
    / l* T. v7 `& r) O
  31.             <input type="submit" value="提交" />4 Y4 }) f' B. H; y' F8 u
  32.         </form>/ o! U8 B4 ^- f- |$ Z
  33.     </body>+ k, ^5 g. X7 S8 L# M! f: e
  34.     <script type="text/javascript">8 _) D+ M3 I$ z8 L2 I
  35.         5 G& v( R& {, n/ f* i  T! B
  36.         function changeCode(){
    # X: k7 }. S1 N/ c* \
  37.             var img=document.getElementById('img');5 ~. u$ T9 y- d3 h' f6 o, y
  38.             //img.src='index.php?r='+Math.random();# I7 H1 j4 g1 N# {! }5 n
  39.             img.setAttribute('src','index.php?r='+Math.random());
    ; y. a. ^- z4 W3 J3 n5 T$ @" W! s
  40.         }
    9 k' |; _* c2 [& R- w8 @. r
  41.     </script>6 g) n$ S# n; f5 j' A+ c: w
  42. </html>
复制代码
! ~; n- ?) O# z3 p; ?
' b) C9 [. \% e' o2 k3 B
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2024-6-26 10:14 , Processed in 0.130882 second(s), 19 queries .

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