This article introduces a code that uses PHP to query dns domain name information. Friends in need can refer to it.
In PHP domain name related operations, two functions are generally used, they are: gethostbyname() and gethostbyaddr(). 1. IP address query The gethostbyname() function can be used to find the IP address of a given domain name. gethostbyname() returns a pointer to a hostent structure containing host name and address information corresponding to the given hostname. string gethostbyname(string hostname) Parameters: host name, http:// is not required, for example: bbs.it-home.org. This function can return the IP address (IP Address) of a certain machine name (Domain Name). If the execution fails, the original machine name is returned. Second, domain name query gethostbyaddr returns the machine name. Syntax: string gethostbyaddr(string ip_address); Return value: string Function type: Network system Content description This function can return the machine name (Domain Name) of a certain IP address. If the execution fails, the original IP address is returned. Three, examples The following code implements domain name IP address and domain name query, which is a good example. <?php // dnslookupip.php - DNS/IP Address Lookup // Page title $pagetitle = 'PHP域名查询程序'; // Prompts $prompt_ip = 'IP Address'; $prompt_dn = 'Domain Name'; // Messages $lookupfail = '<span style="color:red;">* lookup failed *</span>'; // Get submitted host/domain name $dn = isset($_REQUEST['dn']) ? $_REQUEST['dn'] : ''; if ($dn == $prompt_dn) { $dn = ''; } // Get submitted ip address $ip = isset($_REQUEST['ip']) ? $_REQUEST['ip'] : ''; if ($ip == $prompt_ip) { $ip = ''; } // Check if host/domain name specified if ($dn) { // Domain name specified; IP address lookup request if ($dn == 'me') { $ip = $_SERVER['REMOTE_ADDR']; } else { // Lookup IP address by domain/host name $ip = @gethostbyname($dn); if ($ip == $dn) { $ip = $lookupfail; } } $message = $prompt_dn.' '.$dn.' :: '.$prompt_ip.' '.$ip; } // Check if IP address specified else if ($ip) { // Lookup domain/host name by IP address $dn = @gethostbyaddr($ip); // Check lookup if ($dn == $ip) { // IP address invalid or domain name not found $dn = $lookupfail; } $message = $prompt_ip.' '.$ip.' :: '.$prompt_dn.' '.$dn; } else { $message = $prompt_dn.' '.$_SERVER['HTTP_HOST'] .' :: '.$prompt_ip.' '.$_SERVER['SERVER_ADDR']; } ?> <html> <head> <title><?php echo $pagetitle;?></title> </head> <body style="background-color:#cfcfcf;font-family:Arial;sans-serif;font-size:12px;"> <h3 style="font-size:13px;margin-bottom:0px;"><?php echo $pagetitle;?></h3> <hr /> <p style="margin-top:4px;margin-bottom:4px;font-size:12px;"> <?php echo $message;?> </p> <form style="margin-top:4px;margin-bottom:4px;"> <input style="font-size:12px;" type="text" name="dn" value="<?php echo $prompt_dn;?>" size="30" /> <input style="font-size:12px;" type="text" name="ip" value="<?php echo $prompt_ip;?>" size="15" /> <input style="font-size:12px;" type="submit" value="Lookup" /> </form> <hr /> <p style="margin:0px;font-size:9px;color:#666666;"> Copyright © 2003-<?php echo date('Y');?> by 程序员之家,欢迎您! </p> </body> </html> Copy after login The above code, picture: Articles you may be interested in: PHP obtains several global variables of the domain name php example code to get domain name from url How to get the source domain name of the site with php Discuss: How to obtain domain name and domain name IP address with PHP An example of using php to obtain the domain name code in the URL PHP regular matching to obtain the code of the domain name in the URL PHP gets the code of the current website and domain name php regular expression matching domain name in URL PHP calls the Wanwang interface to realize the domain name query function |