PHPThere are several cases that that we need to track the ip address of visitors on websites either for validation, security, spam prevention etc. Using php it’s easy to get visitor ip address. The post will demonstrate how you can get the ip address using PHP.

No Proxy detection

$ip = $_SERVER['REMOTE_ADDR'];

But If a user access the website via a proxy server $_SERVER[‘REMOTE_ADDR’] will return the ip address of proxy server not of the proxy user. So we have two methods here to get the ip address of a user.

Using $_SERVER

$_SERVER is an array created by the web server, that contains the server variables.

Using getenv() function

getenv() function is used to read the value of the environment variables in PHP. This function is useful when visitors are using the proxy server to access the site.