# 抓取最準的IP，從近到遠做判斷

```php
if (!empty($_SERVER["HTTP_CLIENT_IP"])){
    $ipaddress = $_SERVER["HTTP_CLIENT_IP"];
} else if(!empty($_SERVER["HTTP_X_FORWARDED_FOR"])){
    $ipaddress = $_SERVER["HTTP_X_FORWARDED_FOR"];
} else if(!empty($_SERVER["HTTP_X_FORWARDED"])){
    $ipaddress = $_SERVER["HTTP_X_FORWARDED"];
} else if(!empty($_SERVER["HTTP_X_CLUSTER_CLIENT_IP"])){
    $ipaddress = $_SERVER["HTTP_X_CLUSTER_CLIENT_IP"];
} else if(!empty($_SERVER["HTTP_FORWARDED_FOR"])){
    $ipaddress = $_SERVER["HTTP_FORWARDED_FOR"];
} else if(!empty($_SERVER["HTTP_FORWARDED"])){
    $ipaddress = $_SERVER["HTTP_FORWARDED"];
} else {
    $ipaddress = $_SERVER["REMOTE_ADDR"];
}
```
