Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/238.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php文件,通过JavaScript调用_Javascript_Php_Jquery_Html_Nginx - Fatal编程技术网

php文件,通过JavaScript调用

php文件,通过JavaScript调用,javascript,php,jquery,html,nginx,Javascript,Php,Jquery,Html,Nginx,我们的客户使用我们的免费服务,代码如下: <script type='text/javascript'>id='example'; width='640'; height='480';</script><script type='text/javascript' src='http://example.com/example.js'></script> if (typeof (width) == "undefined") { var wi

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

<script type='text/javascript'>id='example'; width='640'; height='480';</script><script type='text/javascript' src='http://example.com/example.js'></script>
if (typeof (width) == "undefined") {
    var width = '100%';
}
if (typeof (height) == "undefined") {
    var height = '100%';
}
if (typeof (p) == "undefined") {
    var p = '0';
}
if (typeof (c) == "undefined") {
    var c = '0';
}
if (typeof (stretching) == "undefined") {
    var stretching = 'uniform';
}
document.write('<iframe allowfullscreen width="' + width + '" height="' + height + '" scrolling="no" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" allowtransparency="true" src="http://example.com/examplefile.php?id=' + id + '&p=' + p + '&c=' + c + '&stretching=' + stretching + '"></iframe>');
id='example';宽度='640';高度='480';
example.js如下所示:

<script type='text/javascript'>id='example'; width='640'; height='480';</script><script type='text/javascript' src='http://example.com/example.js'></script>
if (typeof (width) == "undefined") {
    var width = '100%';
}
if (typeof (height) == "undefined") {
    var height = '100%';
}
if (typeof (p) == "undefined") {
    var p = '0';
}
if (typeof (c) == "undefined") {
    var c = '0';
}
if (typeof (stretching) == "undefined") {
    var stretching = 'uniform';
}
document.write('<iframe allowfullscreen width="' + width + '" height="' + height + '" scrolling="no" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" allowtransparency="true" src="http://example.com/examplefile.php?id=' + id + '&p=' + p + '&c=' + c + '&stretching=' + stretching + '"></iframe>');
if(typeof(width)=“未定义”){
变量宽度='100%';
}
if(类型(高度)=“未定义”){
风险值高度='100%';
}
if(类型(p)=“未定义”){
var p='0';
}
if(类型(c)=“未定义”){
变量c='0';
}
if(类型(拉伸)=“未定义”){
变量拉伸='均匀';
}
文件。写(“”);
问题是人们正在偷窥examplefile.php。我们尝试在nginx中使用secure_link,效果很好,但仅适用于能够在其站点中使用PHP代码、生成随机安全令牌和密钥的客户端。其他一些客户端只能嵌入HTML代码。有没有办法保护examplefile.php,或者随机更改examplefile.php名称,并针对我们的服务器进行验证以停止泄漏


也许使用jQuery?我们需要能够确保examplefile.php由此JavaScript代码开始调用,而不是作为外部站点的iframe手动添加。

如果我理解正确,您希望确保您是唯一使用此资源的人。 一种方法是将
example.js
替换为生成的js文件
example.php

该文件将有两个职责:

  • 根据服务器验证请求
  • 输出纯JS内容,就像它是一个JS文件一样(带有适当的头数据)
  • 更新

    这是我的具体做法:


    通过使用example.php文件(而不是example.js),每次用户加载该文件时,为客户端初始化一个唯一的会话令牌,您将在examplefile.php中立即进行验证。通过这种方式,您可以确保(在某种程度上)请求来自example.php。如果我理解正确,您希望确保您是唯一使用此资源的人。 一种方法是将
    example.js
    替换为生成的js文件
    example.php

    该文件将有两个职责:

  • 根据服务器验证请求
  • 输出纯JS内容,就像它是一个JS文件一样(带有适当的头数据)
  • 更新

    这是我的具体做法:


    通过使用example.php文件(而不是example.js),每次用户加载该文件时,为客户端初始化一个唯一的会话令牌,您将在examplefile.php中立即进行验证。通过这种方式,您可以确保(在某种程度上)请求来自example.php。您可以用AJAX请求替换JavaScript,该请求发送带有令牌的自定义HTTP请求头。验证令牌后,您的服务器将使用在iframe中使用的URL进行响应。此解决方案为您提供了控制URL的机会,因此您可以将其随机化

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

    下面是一些普通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。请参见

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

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

    下面是一些普通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。请参见

    如果客户端服务器只有静态内容,则无法使其真正安全。您甚至可以在nginx中使用某种类型的白名单,哪个域可以构建页面(内容安全策略),但这很容易被破坏。你的一个客户可能会对你不利吗?若客户只能伤害自己——也许我能找到解决办法。但若客户可能就是那个坏蛋,并向其他人提供其服务的“密码”,那个么就并没有办法保证它的安全。所以,给用户提供“帐户”是可能的,而不必担心他们将如何使用它?是的,这是一个选项。看来我得实施一个账户系统了,如果我这么做你有什么想法?我已经起床太多时间了。。。对于任何不清楚的句子,请提前道歉;)我的想法现在是唯一的想法——我甚至没有测试过它,但我认为它可以工作。我会把它写下来作为答案,因为它太长了,不能评论。好的,也许在这里。恐怕我的想法错了。至少,这有点复杂。但是它的第二个核心是
    $\u服务器['HTTP\u REFERER']
    。只是它太弱了,但是有了其他的墙,它就足以保护大多数人不受伤害了。我很想读一下你的想法。你把它作为答案写了吗?如果客户机的服务器只有静态内容,那就谢谢你了——没有办法让它真正安全。您甚至可以在nginx中使用某种类型的白名单,哪个域可以构建页面(内容安全策略),但这很容易被破坏。你的一个客户可能会对你不利吗?若客户只能伤害自己——也许我能找到解决办法。但若客户可能就是那个坏蛋,并向其他人提供其服务的“密码”,那个么就并没有办法保证它的安全。所以,给用户提供“帐户”是可能的,而不必担心他们将如何使用它?是的,这是一个选项。看来我得实施一个账户系统了,如果我这么做你有什么想法?我已经起床太多时间了。。。对于任何不清楚的句子,请提前道歉;)我的想法现在是唯一的想法——我甚至没有测试过它,但我认为它可以工作。我会把它写下来作为答案,因为它不是