安全的.php文件,通过js脚本调用

时间:2017-05-06 01:03:04

标签: javascript php jquery html nginx

我们的客户使用以下代码使用我们的免费服务:

std::future

example.js看起来像这样:

<script type='text/javascript'>id='example'; width='640'; height='480';</script><script type='text/javascript' src='http://example.com/example.js'></script>

问题是人们正在躲过examplefile.php。我们尝试将secure_link与nginx一起使用,效果很好,但仅限于能够在其站点中使用php代码的客户端,生成带密钥的随机安全令牌。其他一些客户只能嵌入HTML代码。有没有办法保护examplefile.php或者可能随机更改examplefile.php名称,并对我们的服务器进行验证以停止leeching?也许使用jquery?我们需要能够确保examplefile.php是由这个js调用的,而不是作为来自外部站点的iframe手动添加的。任何帮助都非常感谢。

提前致谢

1 个答案:

答案 0 :(得分:0)

您可以使用AJAX请求替换JavaScript,该请求使用令牌发送自定义HTTP请求标头。在验证令牌后,您的服务器将使用在iframe中使用的URL进行响应。此解决方案为您提供了控制URL的机会,以便您可以将其随机化。

另一种方法是将请求发送到指示您访问资源的意图的URL。它可以使用会话cookie进行响应,该会话cookie将由后续的iframe链接请求承载。

这里有一些vanilla JavaScript可以帮助您开始使用AJAX。

var myURL = 'https://www.example.com/path/',
    myCustomKey = 'Custom-Header',
    myCustomValue = 'Custom-Token-Value',
    myRequest = new XMLHttpRequest();

// open request so that custom header can be added
myRequest.open('GET', myURL, true);
// set custom header for request
myRequest.setRequestHeader(myCustomKey, myCustomValue);

myRequest.onload = function () {
    // Request finished. Do processing here.
    if (myRequest.status === 200) {
        // Request was successful
        // use the response
        console.log(myRequest.response);
    }
};

myRequest.send(null);

您必须配置服务器以支持CORS。见https://enable-cors.org/server.html