get-ip-address

Get Real IP address using PHP

To get user IP in PHP, a lot of programmer would use $_SERVER[‘REMOTE_ADDR’]. However, it may not return the true IP address of the client.

When will $_SERVER[‘REMOTE_ADDR’] not return true IP ??

1)Client access through Proxy Server

It is because the client may be connected to your site through Proxy Server. By using $_SERVER[‘REMOTE_ADDR’], you may be able to the IP address of the proxy server not the client server.

2) Web Server sit behind loadbalancer

If your Web Server sit behind load balancer. The icoming request were recieved at load balancer and forward the request to your server. Under this suitation, by using $_SERVER[‘REMOTE_ADDR’], you may not be able to get the client IP address correctly.

Solution

In PHP, HTTP_CLIENT_IP and HTTP_X_FORWARDED_FOR, there are two extra Server variable which are useful to determine exact IP address of the client’s machine.

function getRealIPAddress()
 {
 if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check ip from share internet
 {
 $ip=$_SERVER['HTTP_CLIENT_IP'];
 }
 elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip is pass from proxy
 {
 $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
 }
 else
 {
 $ip=$_SERVER['REMOTE_ADDR'];
 }
 return $ip;
 }

Related Posts


4 thoughts on “Get Real IP address using PHP

  1. kore dizileri izle

    i bookmarked you in my browser admin thank you a lot i will probably be in search of your subsequent posts

    Reply
  2. Palma

    I used to be searching for this excellent sharing admin significantly thanks and also have nice running a blog bye

    Reply
  3. how to find ppl on yahoo messenger

    My brother recommended I would possibly like this web site. He was once entirely right. This put up actually made my day. You can not imagine just how a lot time I had spent for this info! Thank you!

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *


*