简体   繁体   中英

How to check if a domain name is a CNAME in PHP

How do I check if the URL accessing the page is the original URL or if it's a CNAME record.

For example, I have a site domain.com . Users can setup a CNAME where they link www.otherdomain.com to our site domain.com .

From our server point of view, how can I detect if the URL accessing the page is the actual one ( domain.com ) or the CNAME one ( www.otherdomain.com )?

I think you have to query a DNS to know it, since this information does not usually come in request headers.

Take a look at the dns_get_record and checkdnsrr functions in PHP documentation.

您可以使用PHPDNS类库来查找给定URL的DNS记录类型。

$answer->results[x]->typeid //Textual record type ID (A, MX, CNAME etc)

If you're checking on URLs, you can use $_SERVER['HTTP_HOST'] in PHP. That's the requested host from the URL (eg. http://otherdomain.com/blah.php would make the server var be otherdomain.com ). $_SERVER['SERVER_NAME'] is the configured servername in the httpd.conf.

If HTTP_HOST != SERVER_NAME, the remote user is almost certainly using an alias of some sort to access the site.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM