Link to home
Start Free TrialLog in
Avatar of Starquest321
Starquest321

asked on

Fetch CNAME record of domain from PHP Code

I am using the following query to get the CNAME record to load my site properly. The issue: The code below works ... but only if refreshed a couple of times.

Query ::

$recsDNS = dns_get_record($_SERVER['HTTP_HOST'], DNS_CNAME );
print_r($recsDNS);

Not getting CNAME records Properly, SOme times coming and some times Not.

If I use DNS_ALL :: After refreshing 3 to 4 times i am getting CNAME records.
Avatar of Chris Jones
Chris Jones
Flag of United Kingdom of Great Britain and Northern Ireland image

It sounds like it's timing out.

Are your DNS servers reachable and fast? Is the CLI dig problematic/slow?

Are you able to write into the code a timeout value?
I have just found this:
http://stackoverflow.com/questions/3111227/why-wouldnt-dns-get-record-show-cnames-when-i-know-they-exist

I'm wondering if this would be related to your issue?
If you read the docs, you will probably come to the same conclusion that I have: This is an unreliable way to test for information because it relies on many factors that are outside of your control.
http://php.net/manual/en/function.dns-get-record.php

Can you please tell us the "50,000 foot view" of what you're trying to do?  What is the business problem that you're trying to solve?  Maybe there is a more reliable approach than querying foreign servers.
Avatar of Starquest321
Starquest321

ASKER

The platform we are rolling out sits on set subdomains. For example: sub1.domain.com :: sub2.domain.com :: sub3...

Now when a new customer comes we simply want to point customer1 to sub1.domain.com so it's ready to go instantly.

We seem to be able to achieve this but its not reliable - we have to refresh several times. It's off :: on
Are you wanting to route all new customers to their own individually chosen subdomains?   So if the customer is "ray" I would get "ray.domain.com" ?
Correct . .but to the "backed" you would get: sub1.domain.com

So you type: ray.domain.com :: and the code sees sub1.domain.com
Hmm... I don't know how to do that. Maybe .htaccess redirect?  It would be a research project for me.  BTW, this script never finds CNAME information on my server unless I use www. in the URL.  Then it finds some information in the return value.  You can try it here:
https://www.iconoun.com/demo/temp_starquest321.php
<?php // demo/temp_starquest321.php
/**
 * https://www.experts-exchange.com/questions/29022245/Fetch-CNAME-record-of-domain-from-PHP-Code.html
 *
 * http://php.net/manual/en/function.dns-get-record.php
 */
error_reporting(E_ALL);
echo '<pre>';


$hostname = $_SERVER['HTTP_HOST'];
$authns = [];
$addtl = [];
$info = dns_get_record( $hostname, DNS_ANY, $authns, $addtl );

var_dump('DNS_ANY', $hostname, $info, $authns, $addtl);


$authns = [];
$addtl = [];
$info = dns_get_record( $hostname, DNS_ALL, $authns, $addtl );

var_dump('DNS_ALL', $hostname, $info, $authns, $addtl);


$authns = [];
$addtl = [];
$info = dns_get_record( $hostname, DNS_CNAME, $authns, $addtl );

var_dump('DNS_CNAME', $hostname, $info, $authns, $addtl);

Open in new window

This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.