โค้ดต่อไปนี้เป็นแนวทาง สามารถนำไปประยุกต์ใช้งาน ในการค้นหา โดเมน ว่าว่างหรือไม่ ได้ตามต้องการ
โค้ดส่วนของการตรวจสอบโดเมน
<?php
$port=43;
$arr_thaidomain_ext=array("in.th","co.th","or.th","ac.th","go.th","mi.th","net.th");
$arr_domain_ext=array(
"com" =>"whois.internic.net",
"net" =>"whois.internic.net",
"org" =>"whois.publicinterestregistry.net",
"info" =>"whois.afilias.net",
"biz" =>"whois.whois.biz",
"tv" =>"whois.nic.tv"
);
$arr_match_found=array(
"com" =>"No match",
"net" =>"No match",
"org" =>"NOT FOUND",
"info" =>"NOT FOUND",
"biz" =>"Not found",
"tv" =>"No match"
);
function checkDomain($ext,$domain){
global $arr_thaidomain_ext,$arr_domain_ext,$arr_match_found,$port;
if(empty($domain)){
$errormsg = "กรุณาระบุ ชื่อโดเมน";
return $errormsg;
}elseif(preg_match("/(^-|-$)|([-]{2})/",$domain)) {
$errormsg = "ชื่่อโดเมน $domain จะต้องไม่ขึ้นต้น หรือลงท้ายด้วย hyphen (ขีดกลาง) หรือมี hyphen (ขีดกลาง) 2 ตัว";
return $errormsg;
}elseif(!preg_match("/^([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9]))/",$domain) || !strlen($domain)){
$errormsg = "ชื่่อโดเมน ($domain) ชื่อโดเมน ต้องเป็นตัวอักษร หรือตัวเลข หรือ hyphen (ขีดกลาง) เท่านั้น";
return $errormsg;
}
// echo $errormsg;
if(!$errormsg){ // begin if no error
if(in_array($ext,$arr_thaidomain_ext)){
$arr_domain_ext[$ext]="whois.thnic.net";
$arr_match_found[$ext]="No entries";
}
$ndomain=trim($domain).".".$ext;
$sock=@fsockopen($arr_domain_ext[$ext],$port);
if(!$sock){
echo "Unable to connect to $qserver:$port";
}else{
fputs($sock,"$ndomainrn");
while(!feof($sock)) {
$reply .= fgets($sock, 1024);
}
if(preg_match("/".$arr_match_found[$ext]."/i",$reply)){
$validDomain[0]=1;
$validDomain[1]=iconv("tis-620","utf-8",nl2br($reply));
return $validDomain;
}else{
$validDomain[0]=0;
$validDomain[1]=iconv("tis-620","utf-8",nl2br($reply));
return $validDomain;
}
} // end no sock
} // end if no error
}
?>
ตัวอย่างการนำไปใช้
<?php
// ต้องการหาว่า โดเมนชื่อ ninenik.com ว่างหรือไม่
// ชื่อโดเมน และนามสกุล dot com ส่งเป็นตัวแปรเข้าไปได้
list($returnValid,$returnDetail)=checkDomain("com","ninenik");
echo $returnValid."<br>"; // 1 =ว่าง , 0 = ไม่ว่าง
echo $returnDetail."<br>";
// $returnValid = 1 $returnDetail = แสดงผลการค้นหาโดเมน
// $returnValid = 0 $returnDetail = แสดง whois ของโดเมน
?>