Summary of PHP methods for obtaining domain names, and summary of php methods for obtaining domain names

Source: Internet
Author: User

Summary of PHP methods for obtaining domain names, and summary of php methods for obtaining domain names

This example summarizes the PHP method for obtaining domain names. Share it with you for your reference. The specific implementation method is as follows:

Method 1 (using system variables)
Copy codeThe Code is as follows: // disadvantage: Do not use the passed address or host that does not support system variables
Echo $ _ SERVER ['HTTP _ host'];

Method 2 (using built-in functions)
Copy codeThe Code is as follows: $ url = 'HTTP: // www.bkjia.com/index.php? Referer = jb51.net ';
$ Arr_url = parse_url ($ url );
Echo $ arr_url ['host'];

Method 3 (Write Functions by yourself)
Copy codeThe Code is as follows: function getdomain ($ url)
{
$ Url = str_replace ('HTTP: // ', ", $ url); // remove the http prefix
$ Pos = strpos ($ url ,'/');
If ($ pos = false)
{
Return $ url;
} Else
{
Return substr ($ url, 0, $ pos );
}
}
Echo getdomain ($ url );

Method 4 (using regular expressions)
Copy codeThe Code is as follows: preg_match ("/^ (http ://)? ([^/] +)/I ", $ url, $ arr_domain );
Echo $ arr_domain [2];

I hope this article will help you with PHP programming.


How can I use php to obtain the IP address corresponding to the 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)

Returns the IP address of the hostname.

Example 1. A simple gethostbyname () example

<? Php
$ Ip = gethostbyname ('www .example.com ');

Echo $ ip;
?>

PHP retrieves the origin Domain Name

$ Url = $ _ SERVER ["HTTP_REFERER"]; // obtain the complete URL

$ Str = str_replace ("http: //", "", $ url); // remove http ://
$ Strdomain = explode ("/", $ str); // split it into an array "/"
$ Domain = $ strdomain [0]; // take the first character before "/"

The above method is correct. If you use the built-in function of PHP, it is incorrect, such:
$ _ SERVER ['server _ name'] This function obtains the SERVER domain NAME.

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.