PHP to obtain the domain name and domain name IP address

Source: Internet
Author: User
The PHP method for obtaining domain names and domain name IP addresses is recently used in a project to obtain domain names and IP addresses. There are roughly the following methods. You can use the built-in function gethostbyname to obtain the IP address of a domain name, for example, echogethostbyname ("www.jbxue.com"). the above method directly outputs the PHP domain name and the IP address of the domain name.
Recently, a project is used to obtain domain names and IP addresses. There are roughly the following methods.

You can use the built-in function gethostbyname to obtain the IP address of a domain name. for example:
echo gethostbyname("www.jbxue.com");

The above will directly output the IP address corresponding to the domain name, of course, the test results are different from each other, because Baidu's server is not only one, and my ip address is 115.239.210.27;
The following is a way to get a domain name, for example, a URL is like this: http://www.jbxue.com/all-the-resources-of-this-blog.html
// The Echo $ _ SERVER ["HTTP_HOST"] will be output to www.jbxue.com//;

When I test locally, the localhost will be output.
The second method to obtain the domain name is to use the parse_url function;
$url ="http://www.jbxue.com/index.php?referer=kakata.com";$arr=parse_url($url);echo "";    print_r($arr);    echo "“;

This code gets an array. when we print this array, you will see it at a glance;
Array
(
[Scheme] => http
[Host] => www.jbxue.com
[Path] =>/index. php
[Query] => referer = jbxue.com
)
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;

The third method for php to obtain domain name information:
We also use the third method to obtain the domain name for this domain name. We use a user-defined function to obtain the domain name.
 $ Url = "http://www.jbxue.com/index.php? Referer = jbxue.com "; get_host ($ url); function get_host ($ url) {// replace http: // $ url = str_replace (" http ://", "", $ url); // get the location where the http: // url is removed/first appears $ position = strpos ($ url ,"/"); // if there is no slash, it indicates that there is no parameter in the url and the url is directly returned. // otherwise, the string if ($ position = false) {echo $ url;} is intercepted ;} else {echo substr ($ url, 0, $ position) ;}}?>

The last method is to use regular expressions. This step is complicated. to implement complex domain name matching, you need to use more complex regular expressions. I just provide a simple idea and 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" matched "; echo" matched ". $ 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.