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))
Related
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
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 :)
My server is behind a proxy and I'm trying to get a IP of a user I'm using in PHP
$IP = $_SERVER['HTTP_X_FORWARDED_FOR'];
The problem I see lately some users Ip's are like this below example Ip's.
173.0.0.000, 190.000.00.01
173.0.0.000, 190.000.00.02
173.0.0.000, 190.000.00.03
173.0.0.000, 190.000.00.04
173.0.0.000, 190.000.00.05
How would I go about just getting the correct IP like below. It doesn't happen on all the Ip's.
The format for X-Forwarded-For is: client1, proxy1, proxy2. So you will want to grab the first address in the list:
$IP = current(explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']));
echo $IP; // 173.0.0.000
That will grab the first IP address whether there are multiple addresses or just one.
How do I get something in PHP/Curl to grab the IP of the URL for me? It's going to be a simple interface that fetches URL IP's back for me.
E.g. Enter "http://mysite.com" then I hit submit then I should get the IP of the mysite.com back.
gethostbyname
$ip_string = gethostbyname( "stackoverflow.com" );
echo $ip_string; //returns ip address, ie 12.34.56.78
Try the gethostbyname function.
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.