Summary of PHP methods for obtaining domain names, php method for obtaining domain name _ PHP Tutorial

Source: Internet
Author: User
Summary of PHP methods for obtaining domain names, and summary of php methods for obtaining domain names. Summary of PHP methods for obtaining domain names; summary of php methods for obtaining domain names this article provides an example of PHP methods for obtaining domain names. Share it with you for your reference. The specific implementation method is as follows: Summary of methods for obtaining domain names using PHP, Summary of methods for obtaining domain names using php

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)

The 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)

The 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)

The 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)

The 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

$ 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.

Examples in this article summarize how PHP can obtain domain names. Share it with you for your reference. The specific implementation method is as follows: method...

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.