0

How do I get my system's IP address?

I am using $ip_address=$_SERVER[REMOTE_ADDR];, but it returns my localhost IP address instead of my system's IP address.

3
  • Is your server run on you local machine? ...cause then it will give you the same IP address. Commented Dec 15, 2010 at 7:40
  • Check out suite101.com/content/socket-programming-gethostbyname-a19557 Commented Dec 15, 2010 at 7:43
  • i am accessing webserver via Internet and not via local network Commented Dec 16, 2010 at 3:47

4 Answers 4

2

If you are accessing a local server, then your local IP address is the remote IP. PHP is giving you the correct response.

If you want to know your IP address to the rest of the internet, go to Google and type "what is my ip".

Sign up to request clarification or add additional context in comments.

Comments

2

$_SERVER['SERVER_ADDR']

See the php docs for a full list of stuff you might find in the $_SERVER superglobal.

2 Comments

@Ginamin: the OP is looking for the server address, not the remote address. That's the reason his code isn't returning the address he expects it to.
hmm... seems to me he is looking for remote address. "how do i get my system ip address" and "but it return my localhost ip address instead of my system ip" to me seems to point to remote address, but no worries. He has both answers in here now :)
1

This function will determine the real IP address:

function ipCheck() { if (getenv('HTTP_CLIENT_IP')) { $ip = getenv('HTTP_CLIENT_IP'); } elseif (getenv('HTTP_X_FORWARDED_FOR')) { $ip = getenv('HTTP_X_FORWARDED_FOR'); } elseif (getenv('HTTP_X_FORWARDED')) { $ip = getenv('HTTP_X_FORWARDED'); } elseif (getenv('HTTP_FORWARDED_FOR')) { $ip = getenv('HTTP_FORWARDED_FOR'); } elseif (getenv('HTTP_FORWARDED')) { $ip = getenv('HTTP_FORWARDED'); } else { $ip = $_SERVER['REMOTE_ADDR']; } return $ip; } 

1 Comment

i am access my local server so it returns the server ip only not my sysytem ip. is it any other way to do this
0

A late reply but I hope it still might be able to help someone. I needed system ip for a project so I quickly scrapped up a custom function using regular expressions. It works on ubuntu linux (and technically should work on any distro/version of linux).

function getLocalIp(){ $output = shell_exec('/sbin/ifconfig'); preg_match("/inet?[[:space:]]?addr:([0-9.]+)/", $output, $matches); if(isset($matches[1])) $ip = $matches[1]; else $ip = 0; return $ip; } 

It requires:

  • making a system call using shell_exec.
  • using regular expressions to find the value of internal ip address.

If you happen to create something similar for windows or improve the functionality of this function, please share your code with the rest of us.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.