3

I want to track the IP address of the users who are opening my website. I have done lot of googling and even other questions asked on stackoverflow but couldn't find the solution. I actually want to know how to I get the IP address by using

 $_SERVER['REMOTE_ADDR'] or $_SERVER['REMOTE_HOST'] variables. 
3
  • 2
    use $_SERVER['REMOTE_ADDR'] Commented Aug 12, 2013 at 5:32
  • 2
    You just keep $_SERVER['REMOTE_ADDR'] into a variable like $ipaddress=$_SERVER['REMOTE_ADDR']; and if you want to save with user information into database then you can save $ipaddress value, if you want to see then use echo $ipaddress. Commented Aug 12, 2013 at 5:35
  • php.net/manual/en/reserved.variables.server.php Commented Aug 12, 2013 at 5:38

4 Answers 4

8

Use $_SERVER['REMOTE_ADDR']

if ($_SERVER['HTTP_CLIENT_IP']!="") { $ip = $_SERVER['HTTP_CLIENT_IP']; } elseif ($_SERVER['HTTP_X_FORWARDED_FOR']!="") { $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; } else { $ip = $_SERVER['REMOTE_ADDR']; } 
Sign up to request clarification or add additional context in comments.

Comments

3

If your client is connected through proxy server then $_SERVER['REMOTE_ADDR'] just returns the IP address of the proxy server not of the client' machine. This is the closest you can get to client' ip.

if (!empty($_SERVER['HTTP_CLIENT_IP'])) { $ip = $_SERVER['HTTP_CLIENT_IP']; } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; } else { $ip = $_SERVER['REMOTE_ADDR']; } 

Comments

1

Your answer is in your question: $_SERVER['REMOTE_ADDR'] will contain the IP of the user accessing your site. Just use it like you'd use any other variable.

Comments

0

For this the answer is simple. just use PHP's $_SERVER["REMOTE_ADDR"] to get the IP of the user who accesses a given page/url

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.