Get User IP Address


The simplest way to get ip address of is to use server variable $_SERVER['REMOTE_ADDR']. Ip address of user can be used for logging, statistics, geo-targeting or ban systems.

Anyway sometimes $_SERVER['REMOTE_ADDR']. is not real IP address, when user for example using a proxy. So for more accurate detection of user IP address we need a little bit more code.


Some web proxy even if hide user real IP address, by making request with proxy IP, it put in header request and user real IP address. This can be detected from PHP, by using a function to get user IP address like this one bellow:

function getUserIP(){
    $ip = $_SERVER['REMOTE_ADDR'];

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

    return $ip;

Add comment