简体   繁体   English

PHP API获取请求者IP地址

[英]PHP API get requesters IP Address

Good day, I have created a PHP API, that accepts GET/POST requests. 美好的一天,我创建了一个PHP API,接受GET / POST请求。

I would like to get the IP address of the server which made a request to my API in order for me to do some verification. 我想获取向我的API 发出请求的服务器的IP地址以便我进行一些验证。

I tried to use $_SERVER['REMOTE_ADDR'] value for that, but noticed that it doesn't work well with all kind of websites. 我尝试为此使用$_SERVER['REMOTE_ADDR']值,但注意到它不适用于所有类型的网站。

I tried to search online for a solution, but only found how to get client's IP. 我尝试在线搜索解决方案,但只找到了如何获取客户的IP。
And I guess that "client" means the user that triggered the API call. 我猜“客户端”是指触发API调用的用户。

What I need is the website's server address from which request has been made. 我需要的是从中提出请求的网站的服务器地址。
Could someone offer a better way to do that? 有人可以提供更好的方法吗?

APIs can be called from a server - eg, another PHP script - or from client side - typically Javascript in a browser using AJAX, which is much more common. 可以从服务器(例如,另一个PHP脚本)或客户端(通常是在使用AJAX的浏览器中使用Javascript)中调用API。 For example, a Javscript snippet for Google Analytics, Google Maps, etc. will be included in plenty of web pages but the actual call to Google will be done by the client browser. 例如,大量的网页中将包含Google Analytics(分析),Google Maps等的Javscript代码段,但对Google的实际调用将由客户端浏览器完成。 In those situations, which I think is typical for many (most?) APIs, the only IP address you will see is the client, not the web server. 在那些情况下,我认为这是许多(大多数?)API的典型情况,您将看到的唯一IP地址是客户端,而不是Web服务器。 The solution Google (and many other APIs) use is to require registration for a free API key in order to use the API. Google(和许多其他API)使用的解决方案是要求注册一个免费的API密钥才能使用该API。 You can't usually track that to a specific web server, but it does allow you to connect with the initial customer or developer who registered for the API. 通常,您无法将其跟踪到特定的Web服务器,但是它确实允许您与注册API的最初客户或开发人员建立联系。

TL;DR Create a free "API Key" for your web site users/customers/developers and track them that way. TL; DR为您的网站用户/客户/开发人员创建一个免费的“ API密钥”,并以此方式进行跟踪。

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

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