PHP - Domain to IP - php

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

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.

PHP Check subdomain sensitive to IP address

I'm having some issue with something that seemed simple to me at first, but is now proving very difficult. Maybe I'm over thinking it - would love your help.
Overview:
I have a web application with two interfaces (1 for clients and 1 for customers). All customers get routed one way and all clients get routed another. I determine which login screen to show based on their subdomain. customers get the base domain (+www) and clients go to [clientName].example.com
Problem:
I was determining this before by doing string manipulations on env("HTTP_HOST") -- see code below. However, this now poses an issue when using local IPs (for testing with other devices/ people). My code, which looks for '.' in the host environment fails because IPs have 3 dots and 'localhost' has 0
These are the different naming parameters I've established and how I'd like the route...
CUSTOMER ----------------------------- CLIENT
xyz.example.com ------>
<------ www.example.com
<------ example.com
xyz.localhost ------>
<------ www.localhost
<------ localhost
xyz.192.168.X.X ------>
<------ www.192.168.X.X
<------ 192.168.X.X
I could be really over thinking this and am hoping there's some simple Php function like "get_subdomain()" that will do this for me. But I'm not seeing it and would really love some help.
Thanks!
Current Code:
$host = env("HTTP_HOST");
$group = null;
// if there is exactly one dot IE example[dot]com then there is no subdomain
if(substr_count($host,'.')==1){
$group = 'customer'
}else{ // there is a subdomain
$subdomain = substr($host, 0, strpos($host, "."));
if($subdomain=='www'){
$group = 'customer'
}else{
$group = 'client'
}
}
CakePhp if that helps?
I hope this answer does not seem to avoid your specific question, but it seems to me that you are inviting alternatives in your question. I do think you are over-thinking the solution, especially since it is primarily to assist you in your test environment.
What I suggest is that you define your own local domains for testing instead of using IP Addresses. So for example you could (in your hosts file or equivalent) define:
xyzclient.mylocaldomain 192.168.1.1 [or whatever your local ip is]
www.mylocaldomain 192.168.1.1
xyz.localhost 192.168.1.1
www.localhost 192.168.1.1
Then you don't need to code any differently for local or live environments. Just in case: this guide to setting up and configuring local domains may be useful.

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?

Get Host Name with Curl using ip

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']

Categories