Home > php教程 > php手册 > PHP获取域名方法

PHP获取域名方法

WBOY
Release: 2016-06-06 19:56:17
Original
1085 people have browsed it

//方法一(用 系统变量) //缺点不使用传递过来的地址和不支持系统变量的主机 echo $_SERVER['HTTP_HOST']; //方法二(用自带函数) $url = 'http://www.51php.net/index.php?referer=51php.net'; $arr_url = parse_url($url); echo $arr_url['host']; //方法三(

 //方法一(用 系统变量)   

  //缺点不使用传递过来的地址和不支持系统变量的主机   

  echo $_SERVER['HTTP_HOST'];   

  //方法二(用自带函数)   

  $url = 'http://www.51php.net/index.php?referer=51php.net';   

  $arr_url = parse_url($url);   

  echo $arr_url['host'];   

  //方法三( 自己写函数)   

  function getdomain($url)   

  {   

   $url = str_replace('http://','',$url);       //如果有http前缀,则去掉   

   $pos = strpos($url,'/');   

   if($pos === false)   

   {   

   return $url;   

   }else  

   {   

   return substr($url, 0, $pos);   

   }   

  }   

  echo getdomain($url);    

  //方法四(用正则)   

  preg_match("/^(http://)?([^/]+)/i", $url, $arr_domain);   

  echo $arr_domain[2];

 

PHP获取域名方法

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template