0

Hello,
Currently I am using my computer as a webserver, as I own an IP I have made the config to access it from outside.
So now I can access my php sripts via http://MY_PUBLIC_IP:PORT/site.php
The weird thing is that I cant get the visitors IP
This script is working very good on sites with no ip on the url. Could someone help me with this issue?!

function getUserIP(){
    $client  = @$_SERVER['HTTP_CLIENT_IP'];
    $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
    $remote  = $_SERVER['REMOTE_ADDR'];
    if(filter_var($client, FILTER_VALIDATE_IP)){
            $ip = $client;
        }elseif(filter_var($forward, FILTER_VALIDATE_IP)){
            $ip = $forward;
        }else{
            $ip = $remote;
    }
    return $ip;
}
2
Contributors
1
Reply
19
Views
2 Years
Discussion Span
Last Post by cereal
0

Hi,

the code seems fine to me. But you can try this function which extends a bit the check:

function get_ip_address()
{
    $array = array(
        'HTTP_CLIENT_IP',
        'HTTP_X_FORWARDED_FOR',
        'HTTP_X_FORWARDED',
        'HTTP_X_CLUSTER_CLIENT_IP',
        'HTTP_FORWARDED_FOR',
        'HTTP_FORWARDED',
        'REMOTE_ADDR'
        );

    foreach($array as $key)
        if(array_key_exists($key, $_SERVER) === true)
            foreach(explode(',', $_SERVER[$key]) as $ip)
                if(filter_var($ip, FILTER_VALIDATE_IP) !== false)
                    return $ip;
}

Source: http://www.kavoir.com/2010/03/php-how-to-detect-get-the-real-client-ip-address-of-website-visitors.html

Besides: are you using Apache as webserver? Check the access and error logs for Apache and the error log for PHP, to verify if there's something wrong with your configuration. In particular the access log for Apache should save the IP address for each client request.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.