PHP get domain name and domain name IP steps

Source: Internet
Author: User
PHP get domain name and domain name IP method
Recently in a project to use to obtain the domain name and IP functions, roughly the following methods.

The method of acquiring the domain name IP can be obtained using the built-in function gethostbyname, for example:
Echo gethostbyname ("www.jbxue.com");

The above will directly output the IP address of the domain name, of course, the results of various local tests are not the same, because Baidu's server is not only one, my side of the IP is 115.239.210.27;
Here's how to get a domain name, such as a URL: http://www.jbxue.com/all-the-resources-of-this-blog.html
The www.jbxue.com//global array will be output echo $_server["Http_host"];

I will output localhost when I test locally
The second method of acquiring a domain name is using: Parse_url function;
$url = "http://www.jbxue.com/index.php?referer=kakata.com"; $arr =parse_url ($url); echo "";    Print_r ($arr);    echo "";

This piece of code gets an array, and when we print this array, you will be at a glance;
Array
(
[Scheme] = http
[Host] = www.jbxue.com
[Path] =/index.php
[Query] = referer=jbxue.com
)
Scheme corresponds to the protocol, the host corresponds to the domain name, path corresponds to the path of the execution file, and query corresponds to the relevant parameters;

PHP Third Way to get domain name information:
This is also the domain name we use the third method to obtain the domain name, we take the custom function to obtain.
 
  $url = "http://www.jbxue.com/index.php?referer=jbxue.com"; Get_host ($url); function Get_host ($url) {//replace http://$ First Url=str_replace ("http://", "", $url);//get rid of http://url/first occurrence position $position=strpos ($url, "/");//If there is no slash, there is no parameter in the URL. Directly returns url,//otherwise intercepts the string if ($position ==false) {echo $url;} Else{echo substr ($url, 0, $position);}}? >

The last method is to use the regular, this step is more complex, to achieve complex domain names to get more complex regular, I just simple to provide ideas, we try more.
 
  Header ("Content-type:text/html;charset=utf-8"); $url = "http://www.jbxue.com/index.php?referer=jbxue.com"; $ Pattern= "/(http:\/\/)? (. *) \//", if (Preg_match ($pattern, $url, $arr)) {echo" matches successfully ", echo" matches ". $arr [2];}? >
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.