Enquiry Email Form

A lot of people rely on the $_SERVER[‘REMOTE_ADDR’] global variable to retrieve a user IP address, but there’s a better more reliable way of retrieving a vistor’s/user’s IP Address…

Retrieve IP Address function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
function getIpAddress()
{
    if (!empty($_SERVER['HTTP_CLIENT_IP']))
    {
    	//check ip from share internet
      $ip = $_SERVER['HTTP_CLIENT_IP'];
    }
    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   
    {
    	//check ip is pass from proxy
      $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    else
    {
      $ip = $_SERVER['REMOTE_ADDR'];
    }
 
    return $ip;
}
 
echo getIpAddress();

Learning & sharing

This is where we store some of our Web Development thoughts, tips and tricks for both our own uses and for others to learn from. Sharing is caring.

Please feel free to contribute to our blog posts; perhaps even teach us a few tricks of your own. We'd love to hear your thoughts.



X