Home > Backend Development > PHP Tutorial > Discussion: How to obtain domain name and domain name IP address with PHP

Discussion: How to obtain domain name and domain name IP address with PHP

WBOY
Release: 2016-07-25 08:58:05
Original
1188 people have browsed it
  1. //Global array
  2. echo $_SERVER["HTTP_HOST"];
  3. //The output will be bbs.it-home.org
Copy the code

The local test will output localhost.

Method 2, use parse_url function;

<?php
$url ="http://bbs.it-home.org/index.php?referer=jbxue.com";
$arr=parse_url($url);
echo "<pre class="brush:php;toolbar:false">";
print_r($arr);
echo "
Copy after login
"; ?>

The output is an array, and the result is:

Array ( [scheme] => http [host] => bbs.it-home.org [path] => /index.php [query] => referer=jbxue.com )

Instructions: Scheme corresponds to the protocol, host corresponds to the domain name, path corresponds to the path of the execution file, and query corresponds to the relevant parameters;

Method 3, using a custom function.

<?php
    $url ="http://bbs.it-home.org/index.php?referer=jbxue.com";
    get_host($url);
    function get_host($url){
        //首先替换掉http://
        $url=Str_replace("http://","",$url);
        //获得去掉http://url的/最先出现的位置
        $position=strpos($url,"/");
        //如果没有斜杠则表明url里面没有参数,直接返回url,
        //否则截取字符串
        if($position==false){
            echo $url;
        }else{
            echo substr($url,0,$position);
        }
    }
?>
Copy after login

Method 4, use php regular expressions.

<?php
    header("Content-type:text/html;charset=utf-8");
    $url ="http://bbs.it-home.org/index.php?referer=jbxue.com";
    $pattern="/(http:\/\/)?(.*)\//";
    if(preg_match($pattern,$url,$arr)){
        echo "匹配成功!";
        echo "匹配结果:".$arr[2];
    }
?>
Copy after login
Articles you may be interested in: PHP obtains several global variables of the domain name Detailed explanation of the method of php to implement dns domain name query (picture and text) php example code to get domain name from url How to get the source domain name of the site 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 Wanwang interface to implement domain name query function


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