โค้ดต่อไปนี้เป็นแนวทาง สามารถนำไปประยุกต์ใช้งาน ในการค้นหา โดเมน ว่าว่างหรือไม่ ได้ตามต้องการ
โค้ดส่วนของการตรวจสอบโดเมน
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | <?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 } ?> |
ตัวอย่างการนำไปใช้
1 2 3 4 5 6 7 8 9 | <?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 ของโดเมน ?> |