将日志错误发送到文件


Send log errors to a file

我为我的毕业设计了一个网站,但它仍然只是我做的一件事。我想要的是当脚本安装在一个网站上时,我想要发送安装了我的脚本的网站的名称,也每当有错误时,我想将其发送到我的网站例如:

这个网站安装了我的脚本

www.security-dz.com/myscript

我想在其他网站的另一个文件中看到路径+网站。例如:

www.getlog.com/mylogs.php

这样做的目的是让我的客户更新,给他们支持,看看发生的错误,这样我就可以在下次更新中修复它们。

您可能需要仔细查看ajax请求的JQuery文档,以便您可以使用安全的http连接进行日志记录。这段javascript代码基本上描述了一个函数,该函数以文本格式将错误发送到服务器端脚本。该脚本可以反过来将错误描述写入服务器上的文件。我建议使用数据库代替;这样你就可以很容易地编写一个web客户端,显示所有报告的错误(和过滤器和其他好东西)。

您可以从服务器上ajax http get-request中的referer [sic]字段中提取原始url。

(function () { // function operator, in case console doesn't exist
    !console ?
        (console = {}) : console;
    !console.log ?
        (console.log = function () { }) : console.log;
    !console.info ?
        (console.info = console.log) : console.info;
    !console.error ?
        (console.error = console.log) : console.error;
}());
// Uses JQuery
function reportError (errDesc) {
    var path = "www.getlog.com/mylogs.php";
    $.ajax({
        url: path,
        type: "GET",
        async: true,
        cache: false,
        contentType: "application/x-www-form-urlencoded; charset=UTF-8",
        crossDomain: true,
        data: errDesc,
        dataType: "jsonp",
        error: function (req, type, errObj) {
            console.error("Reporting error failed: " + type + "'nAt url: " + path + "'n" + errObj);
        // In case you need to debug the error reporting function
        },
        succes: function (res) {
            console.info("Reported error to server:'nRequest:" + errDesc + "'nResponse: " + res);
        // extra error logging facility on client-side, invisible to most users
        },
        global: false // prevent triggering global ajax event handlers
    });
    return errDesc; // in case you want to reuse the errDesc
}

代码已通过jshint验证。如果还有问题,请告诉我,因为我没有花时间完全复制你的设置(设置2个不同的域等)

附录:一些有用的阅读,如果你有跨域消息,JSON不是javascript的子集,跨域资源共享,JSONP.

您可以做的是通过URL将使用脚本的网站名称和错误代码变量通过AJAX发布到您的日志网站,该网站将从URL中获取变量和名称,并使用这些添加到您的日志中。

但是,通过使用这种策略,您应该也使用一些URL验证,否则这将使您对注入攻击敞开大门。

这是很容易的,当你的脚本安装,获得网站信息,并通过套接字和HTTP请求发送get方法,然后在你的服务器上接收它。

错误,PHP有一些方法来控制错误日志,所以自定义它。