$_SERVER[“REMOTE_ADDR”] gives server IP rather than visitor IP

后端 未结 8 793
小蘑菇
小蘑菇 2020-11-28 05:55

I\'m trying to track IP addresses of visitors. When using $_SERVER[\"REMOTE_ADDR\"], I get the server\'s IP address rather than the visitor\'s. I tried this on

8条回答
  •  忘掉有多难
    2020-11-28 06:23

    REMOTE_ADDR can not be trusted.

    Anyway, try

    $ipAddress = $_SERVER['REMOTE_ADDR'];
    if (array_key_exists('HTTP_X_FORWARDED_FOR', $_SERVER)) {
        $ipAddress = array_pop(explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']));
    }

    Edit: Also, does your server's router have port forwarding enabled? It may be possible that it's messing with the packets.

    Security warning: REMOTE_ADDR can be fully trusted! It comes from your own webserver and contains the IP that was used to access it. You don't even need to quote() it for SQL inserts.
    But HTTP_X_FORWARDED_FOR is taken directly from the HTTP headers, it can contain the picture of a cat, malicious code, any content. Treat it like that, never trust it.

提交回复
热议问题