Home > php教程 > php手册 > php中获取主机名、协议及IP地址的方法

php中获取主机名、协议及IP地址的方法

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-06 20:17:22
Original
2219 people have browsed it

这篇文章主要介绍了php中获取主机名、协议及IP地址的方法,其中涉及gethostbyaddr、gethostbynamel、getprotobyname、getprotobynumber等函数及$_SERVER变量的应

本文实例讲述了php中获取主机名、协议及IP地址的方法。分享给大家供大家参考。

具体实现代码如下:

复制代码 代码如下:

$hostname=gethostbyaddr($_SERVER['remote_addr']);    //获取主机名
echo $hostname;            //输出结果
//
$hosts=gethostbynamel('localhost');       //获取ip地址列表
print_r($hosts);           //输出数组
//
$protocol='tcp';        //定义协议名称
$get_prot=getprotobyname($protocol);   //返回协议号
if($get_prot==-1)       //如果找不到
{
  echo 'invalid protocol';      //输出错误信息
}
else
{
  echo 'protocol #'.$get_prot;     //输出相应的协议号
}
//
$protocol_num='6';       //定义协议号
$get_prot=getprotobynumber($protocol_num);  //返回协议名称
if($get_prot==-1)       //如果找不到
{
  echo 'invalid protocol';      //输出错误信息
}
else
{
  echo 'protocol #'.$get_prot;     //输出相应的协议名称
}

希望本文所述对大家的php程序设计有所帮助。

Related labels:
source:php.cn
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template