Local IP Address

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
chrislee
Forum Newbie
Posts: 6
Joined: Fri Jul 25, 2003 8:21 pm

Local IP Address

Post 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
Brian
Forum Contributor
Posts: 116
Joined: Thu Apr 18, 2002 5:33 pm

Your Local IP Address

Post 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.
chrislee
Forum Newbie
Posts: 6
Joined: Fri Jul 25, 2003 8:21 pm

Post 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.
Brian
Forum Contributor
Posts: 116
Joined: Thu Apr 18, 2002 5:33 pm

Your Local IP Address

Post 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.
Post Reply