我想在PHP中构建一个验证脚本,它将与我的网站提供的API一起使用。这个想法是,用户将进入他们的API应该工作的所有领域,防止竞争对手将代码放在他们的网站上。我只是想验证所输入的域名是有效的格式PHP - 使用wilcards和/或子域验证域
以下域类型应该被允许的:
- xyz.domain.xxxx
- * .domain.xxxx
- 域。 XXXX
我已搜索周围,我发现的代码片段(1,2,3)倾向于降低所有领域为最简单的表格,domain.com
,似乎没有考虑通配符或子域名 - 或似乎与我所寻找的有所不同。
我希望有一天能掌握Reg表达式。
在此先感谢!
UPDATE:解决方案
最后我用的是拒绝TLD的正则表达式 - 如果preg_match
失败,我再运行在域作为一个安全网,将验证TLD的一个DNS检查。这可以防止人们进入垃圾一样MyNameIsFred
$domain = str_replace('http://', '', strtolower($_REQUEST['domain']));
//Not checking TLDs. Instead if the domain fails, we will check DNS records to see if it is a TLD or somehow otherwise exists.
/* $reg = '/^(?!.{254,})(?!.*[^.]{64,})(?!.*\*\*)([a-z0-9*](?>[a-z0-9-*]*[a-z0-9*])?)(?>\.(?1)){0,126}$/iD'; //TLD domains OK */
$reg = '/^(?!.{254,})(?!.*[^.]{64,})(?!.*\*\*)[a-z0-9*](?>[a-z0-9-*]*[a-z0-9*])?(?>\.[a-z0-9*](?>[a-z0-9-*]*[a-z0-9*])?){1,126}$/iD'; //No TLD
if (preg_match($reg, $domain)!=1) {
if (!checkdnsrr($domain.'.', 'A') && !checkdnsrr($domain.'.', 'AAAA'))
//ERROR
}
//SUCCESS
它不太你在找什么,所以不回答这个问题,但它是值得了解:http://php.net/manual/en/filter.filters.validate.php – Spudley 2013-03-27 15:08:46
你是正确的 - 不会真的有帮助,但很高兴知道。谢谢 – Dutchie432 2013-03-27 16:55:04