How do I get the external IP of my server using PH

2019-01-11 10:50发布

I often hear people say to use "$_SERVER['SERVER_ADDR']", but that returns the LAN IP of my server (e.g. 192.168.1.100). I want the external IP.

标签: php ip
11条回答
别忘想泡老子
2楼-- · 2019-01-11 11:17

You could try this:

$ip = gethostbyname('www.example.com');
echo $ip;

to get the IP address associated with your domain name.

查看更多
相关推荐>>
3楼-- · 2019-01-11 11:21

There is NO way to get your underlying IP Address that has been designated by your ISP via conventional PHP if you are using a router. A way to get the external IP is to find a service that will obtain it for you and echo the address back to you. I found a handy service which does just that. http://ipecho.net/

You can use:

$realIP = file_get_contents("http://ipecho.net/plain");
查看更多
霸刀☆藐视天下
4楼-- · 2019-01-11 11:21

Assuming your PHP is running on a Linux server you can call ifconfig using PHP's exec function. This will give public IP without need to contact some external website/service. E.g.:

$command = "ifconfig";  /// see notes below
$interface = "eth0";    // 
exec($command, $output);
$output = implode("\n",$output);
if ( preg_match('/'.preg_quote($interface).'(.+?)[\r\n]{2,}/s', $output, $ifaddrsMatch)
        && preg_match_all('/inet(6)? addr\s*\:\s*([a-z0-9\.\:\/]+)/is', $ifaddrsMatch[1], $ipMatches, PREG_SET_ORDER) )
{
    foreach ( $ipMatches as $ipMatch ) 
        echo 'public IPv'.($ipMatch[1]==6?'6':'4').': '.$ipMatch[2].'<br>';
}

Note that sometimes as $command you have to specify full path of ifconfig. You can find this by executing

whereis ifconfig

from shell prompt. Furthermore $interface should be set to the name of your server's main network interface that has the link to the WAN.

On Windows you can do something similar of course, using ipconfig instead of ifconfig (and corresponding adjusted regex).

查看更多
唯我独甜
5楼-- · 2019-01-11 11:23

I think there is much code for this things in others answers, but my answer is short, but you need to execute a command in shell to get the ip...

but it is short and fast, I think that...

php execute bash > bash run > bash get ip > php get ip

echo shell_exec( "dig +short myip.opendns.com @resolver1.opendns.com");

Sorry my for my english, I hope it help all you...

Reference: How can I get my external IP address in a shell script?

查看更多
甜甜的少女心
6楼-- · 2019-01-11 11:24

Just query a host that returns your IP address:

$externalContent = file_get_contents('http://checkip.dyndns.com/');
preg_match('/Current IP Address: \[?([:.0-9a-fA-F]+)\]?/', $externalContent, $m);
$externalIp = $m[1];

or, set up a service that simply echoes just the IP, and use it like this:

$externalIp = file_get_contents('http://yourdomain.example/ip/');

Set up the service yourself by simply echoing the remote IP address, or pay someone to host it. Do not use somebody else's server without permission. Previously, this answer linked to a service of mine that's now being hit multiple times a second.

Note that in an IP network with one or more NATs, you may have multiple external IP addresses. This will give you just one of them.

Also, this solution of course depends on the remote host being available. However, since there is no widely implemented standard (no ISP and only some home routers implement UPnP), there is no other way to get your external IP address. Even if you could talk to your local NAT, you couldn't be sure that there isn't another NAT behind it.

查看更多
霸刀☆藐视天下
7楼-- · 2019-01-11 11:27

I know this question is old and long answered, but I was googling for the same thing and want to add my own "hack". For this to work your webrequest has to come from an external IP address or you have to alter $own_url to a url that does an external request to itself.

The point is, if you let the script do a request to itself than you get it's external IP address.

<?php
if (isset($_GET['ip'])) {
    die($_SERVER['REMOTE_ADDR']);
}
$own_url = (isset($_SERVER['HTTPS']) ? 'https' : 'http') . '://'.$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];
$ExternalIP = file_get_contents($own_url.'?ip=1');
echo $ExternalIP;
?>
查看更多
登录 后发表回答