Laravel 5.5 中的用户 IP 地址和位置

User IP address and location in Laravel 5.5

我试图在注册期间获取用户的 IP 地址和位置...当用户第一次注册时,我想在用户 table 的数据库中保存 IP 地址和他的位置.

请给我一个获取新用户的IP地址和位置的解决方案...

下面是我正在使用的行,但这给了我错误的本地主机 IP 地址...

127.0.0.1

$user = new User([
        'firstname' => $data['firstname'],
        'lastname' => $data['lastname'],
        'company' => $data['company'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
        'phone' => $data['phone'],
        'country' => $data['country'],
        'zipcode' => $data['zipcode'],
        'city' => $data['city'],
        'state' => $data['state'] == "other" ? $data['custom_state'] : $data['state'],
        'ip_address' => request()->ip(),
    ]);

试试这个来获取用户的 ip 地址:

'ip_address' => \Request::ip();

在获得 ip 地址后,您可以使用以下包从该 ip 获取位置。

https://github.com/stevebauman/location

'position' = Location::get(ip_address);

如果你得到本地主机的IP地址然后使用这个包来解决它:https://packagist.org/packages/fideloper/proxy

    $ipaddress = '';
    if (isset($_SERVER['HTTP_CLIENT_IP'])) {
        $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
    } else if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
    } else if (isset($_SERVER['HTTP_X_FORWARDED'])) {
        $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
    } else if (isset($_SERVER['HTTP_FORWARDED_FOR'])) {
        $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
    } else if (isset($_SERVER['HTTP_FORWARDED'])) {
        $ipaddress = $_SERVER['HTTP_FORWARDED'];
    } else if (isset($_SERVER['REMOTE_ADDR'])) {
        $ipaddress = $_SERVER['REMOTE_ADDR'];
    } else {
        $ipaddress = 'UNKNOWN';
    }
    $json     = file_get_contents("http://ipinfo.io/$ipaddress/geo");
    $json     = json_decode($json, true);
    $country  = $json['country'];
    $region   = $json['region'];
    $city     = $json['city'];