code-8
code-8

Reputation: 58652

Grab public IP address using Laravel

My current IP is : 24.62.137.161

and when I use

$ip = $request->getClientIp(); dd($ip);

I keep getting ::1

How can I grab 24.62.137.161 ? I'm not sure if what I'm trying to do if possible.

Any hints / suggestion will be much appreciated.

Upvotes: 7

Views: 12096

Answers (5)

starter-dev
starter-dev

Reputation: 27

It is the expected result when you are in local environment (development mode). But when the app is running in production, you will get the public IP address that you want.

Summary base on your question:

  • In local: 127.0.0.1
  • In production: 24.62.137.161

Upvotes: -1

laravel sagmetic
laravel sagmetic

Reputation: 242

You Can Try This

$ip_address = file_get_contents('https://api.ipify.org');
print_r($ip_address);

Upvotes: 1

Haroon Akram
Haroon Akram

Reputation: 127

You can get the exact IP with the following function gethostbyname(trim(hostname))

Upvotes: -2

code-8
code-8

Reputation: 58652

$ip = trim(shell_exec("dig +short myip.opendns.com @resolver1.opendns.com"));

dd("Public IP: ".$ip); //"Public IP: 24.62.137.161"

Upvotes: 13

user4628565
user4628565

Reputation:

try this to Grab public IP address using Laravel,

Request::getClientIp()

Upvotes: 0

Related Questions