Home php教程 php手册 PHP获取主机名、域名函数

PHP获取主机名、域名函数

Jun 06, 2016 pm 07:42 PM
php several function Can domain name Obtain

今天看到几个函数,可以根据ip地址 / 域名 获取对应的 域名 / ip 的函数。记录于此。 gethostbynamel($hostname) // 根据 域名 获取 IP,返回一个数组,比如baidu。com,就返回2个ip 说明array gethostbynamel ( string $hostname )Returns a list of IPv4 a

今天看到几个函数,可以根据ip地址  /  域名 获取对应的 域名 / ip 的函数。记录于此。

gethostbynamel($hostname) // 根据域名获取IP,返回一个数组,比如baidu。com,就返回2个ip

说明
array gethostbynamel ( string $hostname )
Returns a list of IPv4 addresses to which the Internet host specified by hostname resolves. 

参数
hostname The host name. 

返回值
Returns an array of IPv4 addresses or FALSE if hostname could not be resolved. 
Copy after login

 gethostbyname($hostname)  // 根据域名获取IP,只返回第一个IP

gethostbyname — Get the IPv4 address corresponding to a given Internet host name 

说明
string gethostbyname ( string $hostname )
Returns the IPv4 address of the Internet host specified by hostname. 

参数
hostname The host name. 

返回值
Returns the IPv4 address or a string containing the unmodified hostname on failure. 
Copy after login

 gethostbyaddr($ip)  // 根据IP 获取域名

gethostbyaddr — Get the Internet host name corresponding to a given IP address 

说明
string gethostbyaddr ( string $ip_address )
Returns the host name of the Internet host specified by ip_address. 

参数
ip_address 
The host IP address. 

返回值
Returns the host name on success, the unmodified ip_address on failure, or FALSE on malformed input. 
Copy after login

 例子:

<span>$hostname</span> = 'www.baidu.com'<span>;
</span><span>$addr</span> = '127.0.0.1'<span>;

</span><span>print_r</span>(<span>gethostbyname</span>(<span>$hostname</span><span>));
</span><span>echo</span> '<br>'<span>;

</span><span>print_r</span>(<span>gethostbyaddr</span>(<span>$addr</span><span>));
</span><span>echo</span> '<br>'<span>;

</span><span>print_r</span>(<span>gethostbynamel</span>(<span>$hostname</span>));
Copy after login

输出

220.181.111.148
idc-wang
Array ( [0] =&gt; 220.181.111.148 [1] =&gt; 220.181.112.143 ) 
Copy after login
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles