Page 1 of 1

Local IP Address

Posted: Fri Jul 25, 2003 8:21 pm
by chrislee
Hi,

How can I get the local Computer's IP Address when running PHP program in Windows command line (i.e. php myip.php)

Many thanks in advance.

Regards,
Chris

Your Local IP Address

Posted: Fri Jul 25, 2003 8:46 pm
by Brian
The IP address of your server (including a development workstation running a local instance of PHP) is stored in _SERVER["SERVER_ADDR"]. You can also use the TCP/IP local loopback address: 127.0.0.1.

Posted: Fri Jul 25, 2003 10:49 pm
by chrislee
I got following error when running the program in command prompt.
PHP Notice: Undefined index: SERVER_ADDR

I think Server variables: $_SERVER only return values when the php program under webserver but not on command prompt.

Your Local IP Address

Posted: Sat Jul 26, 2003 12:06 pm
by Brian
You are correct. "SERVER_ADDR" is actually pulled in from the Apache environment (if you happen to be using Apache, that is).

You could probably just use 127.0.0.1 unless you need your command line program to save or pass your current IP address for some reason.

On a Windows system, you can view your current IP address via the "ipconfig" command. You can probably capture its output then parse out the IP address if you really need to.

Another option would be to make a server program that returns only the IP address of the client then connect to that.