Summary of how to obtain domain name in PHP, summary of how to obtain domain name in PHP_PHP tutorial

WBOY
Release: 2016-07-13 10:15:08
Original
1064 people have browsed it

A summary of how to obtain a domain name in PHP, a summary of how to obtain a domain name in PHP

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) 

Copy the code The code is as follows:
//Disadvantages: Not using the passed address and the host that does not support system variables
echo $_SERVER['HTTP_HOST'];
 

Method 2 (use built-in function) 

Copy code The code is as follows:
$url = 'http://www.bkjia.com/index.php?referer=jb51.net' ; 
$arr_url = parse_url($url); 
echo $arr_url['host'];

Method three (write your own function) 

Copy code The code is as follows:
function getdomain($url) 
{  
$url = str_replace('http://',",$url); //If there is an http prefix, remove it   
$pos = strpos($url,'/');
if($pos === false) 
  {  
Return $url;
  }else 
  {  
Return substr($url, 0, $pos);
  }  
} 
echo getdomain($url);

Method 4 (using regular rules)

Copy code The code is as follows:
preg_match("/^(http://)?([^/]+)/i", $ url, $arr_domain); 
echo $arr_domain[2];

I hope this article will be helpful to everyone’s PHP programming design.

How to use php to get the IP corresponding to a 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 )

Return the IP address of hostname

Example 1. A simple gethostbyname() example

$ip = gethostbyname('www.example.com' );

echo $ip;
?>

PHP gets the source domain name

$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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/906676.htmlTechArticleA summary of how to obtain a domain name in PHP, a summary of how to obtain a domain name in PHP. This example summarizes the method of obtaining a domain name in PHP. Share it with everyone for your reference. The specific implementation method is as follows: Method...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template