User IP address and location in Laravel 5.5

29,588

try this to get the ip address of user:

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

and after getting an ip address you can get the location from that ip using below package.

https://github.com/stevebauman/location

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

In the case of you get the localhost ip address then use this package to solve it: https://packagist.org/packages/fideloper/proxy

Share:
29,588
MA-2016
Author by

MA-2016

Updated on July 05, 2022

Comments

  • MA-2016
    MA-2016 almost 2 years

    I am trying to get the ip address and location of user during registration...when user sign up for the first time I want to save the ip address and his location in the database in users table.

    Please give me a solution to get the ip address and location of the new user...

    Below is the line which i am using but this gives me wrong IP address of localhost...

    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(),
        ]);
    
  • RïshïKêsh Kümar
    RïshïKêsh Kümar over 6 years
    hmm i try find wait
  • MA-2016
    MA-2016 over 6 years
    i am using this fideloper package but still getting the local host ip..not my original ip.
  • Kabir Hossain
    Kabir Hossain over 5 years
    If an user use a ip changer , what will happen ?