[英]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. 如何检查访问该页面的URL是原始URL还是CNAME记录。
For example, I have a site domain.com
. 例如,我有一个站点domain.com
。 Users can setup a CNAME where they link www.otherdomain.com
to our site domain.com
. 用户可以设置CNAME,将www.otherdomain.com
链接到我们的网站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
)? 从我们的服务器角度来看,如何检测访问该页面的URL是实际的( domain.com
)还是CNAME的( www.otherdomain.com
)?
I think you have to query a DNS to know it, since this information does not usually come in request headers. 我认为您必须查询DNS以了解它,因为此信息通常不会出现在请求标头中。
Take a look at the dns_get_record and checkdnsrr functions in PHP documentation. 查看PHP文档中的dns_get_record和checkdnsrr函数。
您可以使用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. 如果您正在检查URL,则可以在PHP中使用$_SERVER['HTTP_HOST']
。 That's the requested host from the URL (eg. http://otherdomain.com/blah.php would make the server var be otherdomain.com
). 这是URL中请求的主机(例如http://otherdomain.com/blah.php会使服务器var成为otherdomain.com
)。 $_SERVER['SERVER_NAME']
is the configured servername in the httpd.conf. $_SERVER['SERVER_NAME']
是httpd.conf中配置的服务器名称。
If HTTP_HOST != SERVER_NAME, the remote user is almost certainly using an alias of some sort to access the site. 如果HTTP_HOST!= SERVER_NAME,则远程用户几乎肯定会使用某种别名来访问该站点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.