Home php教程 php手册 经典:10个重要的PHP网络信息函数说明

经典:10个重要的PHP网络信息函数说明

Jun 21, 2016 am 09:00 AM
com gt host nbsp

PHP与网页结合得如此紧密,以致于很容易忘记这种语言还可用来执行其它功能——数据加密、窗口应用与接口编程就是其中三个例子。既然网络编程是大多数开发者最可能遇到麻烦的领域,所以值得花时间来浏览一下PHP的内置网络函数,了解它们提供的功能,以及它如何帮助您完成下一个项目。

下表(表A)中列出了PHP中的10个最有趣、最有用的网络函数,它是您了解PHP网络编程功能的起点,并为您进入多数网络开发者通常并未涉及的领域提供启示。每个函数都有说明与用法实例,为您说明在何时,该怎样应用这些函数。

 函数  说明  实例
 

getmxrr

($hostname,

$mxhosts)

 该函数用来恢复某个特定主机的MX(邮件交换记录)主机名称。

用这个函数来确认某个域的邮件主机,通常以验证那个域上的一个邮箱开始。

 代码:

$hosts = array();

$ret = getmxrr

('techrepublic.com', $hosts);

if ($ret) {

print_r($hosts);

} else {

echo 'MX retrieval failed';

}

?>

 

输出:

Array (

[0] => c10-mail.cnet.com

[1] => c12-mail.cnet.com

)

 gethostbyaddr
($ip)
 该函数用来恢复与一个IP有关的主机名。
用此函数执行一个逆DNS查找,把一个名称给某个IP地址——如记录您的网络服务器日志的IP地址。

 代码:
echo gethostbyaddr
('216.239.115.148');
?>

输出:
c10-sha-redirect-
lb.cnet.com

 gethostbyname
($name)
 该函数与gethostbyaddr()函数互逆,恢复与主机有关的IP地址。
用此函数执行一个标准的DNS查找,获得与IP地址有关的主机名——例如自动将有疑点的域列入黑名单时。

 代码:
echo gethostbyname
('techrepublic.com');
?>

输出:
216.239.115.148

 p2long($ip)

long2ip($long)
 这些函数将由符号连接的四个小于255的数字代表的IP地址转换成整数,或进行逆操作。
在您需要用整数格式表示IP地址(通常用于数字计算),或用IP地址表示数字格式时,应用这些函数。

 代码:
echo ip2long
('216.239.115.148');
echo long2ip(-655395948);
?>

输出:
-655395948
216.239.115.148

 checkdnsrr
($host, $type)
 该函数在DNS中检查与$host主机相符的$type类型记录,如发现,即返回布尔真值。
用此函数检查主机中是否存在某个特定的DNS记录类型。

 代码:
$ret = checkdnsrr
('techrepublic.com', SOA);
if ($ret) {
echo 'SOA records
exist for host';
} else {
echo 'SOA records do
not exist for host';
}
?>

输出:
SOA records exist for host

 dns_get_record
($host, $type)
 该函数返回$host主机的DNS记录。可选的$type参数只可用来恢复那些与一个特定的类型匹配的子集。
用此函数恢复某个特殊主机的详细DNS记录。

 代码:
$data = dns_get_record
('techrepublic.com');
print_r($data);
?>

输出:
Array
(
[0] => Array
(
[host] =>
techrepublic.com
[type] => MX
[pri] => 500
[target] =>
c10-mail.cnet.com
[class] => IN
[ttl] => 10756
)
[1] => Array
(
[host] =>
techrepublic.com
[type] => NS
[target] =>
ns3.cnet.com
[class] => IN
[ttl] => 7885
)
)

 getprotobyname
($num)

getprotobynum
($name)
 这些函数从泛系统/etc/protocols文件中恢复协议名称与号码。
用这些函数根据名称或号码来恢复系统协议信息。


代码:
echo getprotobyname(81);
echo getprotobyname
('icmp');
?>
 

输出:
vmtp
1

 getservbyname
($service,
$protocol)
 该函数利用$protocol协议,从泛系统/etc/services文件中为$service服务恢复端口号码。
用此函数自动获得运行系统服务的端口信息。

 代码:
echo getservbyname('http',
'tcp');
?>

输出:
80

 inet_ntop
($addr)

inet_pton
($addr)
 这些函数在二进制与人可读地址间转换IP地址。
用此函数在Ipv4/Ipv6地址字符串与二进制表达法间进行转换。

 代码:
$packed = inet_pton
('192.168.0.1');
$unpacked = inet_ntop
($packed);
echo $unpacked;
?>

输出:
192.168.0.1

 syslog
($level, $msg)
 这个函数利用警告级别$level将$msg信息记录到系统日志设备中。
用此函数发布泛系统错误或警告。
 代码:
define_syslog_variables();
openlog('mylog',
LOG_NDELAY, LOG_LOCAL0);
syslog(LOG_DEBUG, 'This is
a debug message');
closelog();
?>



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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

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)

Solution: Your organization requires you to change your PIN Solution: Your organization requires you to change your PIN Oct 04, 2023 pm 05:45 PM

The message "Your organization has asked you to change your PIN" will appear on the login screen. This happens when the PIN expiration limit is reached on a computer using organization-based account settings, where they have control over personal devices. However, if you set up Windows using a personal account, the error message should ideally not appear. Although this is not always the case. Most users who encounter errors report using their personal accounts. Why does my organization ask me to change my PIN on Windows 11? It's possible that your account is associated with an organization, and your primary approach should be to verify this. Contacting your domain administrator can help! Additionally, misconfigured local policy settings or incorrect registry keys can cause errors. Right now

How to adjust window border settings on Windows 11: Change color and size How to adjust window border settings on Windows 11: Change color and size Sep 22, 2023 am 11:37 AM

Windows 11 brings fresh and elegant design to the forefront; the modern interface allows you to personalize and change the finest details, such as window borders. In this guide, we'll discuss step-by-step instructions to help you create an environment that reflects your style in the Windows operating system. How to change window border settings? Press + to open the Settings app. WindowsI go to Personalization and click Color Settings. Color Change Window Borders Settings Window 11" Width="643" Height="500" > Find the Show accent color on title bar and window borders option, and toggle the switch next to it. To display accent colors on the Start menu and taskbar To display the theme color on the Start menu and taskbar, turn on Show theme on the Start menu and taskbar

Windows 11 shutdown prompts task host window task host is executing the shutdown task solution Windows 11 shutdown prompts task host window task host is executing the shutdown task solution Feb 12, 2024 pm 12:40 PM

Recently, many Win11 users have reported that when shutting down, they are prompted that the taskhostwindow task host is executing the shutdown task. So what is going on? Users can enter the Desktop folder under the local registry editor, and then select AutoEndTasks in the right window to set it. Let this site carefully introduce to users the solution to this problem when shutting down. Windows 11 shutdown prompts that the taskhostwindow task host is executing the shutdown task. Solution 1. Use the key combination win key + r key, enter "regedit" and press Enter, as shown in the figure below. 2. Search for [HKEY

How to change title bar color on Windows 11? How to change title bar color on Windows 11? Sep 14, 2023 pm 03:33 PM

By default, the title bar color on Windows 11 depends on the dark/light theme you choose. However, you can change it to any color you want. In this guide, we'll discuss step-by-step instructions for three ways to change it and personalize your desktop experience to make it visually appealing. Is it possible to change the title bar color of active and inactive windows? Yes, you can change the title bar color of active windows using the Settings app, or you can change the title bar color of inactive windows using Registry Editor. To learn these steps, go to the next section. How to change title bar color in Windows 11? 1. Using the Settings app press + to open the settings window. WindowsI go to "Personalization" and then

OOBELANGUAGE Error Problems in Windows 11/10 Repair OOBELANGUAGE Error Problems in Windows 11/10 Repair Jul 16, 2023 pm 03:29 PM

Do you see "A problem occurred" along with the "OOBELANGUAGE" statement on the Windows Installer page? The installation of Windows sometimes stops due to such errors. OOBE means out-of-the-box experience. As the error message indicates, this is an issue related to OOBE language selection. There is nothing to worry about, you can solve this problem with nifty registry editing from the OOBE screen itself. Quick Fix – 1. Click the “Retry” button at the bottom of the OOBE app. This will continue the process without further hiccups. 2. Use the power button to force shut down the system. After the system restarts, OOBE should continue. 3. Disconnect the system from the Internet. Complete all aspects of OOBE in offline mode

How to enable or disable taskbar thumbnail previews on Windows 11 How to enable or disable taskbar thumbnail previews on Windows 11 Sep 15, 2023 pm 03:57 PM

Taskbar thumbnails can be fun, but they can also be distracting or annoying. Considering how often you hover over this area, you may have inadvertently closed important windows a few times. Another disadvantage is that it uses more system resources, so if you've been looking for a way to be more resource efficient, we'll show you how to disable it. However, if your hardware specs can handle it and you like the preview, you can enable it. How to enable taskbar thumbnail preview in Windows 11? 1. Using the Settings app tap the key and click Settings. Windows click System and select About. Click Advanced system settings. Navigate to the Advanced tab and select Settings under Performance. Select "Visual Effects"

How to check if aggregator host.exe is safe in Windows 11 How to check if aggregator host.exe is safe in Windows 11 Apr 13, 2023 pm 04:22 PM

When we launch Task Manager to terminate a task or stop an application, we usually find a large number of processes running. This is completely normal. However, sometimes we see programs that are using system resources that we are completely unaware of. One of these processes is the aggregator host.exe, which has been causing some confusion among users recently. Some of these processes may be legitimate Windows requirements, but others may be malicious programs running in the background and causing problems without the user's knowledge or consent. After we've seen the five ways you can launch Task Manager in Windows 11, we'll show you how to check if aggregator host.exe is safe or a virus. keep up

Display scaling guide on Windows 11 Display scaling guide on Windows 11 Sep 19, 2023 pm 06:45 PM

We all have different preferences when it comes to display scaling on Windows 11. Some people like big icons, some like small icons. However, we all agree that having the right scaling is important. Poor font scaling or over-scaling of images can be a real productivity killer when working, so you need to know how to customize it to get the most out of your system's capabilities. Advantages of Custom Zoom: This is a useful feature for people who have difficulty reading text on the screen. It helps you see more on the screen at one time. You can create custom extension profiles that apply only to certain monitors and applications. Can help improve the performance of low-end hardware. It gives you more control over what's on your screen. How to use Windows 11

See all articles