简体   繁体   English

获取通过html表单发送请求的服务器的IP地址

[英]Get IP address of the server sending a request via html form

I want to get the IP address of the server sending request via HTML FORM. 我想获取通过HTML FORM发送请求的服务器的IP地址。

I made a test like this: 我做了这样的测试:

HTML FORM (form.html in server 1): HTML FORM(服务器1中的form.html):

<form action="URL_OF_SERVER2/rec.php" method="post">
<input type="submit" value="submit">
</form>

PHP FILE: (rec.php) PHP文件:(rec.php)

<?php
echo $_SERVER['HTTP_REFERER'].'<br><br>'; // To get referal URL
echo $_SERVER['REMOTE_ADDR']; // To get IP Address
?>

But when i tested, i get my own IP Address and not the one of the server. 但是当我测试时,我得到了自己的IP地址,而不是服务器之一。

Second try: 第二次尝试:

<?php
echo $_SERVER['HTTP_REFERER'].'<br><br>'; // To get referal URL
$result = parse_url($_SERVER['HTTP_REFERER']);
echo gethostbyname($result['host']); // To get IP Address
?>

But this not get real IP but the one of cloudflare for example, i want make same system as perfectmoney, you put your real IP on your dashboard to accept only request coming from, even if you are behind cloudflare, perfectmoney detect the real IP. 但这不是真正的IP,而是cloudflare的一个,例如,我要与perfectmoney制作相同的系统,您将真实IP放在仪表板上以仅接受来自的请求,即使您身在cloudflare后面,perfectmoney也可以检测到真实IP。

On my dashboard i can put IPs by range: 127.0.0.1/24 , 127.0.0.* ... to accept only requests coming from and even if the domain name is behind cloudflare or another similar services. 在我的仪表板上,我可以按以下范围放置IP:127.0.0.1/24,127.0.0。* ...以仅接受来自的请求,即使域名位于cloudflare或其他类似服务的后面。

$_SERVER['SERVER_ADDR']; is the server that executes the script's address. 是执行脚本地址的服务器。 $_SERVER['REMOTE_ADDR']; is the client address (the one that sent the request to the server from the server's point of view. See the $_SERVER array documentation for more info. 是客户端地址(从服务器的角度将请求发送到服务器的地址。有关更多信息,请参见$ _SERVER数组文档

address of the server sending request 发送请求的服务器地址

The server doesn't send a request. 服务器未发送请求。 The browser sends a request, and the server sends a response. 浏览器发送请求,服务器发送响应。

If you want the IP of the browser sending a request, use $_SERVER['REMOTE_ADDR'] . 如果要浏览器的IP发送请求,请使用$_SERVER['REMOTE_ADDR']

If you want the IP of the server sending a response, use $_SERVER['SERVER_ADDR'] . 如果要服务器的IP发送响应,请使用$_SERVER['SERVER_ADDR']

Note: $_SERVER['REMOTE_ADDR'] may not actually represent the browser's IP if there are any proxies in the way. 注意:如果有任何代理, $_SERVER['REMOTE_ADDR']可能实际上不代表浏览器的IP。

Update: If you want the IP address of the REFERER server, you will have to do your own DNS lookup. 更新:如果需要REFERER服务器的IP地址,则必须进行自己的DNS查找。

$data = parse_url( $_SERVER['HTTP_REFERER']);
print_r(dns_get_record($data['host']));

This will give you: 这将为您提供:

Array
(
    [0] => Array
        (
            [host] => www.google.com
            [class] => IN
            [ttl] => 270
            [type] => A
            [ip] => 172.217.9.68
        )

    [1] => Array
        (
            [host] => www.google.com
            [class] => IN
            [ttl] => 14
            [type] => AAAA
            [ipv6] => 2607:f8b0:4009:816::2004
        )

)

Note however that this is unreliable, as $_SERVER['HTTP_REFERER'] can easily be faked. 但是请注意,这是不可靠的,因为$_SERVER['HTTP_REFERER']很容易被伪造。

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

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