PHP Check if Hostname resolved to local machine - php

I want to use PHP to check if a specified host name is resolving to the server on which the PHP script is running. By host name, I'm mean the same as a domain name in my particular context.
I say for example, let's say I have a server and it's public IP address is 123.123.123.123 or something similar. My DNS server is resolving two domains to this IP address: domain1.com and domain2.com . I'm accessing the PHP page on the server by domain1.com but I want the script to tell me if domain2.com is resolving to the server and return an appropriate result, like echoing 'yes' or 'no'.
I'm not sure how to approach this. I'm thinking of two possibilities:
Try something similar to a 'ping' of the hostname and see if there's a response, though I don't think this will work because it's local to local and may require the hostname to be resolved in the server's hosts file, which defeats the point of this (I want to see if the hostname is resolving for other clients on the Internet, not the local machine), or
check the hostname's DNS server to see if it resolves to the server's public IP, in which case I'd have to get the public IP somehow in PHP.
How would I approach this?
An example PHP script would be:
function host_resolves($host){
// get server public IP
// check if hostname is a valid domain name
// try to connect to the hostname's DNS server
// query the DNS server and compare IP (maybe in A record?) to public IP
// split off a sub-domain and check for a CNAME if needed
}
if(host_resolves('domain2.com')){
echo 'yep!';
}
else{
echo 'nup :(';
}

Related

PHP - check if domain matches server's IP when domain's IP is dedicated

I have a remote login script that user hosts (runs) on his server. During registration, user needs to specify a domain he will login from. When user runs script on his domain and logins to my server for the 1st time, I log his IP using:
$ip_address = $_SERVER['REMOTE_ADDR'];
When user logins 2nd time, I check if his IP address is still the same (using the same function above). Then I check if he still uses the same domain using:
$domain = $_SERVER['HTTP_REFERER'];
Finally, besides other security checks, I also check if specified domain really points to IP address using:
$domain_ips_array = gethostbynamel($domain);
if (in_array($ip_address, $domain_ips_array)) {
echo "Wonderful, domain really points to this IP";
}
But there's a problem when domain points to a dedicated IP. For example, if server's IP (where script is actually hosted) is 1.1.1.1 (this IP is also returned by $_SERVER['REMOTE_ADDR']), but domain is configured to use a dedicated IP 1.1.1.2, gethostbynamel function will only return 1.1.1.2, and check will fail (even if domain is actually hosted on server with IP address 1.1.1.1).
How do I solve this issue? Put simply, I need to be sure that user always runs the script on the same IP/domain, and if any of these is changed, alert is displayed.
I think you have a better luck with refactoring a little bit of the script you send to the user.
For example when a first login comes along, you can see the IP from the requested server.
Do your magic and return the IP to your script. Then store it somewhere and always send it after that.
This way you will always have the first IP. And then check with the $_SERVER variable. Domains can be changed and I think it's not that reliable.
Said it more simple, you need to have it stored somewhere.
-EDIT
You can use the function gethostbyaddr.
This will return you the domain of the IP. So you can store the domain from the first request as well and then check it with every other.

gethostbyname function returns domain name instead of IP address in PHP

I want to get machine IP address in PHP so i am using function
<?php echo gethostbyname($serverName); ?>
Its works for all my server machine but it returns domain name for particular IP address for some machine. I don't know why its returning domain name for particular machine and how to get IP address of that particular machine.
Thanks in advance.
According to docs
Returns the IPv4 address or a string containing the unmodified hostname on failure.
Failure reasons may be couple (e.g. wrong domain name, unreachable host or network problems)

server IP address of my website rather than domain IP

Hi I need to know the IP address of my website to give to my payment gateway. I know I can find an IP address by going to any of the websites which come up on google, but my understanding is that they give the domain's IP address which may be different from the IP address of the server the site is hosted on.
So how to I find out what the IP address of my server is?
Thanks :)
Run this from the command line to get your server's outgoing IP:
wget -q -O - checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//'
The server's IP address can be found with the $_SERVER variable:
<?php
echo $_SERVER['SERVER_ADDR'];
you can also do a quick PHP output of
<?php
phpinfo();
?>
it will contain all the values you need for your host details (just don't leave this file public after you use it).
What do you mean by the domain IP? The IP returned from a DNS lookup when querying your domain? This is most likely equal to the IP of your (shared) server, otherwise your website would be unreachable.
An exception to that is when the IP your DNS record represents belongs to a proxy server, which forwards the request to the real server.
You could always get the public IP address of the server by visiting www.whatismyip.com from the server.

PHP - IP address echoed as 127.0.0.1

I'm quite a novice at PHP.
I would like the IP address that I (myself, only. I modified my hosts file for the HotSpot shield webpage) have been given when using HotSpot shield to show up on my webpage, but unfortunately, it's echoed as 127.0.0.1. The main objective is that when I connect, the IP address that I've been set is echoed on the page. This is what code I've used:
<?php $rmt = $_SERVER["REMOTE_ADDR"]; echo "$rmt \n" ?>
The only problem is is that $rmt is 127.0.0.1. Do any of you know how to get it to display the public IP address to be displayed?
This can happen with local proxy servers; you could check out the other headers that are sent with your request by var_dump($_SERVER); and search for stuff like X-Forwarded-For or X-Real-IP
$_SERVER['REMOTE_ADDR'] is referring to the IP adress from which you connected. If you're running the server on your local machine and connecting from your local machine, it uses your local ip (127.0.0.1 is always "localhost").
In order to show a different ip you need to run it on another server (a public web hotel server preferably), or connect to your server from another machine.
I had just the same issue.
As it turns out, I was getting the proxy IP address instead of my own IP.
So I ran:
var_dump($_SERVER)
//you could also use print_r($_SERVER);
And then looked for something like this:
["HTTP_X_REAL_IP"]
Then captured it into a var like this:
$ip = getenv('HTTP_X_REAL_IP')

My IP is showing up wrong in PHP home server

Ok simple enough
<?PHP
echo $_SERVER[REMOTE_ADDR];
?>
Ok maybe not, I my IP is currently 72.184.212.85 however the code above which I am using on an IP blocking system for a script shows my IP as my home server IP of 127.0.0.1
So when I go to my script my IP is shown as 127.0.0.1 but when I go to other websites it is shown as 72.184.212.85
How can I get the first value to show on my test server?
$_SERVER['REMOTE_ADDR'] will always show the IP address from which the request came. If you access your own script on your own computer within your own network, your external IP address never comes into play. The request would have to leave your local network and then come back in for the external address to show up, but if it's all local, that'll never happen.
You'll have to make your server publicly accessible and then access it from the public address. I'm guessing you're currently using localhost to access your server?
run your server say port 8080 and then forward the port in your router so it's public to the internet. Then visit your webpage/phpscript from http://72.184.212.85:8080 instead of http://localhost:8080.
Here is a ridiculous solution that I wouldn't recommend:
Register your home IP with a domain name, then see where the request came from via URL:
$url = $_SERVER["SERVER_NAME"];
or
$url = $_SERVER["HTTP_HOST"];
and then do a dns lookup of that result, which should return the IP it's registered to, ie your external IP.
$ext_ip = gethostbyaddr($url);
The only reason this wouldn't work (so sorry if I'm wrong), is if SERVER_NAME uses the same method as "REMOTE_HOST", which is a reverse DNS lookup, which won't resolve, as your internal IP won't be registered to that domain name. An easy way to check is to do either:
phpinfo();
and see what the environmental variables are.

Categories