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:27

If your server have a domain name you can ping it or you can use:

$sMyServerIP = gethostbyname('yourdomain.com');
echo $sMyServerIP;

It will return your outer IP address.

查看更多
The star\"
3楼-- · 2019-01-11 11:28

Have you tried:

gethostbyname(php_uname('n'));

?

查看更多
兄弟一词,经得起流年.
4楼-- · 2019-01-11 11:29

I'm going to add a solution because the others weren't quite right for me because they:

  • need to be run on a server with a domain name e.g. gethostbyname() (if you think about it, you don't actually have the problem if you know this a priori)
  • can't be used on the CLI (rely on something in $_SERVER to be set by a web server)
  • assume a specific format of ifconfig output, which can include multiple non-public interfaces
  • depend on parsing someone's website (who may disappear, change the URL, change the format, start lying to you, etc, all without notice)

This is what I would suggest:

$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
$res = socket_connect($sock, '8.8.8.8', 53);
// You might want error checking code here based on the value of $res
socket_getsockname($sock, $addr);
socket_shutdown($sock);
socket_close($sock);

echo $addr; // Ta-da! The IP address you're connecting from

The IP address there is a Google public DNS server. I trust they'll be around and running it for a while. It shouldn't really matter what address you use there as long as its a public IP address that belongs to someone who doesn't mind random connection attempts too much (yourself maybe?).

This is based on an answer I came across when I had a similar problem in Python.


P.S.: I'm not sure how well the above would work if there is sorcery going on between your machine and the internet.

查看更多
Juvenile、少年°
5楼-- · 2019-01-11 11:35

Also you could get IP via DNS A record based on hostname or server name:

$dnsARecord = dns_get_record($_SERVER['HTTP_HOST'],DNS_A);
if ( $dnsARecord ) echo 'IPv4: '.$dnsARecord[0]['ip'];
$dnsARecord = dns_get_record($_SERVER['HTTP_HOST'],DNS_AAAA);
if ( $dnsARecord ) echo 'IPv6: '.$dnsARecord[0]['ip'];

You could also use SERVER_NAME instead of HTTP_HOST if one of the two does not give what you want.

However, this is assuming that your server is configured correctly in correspondence with DNS. This may not always be the case. See my other answer using ifconfig that is perhaps better.

查看更多
看我几分像从前
6楼-- · 2019-01-11 11:43

You could parse it from a service like ip6.me:

<?php

// Pull contents from ip6.me
$file = file_get_contents('http://ip6.me/');

// Trim IP based on HTML formatting
$pos = strpos( $file, '+3' ) + 3;
$ip = substr( $file, $pos, strlen( $file ) );

// Trim IP based on HTML formatting
$pos = strpos( $ip, '</' );
$ip = substr( $ip, 0, $pos );

// Output the IP address of your box
echo "My IP address is $ip";

// Debug only -- all lines following can be removed
echo "\r\n<br/>\r\n<br/>Full results from ip6.me:\r\n<br/>";
echo $file;
查看更多
登录 后发表回答