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.

code-8
  • 54,650
  • 106
  • 352
  • 604
  • 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? – Bolovsky Aug 20 '15 at 21:49
  • try this and see what you get as result `print_r($_SERVER['REMOTE_ADDR']);` – mdamia Aug 20 '15 at 21:51
  • 1
    Are you running a local (development) server and trying to get your own IP address by accessing it? – Bogdan Aug 20 '15 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. – code-8 Aug 20 '15 at 21:55
  • @Bolovsky: I'm not that good with VM. I'm a noob. I just want to grab my user ip address when they register my site. I just want to get the rough idea of where they are at in the world. – code-8 Aug 20 '15 at 21:57
  • 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 – Bolovsky Aug 20 '15 at 22:02
  • @Bogdan : I got it now, thank-you for commenting on my post. :) – code-8 Aug 20 '15 at 22:02

4 Answers4

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

dd("Public IP: ".$ip); //"Public IP: 24.62.137.161"
code-8
  • 54,650
  • 106
  • 352
  • 604
0

try this to Grab public IP address using Laravel,

Request::getClientIp()
  • I'm sorry. I tried that already. I keep getting `::1`. Thanks for suggesting. :-) – code-8 Aug 20 '15 at 21:58
  • 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. – vishal-mote Jan 14 '20 at 10:33
0

You Can Try This

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

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

Haroon Akram
  • 127
  • 3