The title pretty much sums it up:
How can I get the server IP address using PHP from the CLI?
Using PHP’s CLI, $_SERVER does not contain the server’s IP address, so the obvious solution is out of the question.
Thanks!
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The thing is, when running PHP on the CLI, there’s no network activity involved whatsoever, hence no IP addresses. What you need to define first and foremost is, what IP you want to get.
The IP of the client running the script?
Not possible, since there is no “client”.
The local IP of a network interface?
Run some system utility like
ifconfig, but you may get more than one IP if the server has more than one network interface.The IP of a domain, which may or may not correspond to the server you’re running on?
Use
gethostbyname(), but you need to know which domain you want to query for.The public facing IP of the network/server you’re on?
You’ll need to use an external service like http://www.whatsmyip.org (note that they don’t like this) which will tell you what IP your request came from. Whether this address corresponds to anything useful is a different topic.
The bottom line is, if you’re interested in
$_SERVER['REMOTE_ADDR']or$_SERVER['SERVER_ADDR'], it simply does not apply to a CLI-run script.