Home >Backend Development >PHP Tutorial >Summary of how to obtain domain name in PHP, summary of how to obtain domain name in PHP_PHP tutorial
The example in this article summarizes the method of obtaining domain name in PHP. Share it with everyone for your reference. The specific implementation method is as follows:
Method 1 (using system variables)
Method 2 (use built-in function)
Method three (write your own function)
Method 4 (using regular rules)
I hope this article will be helpful to everyone’s PHP programming design.
gethostbyname (PHP 3, PHP 4, PHP 5)
gethostbyname -- Get the IP address of the specified machine name
Function format description:
string gethostbyname ( string hostname )
Return the IP address of hostname
Example 1. A simple gethostbyname() example
$ip = gethostbyname('www.example.com' );
echo $ip;
?>
$url = $_SERVER["HTTP_REFERER"]; //Get the complete source URL
$str = str_replace("http://","",$url); //Remove http://
$strdomain = explode("/",$str); // Separate into arrays with "/"
$domain = $strdomain[0]; //Take the first "/" The previous characters
are accurate using the above method. If you use the function that comes with PHP, it will be wrong. For example:
$_SERVER['SERVER_NAME'] This function obtains the server domain name