This article is also available as a TechRepublic download.

PHP integrates so well with Web pages that it’s easy to
forget the language can be used for other things as well — data encryption,
windowing applications and socket programming are just three examples. And
since network programming is one of the areas most developers find potentially
troublesome, it’s worthwhile spending some time browsing PHP’s
built-in network functions to understand the kind of capabilities they offer
and how they might help in your next project.

This listing of 10 of PHP’s most interesting and useful network-related
functions (Table A) provides a
starting point for your journey through PHP’s network
programming capabilities and gives you insight into an area most Web developers
don’t usually enter. Explanations and usage examples are used to indicate when
and how each function may be used.

Table A

Function

Explanation

Example

getmxrr
($hostname, $mxhosts)

This function is used to retrieve the names of MX (mail
exchanger) hosts for a particular host.

Use this
function to identify the mail hosts for a domain, typically as a prelude to
verifying a mailbox on that domain.

Code:
<?php
$hosts = array();
$ret = getmxrr(‘techrepublic.com’, $hosts);
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 retrieve the host name associated
with an IP.

Use this function to
perform a reverse DNS lookup and put a name to an IP address – for example,
the IP addresses recorded in your Web server logs.

Code:
<?php
echo gethostbyaddr(‘216.239.115.148’);
?>

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

gethostbyname($name)

This function does the reverse of gethostbyaddr(), retrieving the IP address
associated with a host name.

Use this function to
perform a standard DNS lookup and get the IP address associated with a host
name – for example, when automatically blacklisting suspicious domains.

Code:
<?php
echo gethostbyname(‘techrepublic.com’);
?>

Output:
216.239.115.148

ip2long($ip)
and
long2ip($long)

These functions convert IP addresses in dotted-quad
notation to long integers, or the other way around.

Use these
functions when you need to represent IP addresses in integer format
(typically for numerical calculations), or vice-versa.

Code:
<?php
echo ip2long(‘216.239.115.148’);
echo long2ip(-655395948);
?>

Output:
-655395948
216.239.115.148

checkdnsrr
($host, $type)

This function checks the DNS for records corresponding to
type $type for host $host and returns Boolean true if
found.

Use this
function to evaluate whether a particular type of DNS record exists for a
host.

Code:
<?php
$ret = checkdnsrr(‘techrepublic.com’, SOA);
if ($ret) {
    echo ‘SOA records exist for host’;
} else {
    echo ‘SOA records do not exist for host’;
}
?>

Output:
SOA records exist for host

dns_get_record
($host, $type)

This function returns the DNS record for host $host. The
optional $type parameter can be used to retrieve only those subsets
matching a specified type.

Use this
function to retrieve detailed DNS records 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] => 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)
and
getprotobynum
($name)

These functions retrieve protocol names and numbers from
the system-wide /etc/protocols file.

Use this
function to retrieve system protocol information, either by name or number.

Code:
<?php
echo getprotobyname(81);
echo getprotobyname(‘icmp’);
?>

Output:
vmtp
1

getservbyname
($service, $protocol)
 

This function is used to retrieve the port number for the
service $service using the protocol $protocol, from the
system-wide /etc/services file.

Use this
function to dynamically obtain information on ports for running system
services.

Code:
<?php
echo getservbyname(‘http’, ‘tcp’);
?>

Output:
80

inet_ntop
($addr)
and
inet_pton($addr)

These functions unpack and pack IP addresses between binary
format and human-readable addresses.

Use this
function to convert between IPv4/IPv6 address strings and binary
representation.

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 logs the message $msg to the system logging device with warning level $level.

Use this
function to issue system-wide errors or alerts.

Code:
<?php
define_syslog_variables();
openlog(‘mylog’, LOG_NDELAY, LOG_LOCAL0);
syslog(LOG_DEBUG, ‘This is a debug message’);
closelog();
?>