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.
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
If your server have a domain name you can ping it or you can use:
It will return your outer IP address.
Have you tried:
?
I'm going to add a solution because the others weren't quite right for me because they:
gethostbyname()
(if you think about it, you don't actually have the problem if you know this a priori)$_SERVER
to be set by a web server)ifconfig
output, which can include multiple non-public interfacesThis is what I would suggest:
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.
Also you could get IP via DNS A record based on hostname or server name:
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.
You could parse it from a service like ip6.me: