PHP 域名與子域名判斷



/**

* nimo

* 判斷域名是否在域名陣列裡面

* @.表示域名本身,*.表示所有子域名

* @param string $domain 被判斷的域名

* @param array $domain_list 域名陣列

* @param string $match_sub_domain 符合域名的的字域名

* @param string $match_domain 符合域名的母域名

* @return boolean true(在)/false(不在)

*/

function in_domain($domain, $domain_list, &$match_sub_domain='', &$match_domain='') {

foreach ($domain_list as $judge_domain) {

if (preg_match("#^(?:\*\.)(.*)$#", $judge_domain, $match)) {

$tmp_domain = str_replace(".","\.",$match[1]);

if (preg_match("#^(.+)\.({$tmp_domain})$#", $domain, $match2)) {

$match_sub_domain = $match2[1];

$match_domain = $match2[2];

return true;

}

} else if (preg_match("#^(?:\@\.)(.*)$#", $judge_domain, $match)) {

$tmp_domain = str_replace(".","\.",$match[1]);

if (preg_match("#^({$tmp_domain})$#", $domain, $match2)) {

$match_sub_domain = '';

$match_domain = $match2[1];

return true;

}

} else if ($judge_domain == $domain) {

$match_sub_domain = '';

$match_domain = $domain;

return true;

}

}

return false;

}



這裡是一個域名轉跳用的範例



$tmp_domain = $_SERVER['HTTP_HOST'];

$to_https_array = array('@.test.com, '*.test.com');

if ($web_protocol == 'http' AND in_domain($tmp_domain, $to_https_array, $match_sub, $match_domain)) {



// Example: *.test.com 包含其子域名轉到 *.test.com.tw

if ($match_domain == 'test.com') {

$redirect_domain = ($match_sub == '' ? 'test.com' : "{$match_sub}.test.com.tw");

header("Location: http://{$redirect_domain}{$_SERVER['REQUEST_URI']}");

die('redirect~~');

}

}






留言

這個網誌中的熱門文章

成人剪舌繫帶聽過嗎?我剪了!!

Scp - ssh 的遠端檔案傳輸指令

睡覺使你更有效率