Raza Mehdi's Blog

Get a Client’s Remote IP Address in PHP

by on Jun.09, 2010, under PHP

In PHP, we use $_SERVER superglobals to access different server parameter values. We can also get the remote client IP address by use of this superglobal. But what if the remote client is connected to network at his/her location, and is accessing the site. It means there is a proxy through which he access the internet. To overcome this problem, write the following code in your PHP script to get a client remote IP address:

<?php

if (getenv(HTTP_X_FORWARDED_FOR)) {
$ipaddress = getenv(HTTP_X_FORWARDED_FOR);
} else {
$ipaddress = getenv(REMOTE_ADDR);
}

?>

Share and Enjoy:
  • Print
  • Digg
  • StumbleUpon
  • del.icio.us
  • Facebook
  • Yahoo! Buzz
  • Twitter
  • Google Bookmarks

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!