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

QQ登录

只需一步,快速开始

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

[php学习资料] 验证码

[复制链接]
跳转到指定楼层
楼主
发表于 2018-12-16 01:00:13 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
index.php
! F1 a) @, J" G, u
  1. <?php- V6 S3 n/ g4 h; c
  2. session_start();
    + r( G3 J6 v. ]6 D' t6 Z
  3. // 丢弃输出缓冲区的内容 **
    9 g/ t  x, Y5 |. g$ r) _
  4. ob_clean();5 x$ m% p. Z: |1 t4 S! ~  R0 Q7 ~
  5. header ('Content-Type: image/png');
    - P( n& A) v- r3 r/ k" P, w* \
  6. // 1 创建画布2 P! \# _8 w: l
  7. $image=imagecreatetruecolor(100, 30);6 i$ n$ a. @; e/ x6 r: s7 B
  8. // 2 画一个矩形并填充背景颜色为白色8 n6 s7 e% R5 L
  9. $color=imagecolorallocate($image, 255, 255, 255);
    , Y5 I1 ^2 l0 p, G0 y+ s4 \
  10. imagefill($image, 20, 20, $color);
    * i& z$ B3 c5 B0 i; e
  11. // for($i=0;$i<4;$i++){4 ?- p) J2 v8 m  z% y  q7 t5 Y
  12.     // $font=6;9 i' h, z1 e0 H7 A
  13.     // $x=rand(5,10)+$i*100/4;
    5 b  [/ j- p& n" L7 e  e# @2 a3 l! ~* Q* Y
  14.     // $y=rand(8, 15);5 N1 y; V3 D: c8 _  `, y
  15.     // $string=rand(0, 9);' s7 V# n9 |6 C% g$ {6 H
  16.     // $color=imagecolorallocate($image, rand(0,120), rand(0,120), rand(0,120));; P- e3 ^6 g5 O" _
  17.     // imagestring($image, $font, $x, $y, $string, $color);
    $ L2 X$ P  h. T1 H
  18. // }
    - y4 b+ R- s, y5 I5 K1 D1 R9 J$ T
  19. $code='';" c( u6 X( E  x4 J3 \/ V9 v
  20. for($i=0;$i<4;$i++){/ Z  x' W7 W$ `- t  A0 C5 i: s
  21.     $fontSize=8;
    ! k& c' P7 g9 W$ l- C, n
  22.     $x=rand(5,10)+$i*100/4;8 F. M3 l9 i0 A5 K6 s0 q
  23.     $y=rand(5, 15);3 w9 n( M9 e* g2 m$ j* s9 |
  24.     $data='abcdefghijklmnopqrstuvwxyz123456789ABCDEFGHJKLMNPQRTUVWXYZ';- D! l2 N( R8 F2 R5 _# Y
  25.     $string=substr($data,rand(0, strlen($data)),1);- d% T7 z5 j9 n' U2 S: |
  26.     $code.=$string;2 z! }4 L' A$ o9 j* t
  27.         // 3.2 加载自定义字体  a1 ?' S" F2 s: j' e
  28.         $font_file = '1.ttf';+ N$ `$ }/ I: N8 t; e( ?
  29.     $color=imagecolorallocate($image,rand(0,120), rand(0,120), rand(0,120));1 F1 U  y- l/ V1 ~2 M  j: r
  30.         // 把文字填充到画布1 U9 j: E7 g8 M/ d" v" P% N
  31.         //imagefttext($image, 10, mt_rand(-5, 10), $x, $y, $color, $font_file, $string);
    ! E$ \; n7 }/ C6 Q$ i
  32.     imagestring($image, $fontSize, $x, $y, $string, $color);
    ! i, j0 u- R6 p' L
  33. }
    8 ?& V) q- `* i
  34. $_SESSION['code']=$code;//存储在session里
    ' h& O3 g' y4 c) @' R  X
  35. for($i=0;$i<200;$i++){- y: N$ v$ {+ x
  36.     $pointColor=imagecolorallocate($image, rand(100, 255), mt_rand(100, 255), rand(100, 255));
    0 T' Q. A- m9 q. Z9 `6 \/ f1 }
  37.     imagesetpixel($image, mt_rand(0, 100), rand(0, 30), $pointColor);
    / a) C+ i4 u) F1 a$ y
  38. }
    5 m5 ?- e& v8 n7 a$ C$ Q" ^+ L
  39. for($i=0;$i<2;$i++){
    8 B& X2 B+ f/ a  [
  40.     $linePoint=imagecolorallocate($image, rand(150, 255), rand(150, 255), rand(150, 255));* O( x, }; G( ~$ B1 U* e8 U. F3 I
  41.     imageline($image, rand(10, 50), rand(10, 20), rand(80,90), rand(15, 25), $linePoint);
    * N# v( S! D3 ~3 z4 J* i$ R
  42. }
    * j+ X, X+ F) T
  43. imagepng($image);5 A0 f5 k0 q8 A, Y
  44. imagedestroy($image);
    # \% }( o& C, ^/ b7 X

  45. 4 L, A) e+ o$ A5 e) k
  46. ?>
复制代码
! w8 f1 ]3 A: a" s  o2 B" x
form.php
0 k/ _* L* [  R: `9 H
  1. <?php
    5 t: v% J& `$ R2 r4 F8 j
  2.     if(isset($_REQUEST['code'])){
    - {& \/ k6 b1 A
  3.          session_start();+ k- ]! G+ g$ F) N# h
  4.         if($_REQUEST['code']==$_SESSION['code']){
    + e% f4 Y- Y3 S1 M. d  L
  5.             echo "<font color='red'>输入正确</font>";
    2 E% Q" e" Y; i$ Z9 ?4 p
  6.         }else{1 _6 X  v0 N; w7 @3 r; w5 G, n/ v
  7.             echo "<font color='red'>输入错误</font>";
    : U6 _$ t, |' I( @6 z! {" y
  8.         }5 x1 R: u' R- q' O
  9.     }
    " N4 {! k' G& P, W# j  t
  10. ?>) j! v! M- L: M! W% Z0 ~* B  M
  11. <!DOCTYPE html>  ~; p' @% a) v# l; m: @1 l- Y
  12. <html>2 o0 u' A  ~, i
  13.     <head>- B. I( p- f( w' ]/ |) n. C* s
  14.         <meta chartset="UTF-8" />( E0 j0 O/ E2 j- m
  15.         <style type="text/css" rel="stylesheet">6 t6 m+ a  ]7 v8 B3 S" @
  16.             a{
    - ^. q! ]$ P' f2 z6 @  C
  17.                 text-decoration:none;
    + N4 ~5 o$ @, g$ g
  18.                 font-size:30px;
    7 M! m* B% t' S$ O+ R8 M$ D8 ^- J
  19.                 color:blue;
    6 X& e$ K7 E* X! n& t3 Q
  20.             }" O0 [; V) [9 P' q) r
  21.             a:hover{( z3 j% ^. N2 M$ z, e
  22.                 text-decoration:underline;
    2 X& r% m0 n4 o1 J) j( h
  23.             }5 }, X1 A. q9 {/ _, ]; x
  24.         </style>
    0 [! ~. }: Y# e2 z& V! @* |2 n  S" ~
  25.     </head>: A% ?2 x( g+ C; }
  26.     <body>
    : X$ E2 Y/ \) k
  27.         <form action="form.php" method="get">' X" E. W6 \2 l; ~" [
  28.             验证码:  <img id="img" src="index.php?" onclick="changeCode()"/>  N0 V, ]+ V  l' i' B6 x4 Y7 }
  29.             <a href="javascript:void(0)" onclick="changeCode()">看不清?</a> <br />
      J3 @. I- k+ `
  30.             请输入验证码: <input name="code" /><br />0 `0 ^( |  |5 R5 i% X+ [( N
  31.             <input type="submit" value="提交" />! r0 h1 e! Q) W
  32.         </form>$ f4 P( N, }! T; {: J% {
  33.     </body>
    ) y7 i) l4 }2 K
  34.     <script type="text/javascript">) {1 R- g1 `2 ]5 x; r' u* I6 u
  35.         6 W1 _! u% m, {4 t" v5 q
  36.         function changeCode(){
    - W8 {. G8 b& j+ A' C9 A" p
  37.             var img=document.getElementById('img');
    " U5 n8 P& ]  b; \
  38.             //img.src='index.php?r='+Math.random();, M1 w3 C6 ]5 ?) Q
  39.             img.setAttribute('src','index.php?r='+Math.random());0 E* a5 V/ ?- F& }' \+ d& S
  40.         }
    4 O9 p- C* \5 y; l; V, u& V5 X5 Q/ y
  41.     </script>
    & e3 m4 N' Q. ?
  42. </html>
复制代码

+ R1 N" m# n+ }
2 E# o9 ]# g+ k; I, o: o9 m
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 支持支持 反对反对
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

GMT+8, 2024-5-7 19:37 , Processed in 0.107250 second(s), 19 queries .

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