简体   繁体   中英

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.

I made a test like this:

HTML FORM (form.html in server 1):

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

PHP FILE: (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.

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.

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.

$_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.

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'] .

If you want the IP of the server sending a response, use $_SERVER['SERVER_ADDR'] .

Note: $_SERVER['REMOTE_ADDR'] may not actually represent the browser's IP if there are any proxies in the way.

Update: If you want the IP address of the REFERER server, you will have to do your own DNS lookup.

$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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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