Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

hardwair's avatar

I Need How store Ip address and place of user ?

I Need How store Ip address and place of user ?

1 like
5 replies
jj15's avatar

You can retrieve the user's IP address from the current request instance. For example, in a controller:

public function store(Request $request): mixed
{
    $ipAddress = $request->ip();
}

To store the IP address, you could add a text column to your database table (i.e. $table->string('ip_address')->nullable() in your migration) and then fill that with what you get from $request->ip().

To determine the geolocation of the IP address, you'd need to use an external service/API. There are many of these out there; some are free and some are paid, all with different limits and accuracy. I've used IP-API before; it's free and doesn't require an API key.

There are also some Composer packages out there that make this easier to do in a Laravel application (abstracting the API calls and such), Google will be your friend here :)

If you can provide more details on what you want to store the IP address for, you might get some more concrete suggestions, as your question is a bit broad.

1 like
tisuchi's avatar

@hardwair This is how you can get the IP address of a visitor:

// Get the IP address simply
$ip = request()->ip();

// Further
// For when behind proxies/load balancers
if (request()->hasHeader('X-Forwarded-For')) {
    $ip = request()->header('X-Forwarded-For');
}

// Some setups use these headers instead
$ip = request()->header('X-Real-IP') ?? $ip;
$ip = request()->header('CF-Connecting-IP') ?? $ip; // Cloudflare specific

To get the location from the IP, you need to use an external API. I use this free service: https://ipinfo.io/ Also there are more free services available.

To connect with this ipinfo service, I use this package: https://github.com/stevebauman/location

3 likes
Snapey's avatar

you changed the question? If you want to find where the user is based on their IP you need to use a third party service

1 like

Please or to participate in this conversation.