PHP Get root domain method Summary, PHP get domain name Summary _php Tutorial

Source: Internet
Author: User

PHP Get root domain method Summary, PHP get domain name summary


This article summarizes the PHP get root domain name method, share to everyone for your reference. The implementation method is as follows:

If you simply get the current access to your page's domain name, we only need to use the PHP function http_host can be done, if it is to extract the URL root domain is the need for regular, the following to see a few specific instances.

It is very simple to get the current domain name:

Copy the Code code as follows: <?php
Get the current domain name:
echo $_server[' server_name '];
Get the source URL, that is, click to the previous page URL of this page
echo $_server["Http_referer"];
$_server[' Request_uri '];//gets the suffix of the current domain name
$_server[' Http_host '];//get the current domain name
DirName (__file__);//Gets the physical path of the current file
DirName (__file__). " /.. /";//Gets the upper-level physical path of the current file
?>

Example 1

Copy the Code code as follows: function Geturlroot ($url) {
#添加头部和尾巴
$url = $url. "/";
#判断域名
Preg_match ("/(\w*): \/\/)? \w*\.? ([\w|-]*\. (Com.cn|net.cn|gov.cn|org.cn|com|net|cn|org|asia|tel|mobi|me|tv|biz|cc|name|info))
\//", $url, $ohurl);
#判断IP
if ($ohurl [3] = = ") {
Preg_match ("/(\d+\.) {3}\d+) \//", $url, $ohip);
return $ohip [1];
}
return $ohurl [3];
}

Example 2

Copy the Code code as follows:/**
* Get root domain name
* @param type $domain domain name
* @return string returns the root domain name
*/
function Geturltodomain ($domain) {
$re _domain = ";
$domain _postfix_cn_array = Array ("com", "net", "org", "gov", "edu", "com.cn", "cn");
$array _domain = Explode (".", $domain);
$array _num = count ($array _domain)-1;
if ($array _domain[$array _num] = = ' cn ') {
if (In_array ($array _domain[$array _num-1], $domain _postfix_cn_array)) {
$re _domain = $array _domain[$array _num-2]. "." . $array _domain[$array _num-1]. "." . $array _domain[$array _num];
} else {
$re _domain = $array _domain[$array _num-1]. "." . $array _domain[$array _num];
}
} else {
$re _domain = $array _domain[$array _num-1]. "." . $array _domain[$array _num];
}
return $re _domain;
}

I hope this article is helpful to everyone's PHP programming.


PHP section How to extract the root domain name of the regular

([0-9a-za-z-.] +?\.)? [0-9a-za-z-]\. [A-za-z] {2,4}$

How to use PHP to get the IP address of the domain name?

gethostbyname (PHP 3, PHP 4, PHP 5)

GetHostByName--Gets 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;
?>

http://www.bkjia.com/PHPjc/901280.html www.bkjia.com true http://www.bkjia.com/PHPjc/901280.html techarticle php Get root domain name method Summary, PHP get domain name Summary This article summarizes the PHP get root domain name method, share to everyone for your reference. The implementation method is as follows: If you are simply ...

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