7

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.

7
  • 1
    ::1 is IPv6 version of 127.0.0.1, so I'm guessing you are calling yourself. I don't know which OS you are using, but maybe you should have a look at doing requests with eth0 instead of lo? Edit: since you want your external address, probably you will have to use some loopback. Right now, can't really thing of a solution, except maybe, bootup a couple vm's, change their ip address and do the requests between them? Commented Aug 20, 2015 at 21:49
  • try this and see what you get as result print_r($_SERVER['REMOTE_ADDR']); Commented Aug 20, 2015 at 21:51
  • 1
    Are you running a local (development) server and trying to get your own IP address by accessing it? Commented Aug 20, 2015 at 21:52
  • @Bogdan : I don't want to grab the ip of my localhost. I want to grab the IP of the user that view my site. Basically, I want to grab my user ip address. I'm not sure if this is possible. Commented Aug 20, 2015 at 21:55
  • 1
    @ihue this should work perfectly in a publicly reachable server. if you just want to get a feel for it, you can go to laravel entry script (don't know if you changed any default, probably should be app.php or something like that) and override $_SERVER['REMOTE_ADDR'] to the ip you want. Keep in mind this is a hack for you to be able to test it. To get you own IP will take you a lot of reading hours and effort Commented Aug 20, 2015 at 22:02

5 Answers 5

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

dd("Public IP: ".$ip); //"Public IP: 24.62.137.161" 
Sign up to request clarification or add additional context in comments.

3 Comments

Ahh,, thanks much. this is what I was looking from 2 days
Great answer , saved my day !!! , can you please explain what this command do dig +short myip.opendns.com @resolver1.opendns.com ?
@ßiansorÅ.Ålmerol Can someone suggest how to the client Ip address in the same way.
1

You Can Try This

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

Comments

0

try this to Grab public IP address using Laravel,

Request::getClientIp() 

2 Comments

I'm sorry. I tried that already. I keep getting ::1. Thanks for suggesting. :-)
when i fire ifconfig on client I am getting actual public IP of client, and when I request to my laravel server from the client and logging the ip using Request::getClientIp() then I am getting different IP of the client.
-1

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

Comments

-2

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.