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

This question already has an answer here:
How to get server's public IP address using PHP?
(1 answer)
Closed 1 year ago.
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.

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

You can use API to get public IP address from localhost.
https://www.ipify.org/
<?php
$ip = file_get_contents('https://api.ipify.org');
echo "My public IP address is: " . $ip;
?>

try this too and check with print_r
$ip= isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];

Try this as it is tested on local machine(localhost)
$ip = file_get_contents('https://api.ipify.org'); echo $ip;

I Think you should use $_SERVER['REMOTE_ADDR'] to get the IP address of the router

Related

PHP External IP

I am using software called gns3 to build networks.
I have the need to find out devices external ip from within an gns3 internal network. This would be like a pc behind a router doing NAT.
Please could someone tell me if there is a way that you can echo out the clients external ip in php so I can add a web server to one of my virtual networks within gns3 so I can visit it on some devices and find their external ip’s for testing?
Thanks in advance
You need to make a request to an external website that could tell you the ip address you have. You may use checkip.dyndns.org - click the link to see your own ip addres.
if you want to do that from php here is an example code:
<?php
//get website content as a string
$ipCheckUrl = 'http://checkip.dyndns.org';
$subject = file_get_contents($ipCheckUrl);
//extract from string just ip address
$pattern = '/Current IP Address\:\s*(\d+\.\d+\.\d+\.\d+)/U';
$ip = preg_match($pattern,$subject,$result) ? $result[1] : 'Error';
//print ip address.
echo $ip;

How to get the Client IP that request a Tor WebPage?

i'm running an hidden service as the final exam of my school, but i've a problem: i have to show that the IP of the same computer which connects to my website can change in a bit , but when i try functions such as $ip=$_SERVER['REMOTE_ADDR']; it always gives me 127.0.0.1 instead of the last tor node which connects to me.
How can i solve this ? Is it normal on the Tor network? Cheers in advance!
There is a no direct way , but you can try using a third party server to get actual ip even from localhost server.
<?php
$content = file_get_contents('http://checkip.dyndns.com/');
preg_match('/Current IP Address: \[?([:.0-9a-fA-F]+)\]?/', $content, $ip);
echo $externalIp = $ip[1];
?>
Here is a working PHPFiddle . Hope this will help, cheers :)

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.
$ip = trim(shell_exec("dig +short myip.opendns.com #resolver1.opendns.com"));
dd("Public IP: ".$ip); //"Public IP: 24.62.137.161"
try this to Grab public IP address using Laravel,
Request::getClientIp()
You can get the exact IP with the following function
gethostbyname(trim(hostname))

i want know system system localhost IP address in PHP?

here i am using $_SERVER['remote_addr'] i am getting the IP address INTERNET providers. please help me out.
function getRealIpAddr()
{
if (!empty($_SERVER['HTTP_CLIENT_IP'])) //check ip from share internet
{
$ip=$_SERVER['HTTP_CLIENT_IP'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) //to check ip is pass from proxy
{
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
$ip=$_SERVER['REMOTE_ADDR'];
}
return $ip;
}
It would seem that the question is looking for the server's IP address, and the problem is that the supplied code is returning the remote IP. If you want the IP address of the server, try $_SERVER['SERVER_ADDR'] . See $_SERVER for more details, or clearly define what you mean by "system IP".
You will not be able to get the client's local IP address from PHP like this, you will only ever get the
common gateway IP address
as you put it. This is because the request will appear to be coming from the router or whatever is between your client and the web server

Getting IP address in php

I am this code to get the IP address on my locolhost and I get
::1
result
code is:
function ip()
{
if (!empty($_SERVER['HTTP_CLIENT_IP']))
{
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
{
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
echo ip();
where is error?
Actually, I want to get country & city name after I get IP... Any other solution?
Thanks
::1 is shorthand for localhost, using IPv6 terminology, so it is working. Try connecting using your hostname, and you should see the address change to be more obviously an address...
As #Roland writes it is an IPv6 address. If you don't intend on using IPv6 in your application then you should tell your web server to stop listening on IPv6 ports. When you send a request the client will try IPv6 first if it doesn't work then it will fall back to IPv4 and you should get the much more familiar looking 127.0.0.1.
You could use the following:
$server_address = $_SERVER['SERVER_ADDR'];
$port_used = $_SERVER['SERVER_PORT'];
$ip_address = $_SERVER['REMOTE_ADDR'];
// on my test machine this gives the following results:
$server_address = 127.0.0.1
$port_used = 80
$ip_address = 127.0.0.1
Edited: to include the geo-location aspect of the question, that I hadn't noticed 'til after I submitted the original answer.
Rather than repeat answers found elsewhere, I'll first link to this (well-answered) SO question:
google-geolocation-api-library, and then to the Google results page for the search terms geolocation php site:stackoverflow.com, which links to many other -probably relevant- answers that might better address your questions than I'm able.
If you print_r($_SERVER) you will see what information is available, what it looks like, and what fields might be empty.

Categories