Classic: 10 Important PHP Network Information function description

Source: Internet
Author: User
Tags format integer log syslog system log domain mail exchange port number

PHP is so tightly integrated with Web pages that it's easy to forget that the language can also be used to perform other functions--Data encryption, window application and interface programming are three examples. Since network programming is the most likely area for most developers to get into trouble, it's worth taking the time to browse through PHP's built-in network functions, learn what they provide, and how it can help you with your next project.

The 10 most interesting and useful network functions in PHP are listed in the following table (table A), which is the starting point for you to learn about the PHP network programming features and provides insights into areas where most web developers do not usually cover. Each function has a description and an instance of usage that shows you when and how to apply these functions.

Function Description Instance
 

Getmxrr

($hostname,

$mxhosts)

This function is used to recover the MX (mail exchange record) host name for a particular host.

Use this function to confirm the mail host for a domain, typically to verify that a mailbox on that domain starts.

  code:

<?php

$hosts = Array ();

$ret = GETMXRR

if ($ret) {

print_r ($hosts);

} else {

echo ' MX retrieval failed ';

?>

 

output:

array (

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

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

)

Gethostbyaddr
($IP)
This function is used to restore the host name associated with an IP.
Use this function to perform an inverse DNS lookup, giving a name to an IP address-such as recording the IP address of your network server log.

Code:
<?php
Echo gethostbyaddr
(' 216.239.115.148 ');
?>

Output:
c10-sha-redirect-
Lb.cnet.com

gethostbyname
($name)
This function is inverse with the gethostbyaddr () function and restores the host-related IP address.
Use this function to perform a standard DNS lookup and obtain the hostname associated with the IP address-for example, automatically blacklist a suspect domain.

Code:
<?php
Echo gethostbyname
(' techrepublic.com ');
?>

Output:
216.239.115.148

 p2long ($IP)
and
Long2ip ($long)
  these functions convert an IP address represented by a number less than 255 from a symbolic connection to an integer or reverse operation.
Apply these functions when you need to represent an IP address in an integer format (typically for numeric calculations), or to represent a number format with an IP address.

  code:
<?php
Echo ip2long
(' 216.239.115.148 ');
Echo Long2ip (-655395948);
?

output:
-655395948
216.239.115.148

&NBSP;CHECKDNSRR
($host, $type)
  this function checks DNS for $TYPE type records that match the $host host, and returns a Boolean true value if found. The
uses this function to check whether a particular DNS record type exists in the host.

  code:
<?php
$ret = CHECKDNSRR
(' Techrepublic.com ', SOA);
if ($ret) {
echo ' SOA records
exist for host ';
} else {
echo ' SOA records does
not exist for host ';

?>

output:
SOA records exist for host

 dns_get_record
($host, $type)
  This function returns the DNS record of the $host host. The optional $type parameter can only be used to recover those subsets that match a particular type. The
uses this function to recover a detailed DNS record for a particular host.

  code:
<?php
$data = Dns_get_record
(' techrepublic.com ');
Print_r ($data);
?

output:
Array
(
[0] => Array
(
[host] =>
techrepublic.com
[type] => MX
[pri] =>
[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)
And
Getprotobynum
($name)
These functions restore the protocol name and number from the generic system/etc/protocols file.
Use these functions to recover system protocol information by name or number.


Code:
<?php
Echo Getprotobyname (81);
Echo Getprotobyname
(' ICMP ');
?>

Output:
Vmtp
1

Getservbyname
($service,
$protocol)
This function uses the $protocol protocol to recover the port number for the $service service from the generic system/etc/services file.
Use this function to automatically obtain port information for running system services.

Code:
<?php
echo getservbyname (' http ',
' TCP ');
?>

Output:
80

Inet_ntop
($ADDR)
And
Inet_pton
($ADDR)
These functions convert IP addresses between binary and human-readable addresses.
Use this function to convert between the Ipv4/ipv6 address string and the binary expression method.

Code:
<?php
$packed = Inet_pton
(' 192.168.0.1 ');
$unpacked = Inet_ntop
($packed);
Echo $unpacked;
?>

Output:
192.168.0.1

Syslog
($level, $msg)
This function uses the warning level $level to log $msg information to the system log device.
Use this function to publish a generic system error or warning.
Code:
<?php
Define_syslog_variables ();
Openlog (' MyLog ',
Log_ndelay, log_local0);
Syslog (Log_debug, ' this is
A debug message ');
Closelog ();
?>



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.