DNS pre-resolve at server - php

Is there a way to resolve DNS at server (using PHP), then include that pre-resolved IP address with domain name in html? So client's browser fetches directly from that IP without having a need to resolve DNS first?
Here is a bit more clarification. My PHP generated page has many external links to js, css and image files from different domains. It takes much time when DNS is resolved on clients' browsers. It can be done very fast on server but I don't want to links those resources directly from IP address. Instead I want something that tells browser that the domain example.com is hosted at 1.1.1.1 for example and no need to perform a DNS lookup.
Is it possible? or can there be a workaround?
Some thing like dns-prefetch with a value or attribute?
<link rel="dns-prefetch" href="http://example.com" value="1.1.1.1" />
Thanks in advance.

Well, you can resolve dns-name to IP.
While PHP can't do this, you can create a test file on your server. After that you fetch request for his file. According OSI model, DNS client will resolve DNS name->IP, after that you will have IP in your HTTP headers, because your real query depend on IP of server.
Low level. Windows and Linux both have an API for resolving DNS name -> IP. So you can use their functions in C++ to get IP without test file.
Or you can construct DNS message: DNS-message => UDP-datagram => IP-package. After you send it to DNS-server and wait for answer.
Your DNS-server IP you can find in your config.

Related

PHP: gethostbyaddr() returning an invalid url

I'm working a PHP script to try and resolve a vague URL (for example typing in facebook.com) as an absolute url (such as https://www.facebook.com); similar to what your browser does on a daily basis using PHP.
So far I've got the following code:
$link = gethostbyname("facebook.com");
This provides an IPV4 address, which works, but then when I reverse lookup using:
$link2 = gethostbyaddr($link);
I'm expecting to receive a valid URL like "https://www.facebook.com", but instead, I get garbage such as "'edge-star-mini-shv-13-atn1.facebook.com'"
This then breaks any hope of using fopen or curl to try and read the contents of the webpage.
Can anyone explain what's gone wrong here and how I can resolve it?
EDIT: Attempting an insecure URL like "google.co.uk" returns "'lhr25s10-in-f3.1e100.net'", so it's not something to do with secure HTTP (HTTPS)
gethostbyaddr gets a hostname, not a URL, for an IP address.
Multiple hostnames can be assigned to a single IP address.
gethostbyaddr will get the default one.
An HTTP server listening on that IP address will handle requests to all the hostnames.
An HTTP request includes a request header called Host which specifies which hostname you are asking for.
The HTTP server can pay attention to that header and serve up different content for different hostnames. This allows multiple websites to be hosted on a single IP address. This is very useful since IPv4 addresses are in limited supply and there are many, many websites.
You are getting the default hostname for the computer hosting facebook.com, but the webserver isn't hosting the website you want on that hostname.

Site running by the host entry

I am running one site by host entry and pointing out it to another server.
For example,
My site is www.example.com and I did host entry and pointing it to xxx.xxx.xxx.xxx IP.
When I run URL into browser it is pointing to new IP correctly but internal URLs, JavaScripts and images are calling from it's original old server.
Is it any server configuration issue or something else ?
In which technology the website is built with. May be internal URLs, JavaScripts and images are stored in database like in wordpress CMS?
Resolving hostnames to/from IP is a DNS issue. Perhaps your DNS cache is corrupted so try flusing your DNS.
https://www.whatsmydns.net/flush-dns.html

change DNS with php like as if editing hosts file?

I don't know how to phrase this better, mainly because I dont really know how DNS fully works.
Given a URL and an IP, is it possible to tell PHP to make all requests to that IP AS the given URL instead of the real DNS (Just as if you edited your hosts file locally)?
The only solution I see is to run your own bind server, and tell your clients to use your bind server. You then have full control how a name is routed to a IP.
Unfortunately for your case, you are at the whims of your client.
If they type the IP address in their browser it will work, if they type the domain which has not been set up in DNS or in their hosts files, it will never reach a server that you control.
If you have designed you site correctly, it probably really shouldn't matter if they access it via domain name or IP address.
I think this is what apache does via .htacess or other configuration files. That is not a "change" to the DNS, but a way to manage every request as you want, even with a unique and generic .php file

How do you set the proxy IP with Phproxy?

I just downloaded PhProxy from here. When I went to some IP locator site my IP was a Dallas/Texas IP. Is there any way to change the IP PhProxy uses? I couldn't find a way to do it.
PHProxy - like all proxies - accepts requests from any of a number of IPs and acts on their behalf to fulfill that request. The IP you're seeing is almost certainly the IP address of your webserver, and no you can't edit some setting to change it. Your server makes a request to some address and includes its own address - this is how the remote end knows how to answer back. You couldn't change this with a PHProxy setting even if you wanted to - if you could, your packets wouldn't get back to you.
You could get a different webhost, if a Dallas-located IP is a real problem, but it would be locked to the new webhost just the same. Webservers can't exactly IP-hop...

Does a cURL post to the same site leave the local network?

I'm doing a PHP cURL post, using a complete URL (http://www.mysite.com), from one page to another on the same site. (I know this isn't the best way to do it; but for my purpose this is what I need)
My question is:
Will the cURL post still go out across the internet, do a name lookup and travel a route as though it were a post coming from a different site. Or will the post stay on the servers local network?
There are multiple parts to the request, the dns lookup and the get or post to the site.
DNS Records are usually cached on most OSes, so it's rather unlikely that the server would have to do a dns lookup for it's own external ip, but it's possible.
As for the post, let's assume a basic layout:
Firewall => DMZ Apache PHP Server (www.mysite.com)
222.xxx.xxx.123 => 192.168.0.2
And mysite.com resolves to 222.xxx.xxx.123, then your request will go to your firewall's external interface and bounce back in. That's not terribly public traffic, but it goes out none-the less.
However, if you wanted to bypass that, you could put an entry in the host file of the server to say
127.0.0.1 mysite.com
(assuming you control the server, ie not shared hosting)
No. The post itself (unless you have multiple interfaces and your routing is totally screwed up) will not traverse the internet. Your local host ought to be able to resolve its own name as well, but there is a possibility that a DNS request will be made to determine the IP address corresponding to the name. I would hope that the network stack implementation on your system would prevent the post's packets from even hitting the wire (similar to localhost), but I wouldn't count on it.
It depends on your network setup. Many sites have a domain name pointing to the IP address of a front facing router or load balancer which forward the request to the web server.
If that's the case a request to your own site can make a round-trip to the router. Though it's unlikely that the request will go through the internet unless you have a very unusual setup (such as round robin DNS with multiple datacenters).
You can avoid the round-trip by associating the site FQDN to the loopback interface in your webserver /etc/hosts which will also save you a DNS request.

Categories