简体   繁体   English

获取来自我网站的请求

[英]Get where requests coming from to my website

I'm having a simple PHP page that generates random json values and returns it to the user. 我有一个简单的PHP页面,该页面生成随机json值并将其返回给用户。 I want to know what website is using this page to get data using curl or javascript. 我想知道哪个网站正在使用此页面来使用curl或javascript获取数据。 for example: 例如:

the PHP page: PHP页面:

$datas['random'] = ['firstValue'=>'Hello', 'secondValue'=>'world'];
header('Content-Type: application/json');
echo json_encode($datas);

now that code will return this value 现在该代码将返回该值

{"random":{"firstValue":"Hello","secondValue":"world"}}

what I want to do is: if someone used this file in his website using curl or javascript I want to be able to know which website is using it and what website requested it. 我想做的是:如果有人使用curl或javascript在他的网站中使用了此文件,我希望能够知道哪个网站正在使用它,哪个网站请求了它。

if the website http://example.com used this code in the website 如果网站http://example.com在网站中使用了此代码

var xhttp = new XMLHttpRequest();

                  xhttp.onreadystatechange = function () {
                      if (this.readyState === 4) {
                          if (this.status === 200) {
                              resolve(this.responseText);
                          } else if (this.response == null && this.status === 0) {
                              var reason = new Error('OFFLINE');
                              reject(reason);
                          } else {
                              var reason = new Error('ERROR');
                              reject(reason);
                          }
                      }
                  };


              xhttp.open("GET", "jsonpage.php", true);
              xhttp.send();

I want to be able to know that the website http://example.php requested the file, without using extra value jsonpage.php?website=example.com 我想知道网站http://example.php请求了文件, 而没有使用额外的值jsonpage.php?website=example.com

is there any way to do it using PHP. 有什么办法可以使用PHP做到这一点。

You can check the IP address of the party making the call to jsonpage.php, but to the best of my knowledge, not the domain. 您可以检查拨打jsonpage.php的一方的IP地址,但据我所知,不是域。

$_SERVER['REMOTE_ADDR'] contains the real IP address of the connecting party. $_SERVER['REMOTE_ADDR']包含连接方的真实IP地址。 That is the most reliable value you can find. 那是您能找到的最可靠的价值。

However, they can be behind a proxy server in which case the proxy may have set the $_SERVER['HTTP_X_FORWARDED_FOR'] , but this value is easily spoofed. 但是,它们可能位于代理服务器之后,在这种情况下,代理可能已设置$_SERVER['HTTP_X_FORWARDED_FOR'] ,但此值很容易被欺骗。 For example, it can be set by someone without a proxy, or the IP can be an internal IP from the LAN behind the proxy. 例如,它可以由没有代理的人设置,或者IP可以是来自代理后面的LAN的内部IP。

Finally, if your concern is that only some people should have access to jsonpage.php, I suggest you implement either a public/private API key or oAuth to ensure only the right people have access. 最后,如果您担心只有一些人可以访问jsonpage.php,我建议您实现公共/私有API密钥或oAuth,以确保只有合适的人才能访问。

Further reading: How to get the client IP address in PHP 进一步阅读: 如何在PHP中获取客户端IP地址

您可以使用$_SERVER['HTTP_REFERER']来告诉您哪个网站正在获取您的信息,但它并不可靠,因为很容易从源代码进行更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM