Javascript的全局误差通过AJAX / PHP处理:限制登录到我自己的脚本 [英] Javascript global error handling via AJAX/PHP: limit logs to my own script

查看:136
本文介绍了Javascript的全局误差通过AJAX / PHP处理:限制登录到我自己的脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个检测Javascript错误在我的网站,并将它们发送到我的后台报告的脚本:

I have a script that detects Javascript errors on my website and sends them to my backend for reporting:

<script>
window.onerror = function(msg, url, line, col, error){

    msg   = msg || '';
    url   = url || '';
    line  = parseInt(line || 0);

    // Note that col & error are new to the HTML 5 spec and may not be supported in every browser.
    col   = parseInt(col || 0);
    error = error || '';

    try
    {
        // Ajax Request for IE 5.5+, Firefox, Opera, Chrome, Safari XHR object
        var x = new (this.XMLHttpRequest || ActiveXObject)('MSXML2.XMLHTTP.3.0');
        x.open('POST', '/log.php', 1);
        x.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
        x.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        x.send('msg='+encodeURIComponent(msg)+'&url='+encodeURIComponent(url)+'&line='+line+'&col='+col+'&error='+encodeURIComponent(error));

        x.onreadystatechange = function()
        {
            if( (x.readyState > 3) && (x.status > 0 && x.status < 400))
                window.console && console.log(x.responseText);
        };
    }
    catch(e)
    {
        window.console && console.log(e);
    }

    return true;
};
</script>

我有一个服务器端的PHP脚本将侦听Ajax请求,并写入日志文件

I have a server-side php script that will listen for Ajax request and writes a log file

<?php

if( $handle = fopen('log.txt', 'a') ) {

    $log = date('d/m/Y H:i:s').PHP_EOL;

    if( isset($_REQUEST['msg']) )
         $log .= 'msg:'.$_REQUEST['msg'].PHP_EOL;

    if( isset($_REQUEST['url']) )
         $log .= 'url:'.$_REQUEST['url'].PHP_EOL;

    if( isset($_REQUEST['line']) )
         $log .= 'line:'.$_REQUEST['line'].PHP_EOL;

    if( isset($_REQUEST['col']) )
         $log .= 'col:'.$_REQUEST['col'].PHP_EOL;

    if( isset($_REQUEST['error']) )
         $log .= 'error:'.$_REQUEST['error'].PHP_EOL;

    $log .= '---------------------------------------------'.PHP_EOL;

    fwrite($handle, $log);

    fclose($handle);

    echo 1;

} else {
    echo 0;
}

如果在一个页面上涨JavaScript异常,如:

if in a page rise a javascript exception, eg:

<script> call_undefined_function(); </script>

写在日志文件...

write in the log file...

25/10/2014 11:31:08
msg:ReferenceError: call_undefined_function is not defined
url:http://www.test.it/
line:46
col:1
error:ReferenceError: call_undefined_function is not defined
---------------------------------------------

一切都运行得很好!

但是,我找了很多日志通过插件,工具栏,蠕虫或浏览器扩展使用的用户提出...

But, i find a lot of logs raised by plug-ins, toolbars, worms or browser extensions used by the users...

例如

24/10/2014 10:20:32
msg:Unsafe JavaScript attempt to access frame
url: http://ads.XXXXXX.net/?XXXXXX
line:0
col:0
error:Unsafe JavaScript attempt to access frame
---------------------------------------------

显然,这个剧本是不是我的网站,做一些研究,我发现是Internet Explorer的一个蠕虫病毒

我的问题是:?如何限制日志以我自己的脚本

推荐答案

您可以随时检查针对的网址:

You could always check against the URL:

// If the script is not being loaded from my domain
if(url.indexOf('http://mydomain') == -1)
{
    // Allow the error to propagate normally
    return false;
}

这可能无效但是,如果一些第三方的注入,而不是外部加载的那些内嵌脚本。

This may not be effective however if some third party is injecting inline scripts as opposed to externally loaded ones.

这篇关于Javascript的全局误差通过AJAX / PHP处理:限制登录到我自己的脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆