Summary of PHP methods for obtaining domain names, and summary of php methods for obtaining domain names
This example summarizes the PHP method for obtaining domain names. Share it with you for your reference. The specific implementation method is as follows:
Method 1 (using system variables)
Copy codeThe Code is as follows: // disadvantage: Do not use the passed address or host that does not support system variables
Echo $ _ SERVER ['HTTP _ host'];
Method 2 (using built-in functions)
Copy codeThe Code is as follows: $ url = 'HTTP: // www.bkjia.com/index.php? Referer = jb51.net ';
$ Arr_url = parse_url ($ url );
Echo $ arr_url ['host'];
Method 3 (Write Functions by yourself)
Copy codeThe Code is as follows: function getdomain ($ url)
{
$ Url = str_replace ('HTTP: // ', ", $ url); // remove the http prefix
$ Pos = strpos ($ url ,'/');
If ($ pos = false)
{
Return $ url;
} Else
{
Return substr ($ url, 0, $ pos );
}
}
Echo getdomain ($ url );
Method 4 (using regular expressions)
Copy codeThe Code is as follows: preg_match ("/^ (http ://)? ([^/] +)/I ", $ url, $ arr_domain );
Echo $ arr_domain [2];
I hope this article will help you with PHP programming.
How can I use php to obtain the IP address corresponding to the domain name?
Gethostbyname (PHP 3, PHP 4, PHP 5)
Gethostbyname -- get the IP address of the specified Machine name
Function Format description:
String gethostbyname (string hostname)
Returns the IP address of the hostname.
Example 1. A simple gethostbyname () example
<? Php
$ Ip = gethostbyname ('www .example.com ');
Echo $ ip;
?>
PHP retrieves the origin Domain Name
$ Url = $ _ SERVER ["HTTP_REFERER"]; // obtain the complete URL
$ Str = str_replace ("http: //", "", $ url); // remove http ://
$ Strdomain = explode ("/", $ str); // split it into an array "/"
$ Domain = $ strdomain [0]; // take the first character before "/"
The above method is correct. If you use the built-in function of PHP, it is incorrect, such:
$ _ SERVER ['server _ name'] This function obtains the SERVER domain NAME.