Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get public ip address of my localhost system [duplicate]

Tags:

php

I am connected to a lan.. I can access internet. using the browser I can find my public ip using the search "what is my ip".

I want to get the public ip using php

I am running the script in my localhost wamp server..

I tried:

$_SERVER['REMOTE_ADDR'] and $_SERVER['SERVER_ADDR']` both give me `localhost ip ::1

Is there any networking functions that can give me my public ip address?

is there any way without using any external service? because if I use an external service, it may not available in the future.

like image 941
Sugumar Venkatesan Avatar asked Dec 23 '22 17:12

Sugumar Venkatesan


1 Answers

try this please:

$externalContent = file_get_contents('http://checkip.dyndns.com/');
preg_match('/Current IP Address: \[?([:.0-9a-fA-F]+)\]?/', $externalContent, $m);
$externalIp = $m[1];

Or use httpbin.org/ip as Priyesh Kumar suggests

like image 124
Alessandro Minoccheri Avatar answered Mar 05 '23 23:03

Alessandro Minoccheri