zoukankan      html  css  js  c++  java
  • 温故而知新 前端日志上传新姿势 navigator.sendBeacon(信标)

    原文地址:https://mp.weixin.qq.com/s/-moAfEZicrFmun4qfWLdOQ

    简单示例js:

    var data = JSON.stringify({
      name: 'Berwin'
    });
    navigator.sendBeacon('http://localhost:80/index.php', data)

    简单php示例:

    由于是信标发送的 POST 请求,并且是 "text/plain" 数据类型,所以适合用 $GLOBALS['HTTP_RAW_POST_DATA'] 接受。

    顺便一提,最好不要使用GET方式请求类似: navigator.sendBeacon('http://localhost:80/index.php?a=123') ,不太适合也不是正确的套路。

    <?php 
    
    header('Access-Control-Allow-Origin:*');
    header('Access-Control-Allow-Headers:x-requested-with,content-type'); 
    
    function WriteLog($msg,$module = null,$logLevel = "DEBUG")
    {
        $filepath = "./log/";
        if(!is_dir($filepath)) mkdir($filepath,'0777');
        $MyLogFile = @fopen($filepath.date("Y-m-d").".txt",'a+');
    
        $time = date("Y-m-d H:i:s");
        if(isset($module)){$module =  sprintf("
    归属模块:".$module."
    ");}
        $logLine = "
    -------------------------------  $time -------------------------------
    ";
        $logLine .= $module;
        $logLine .= "
    异常信息:$msg
    ";
        $logLine .= "
    错误等级:$logLevel
    ";
        fwrite($MyLogFile,$logLine);
    }
    
    WriteLog($GLOBALS['HTTP_RAW_POST_DATA']);
  • 相关阅读:
    cookie的路径
    cookie的生命
    cookie详解
    cookie简介&用途
    编码
    请求转发和重定向的区别
    request:域
    request:请求转发,请求包含
    常用的html语法
    request:获取请求的URL
  • 原文地址:https://www.cnblogs.com/CyLee/p/9925975.html
Copyright © 2011-2022 走看看