Get Host Name with Curl using ip - php

Is there a way to get the host name using CURL, or what is the preferred way using PHP?

You don't need to do this in curl. Just use the gethostbyaddr function.
echo gethostbyaddr('1.2.3.4');

My suggestion would be to experiment without using cURL.
Try looking at: gethostbyname(); and gethostbyaddr();
Basicly:
Get host IP address by using gethostbyname();
Fetch host name by using gethostbyaddr(); with previously fetched IP address.
$ip = gethostbyname('www.example.com');
$host = gethostbyaddr($ip);
echo $host;
Just tested it, and — works, plus, you don't have to know targeted host's IP address.

http://php.net/manual/en/function.gethostbyaddr.php

I don't think you need cURL for this. gethostbyaddr does a reverse DNS lookup. I believe that's what you want.

Could also be gotten with $_SERVER, specifically $_SERVER['HTTP_HOST']

Related

How to get the Public IP

I'm trying to get the Public IP of someone that use the form of the page I do.
I don't know with which programming language would do that. I was reading on the web and I found some:
// PHP Code
$_SERVER["REMOTE_ADDR"]
This outputs: 127.0.0.1 (Local IP).
Then I found this too:
// PHP Code
$externalContent = file_get_contents('http://checkip.dyndns.com/');
preg_match('/\b(?:\d{1,3}\.){3}\d{1,3}\b/', $externalContent, $m);
$externalIp = $m[0];
This outputs the correct IP (Public IP), but that needs to use other web page (http://checkip.dyndns.com/).
I wonder How do the pages like that get the Public IP?. I am looking for a way to get it without need to use other web page. Thanks.
The $_SERVER["REMOTE_ADDR"] should work fine for what you are trying to do here. The reason you are getting 127.0.0.1 is because you are running this in a local environment.
If you put this script on a live webserver and I access it, you will get the same IP from $_SERVER["REMOTE_ADDR"] as I get when I check whatismyip
And anyhow, having the server call:
$externalContent = file_get_contents('http://checkip.dyndns.com/');
will only get you successful in returning your servers IP address, not the visitors.
This problem had me stumped for a long while.
If you have access to your own remote server, what I did to solve this problem was create a simple server-side script echo $_SERVER['REMOTE_ADDR']; to give me the public IP assigned by my ISP to my device from my localhost.

How to know domain name from IP address in PHP

As we know about PHP has an inbuilt function to get IP address of a domain name
<?php
$ip = gethostbyname('www.example.com');
echo $ip;
?>
But is there any way to know domain name from Ip address?
I have tried using gethostbyaddr but it din't worked.
<?php echo gethostbyaddr( '198.252.206.16' ); ?>
I think there should be some way of using the command dig in combination with PHP in Linux but I am not sure.
You can get the A adress of that server. If there are multiple websites on that webserver you do not get that information
Try using a valid IP address. I tried going to the IP address that you provided, but nothing was there.
If you have shell access, Unix/Linux servers can use this for a
timeout response:
shell_exec('host -W 2 0.0.0.0');
Where 0.0.0.0 is of course the IP, and '2' is the number of seconds
for the timeout. This returns a more detailed string of info, with
some additional text which might vary depending on the system, so if
you want a string with the hostname and nothing else, you'll have to
do some substring cutting.
Try this.

Why is fopen() giving me a different IP than $_SERVER['SERVER_ADDR']?

Using this code:
On server A I have this:
$handle = fopen('http://www.server_b.com/get_ip.php', 'r'); //This is just a PHP file that echoes the REMOTE_ADDR
echo "IP looks like ".fread($handle, '100000')." to external server.\n";
fclose($handle);
echo "IP looks like ".$_SERVER['SERVER_ADDR']." to this server.";
on server B I have this:
echo $_SERVER['REMOTE_ADDR'];
I'm getting the following output from server A:
IP looks like xxx.xxx.223.90 to external server. //xxx.xxx on both lines are the same
IP looks like xxx.xxx.223.94 to this server.
Why am I getting two different IPs? Note, we do own the IP range from .90-.94
Since it's a VPS, what you're probably seeing on server_b is the IP address of the host machine of the VPS that server_a is running on.
Either that, or there's some other proxying mechanism going on.
There may be a way around this:
Do a print_r() of $_SERVER on server_b.
Depending on the config of the various servers involved, in addition to the REMOTE_ADDR you may also get a value like $_SERVER['HTTP_FORWARDED']. This will be the IP address of the originating machine being passed on by the proxy, and should be the one you're expecting.
This question may help you further: What is the most accurate way to retrieve a user's correct IP address in PHP?

inet_pton does not work on local ip addresses like 10.0.1.4

<?php
//$ipaddress = $_SERVER['REMOTE_ADDR'];
$ipaddress = '10.0.1.4';
$binip = inet_pton($ipaddress);
echo $binip;
?>
It returns blank. If i use my public facing IP it returns the right result. It will return a result for 192.168.1.2(My old LinkSys router had a ip similar to this). I'm testing my app on a Linux machine i have with a local ip. All Machines that connect to it uses a internal ip 10.0.1.XX. My app uses MySQL and if the binip is blank, it gives me a error. So i am not sure of a work around for local ip. I was thinking maybe it has to do with detecting if its a local ip in that format and then edit the ipaddress variable so its valid for inet_pton some how. Any ideas?
It works for me. I get a binary structure from your example code (although, there's no printable characters in it so if you run it in a web browser you'll probably get nothing).
Actually, I even tried this code which outputs the original address and shows that there's no information loss going on:
<?php
$ipaddress = '10.0.1.4';
$binip = inet_pton($ipaddress);
echo inet_ntop($binip);
?>
Maybe you are more interested in ip2long which converts the address to an integer? Or maybe your problem is elsewhere, for example in escaping the data before putting it into a database?
You have similar issues here:
http://php.net/manual/en/function.inet-pton.php
You can find out solutions from there too. Check out how that function returns FALSE.

PHP - Domain to IP

I current have a system which works like this:
Insert IP and it will post the IP to another .php page. However, when I try post http://google.com it does not turn the domain into a IP.
How would I do that? E.g. when a user inserts http://google.com or any domain it will auto resolve the IP.
I know the function gethostbyadd, I dont know how to structure it out e.g. Forms, table, post data.
Thanks if any can help.
What have you got together so far? How is it failing?
A wild guess is that you're typing in http://google.com/ and trying to get an IP from that, and that will fail, as the URL contains protocol information as well. You need to pass the domain name, and only the domain name to gethostbyname:
gethostbyname('www.google.com'); // Works
gethostbyname('http://www.google.com'); // Will not work
If you have the protocol part (http://) in the beginning, you can use parse_url:
gethostbyname(parse_url('http://www.google.com', PHP_URL_HOST));
If you're having some other, specific problem, let us know. If you don't know where to start, I suggest start by reading up on a programming manual ;)
I think you're looking for gethostbyname:
$ip = gethostbyname('www.google.com');
Note, make sure you strip the http:// and any white space/trailing characters as this will likely prevent accurate results.
the function you are looking for is $x = gethostbyname('stackoverflow.com');
You probably need to look into using http://www.php.net/manual/en/function.gethostbynamel.php
In some people the function gethostbyname is running slowly or running once in a while. Some say that the apache needs rebooting to get the function started. I can not confirm this, but I want to give an alternative method how to find IP by Domain using nslookup
function getAddrByHost($host, $timeout = 1) {
$query = `nslookup -timeout=$timeout -retry=1 $host`;
if(preg_match('/\nAddress: (.*)\n/', $query, $matches))
return trim($matches[1]);
}
echo getAddrByHost('example.com');
speed test using XHProf:
attempt 1
gethostbyname 5,014,593 microsec
getAddrByHost 29,656 microsec
attempt 2
gethostbyname 5,016,678 microsec
getAddrByHost 13,887 microsec
attempt 3
gethostbyname 5,014,640 microsec
getAddrByHost 8,297 microsec
Conclusion: the function gethostbyname is performed for more than 5 seconds, which is very long. Therefore, I advise you to use a faster function getAddrByHost
note: php use this file /etc/resolv.conf to get DNS servers:
In my case I use BIND (named) which works on the host 127.0.0.1
# /etc/resolv.conf
nameserver 127.0.0.1
nameserver 8.8.8.8
nameserver 4.4.4.4

Categories