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 Use this |
Code: <?php $hosts = array(); $ret = getmxrr(‘techrepublic.com’, $hosts); if ($ret) { print_r($hosts); } else { echo ‘MX retrieval failed’; } ?> Output: |
gethostbyaddr($ip) |
This function is used to retrieve the host name associated Use this function to |
Code: <?php echo gethostbyaddr(‘216.239.115.148’); ?> Output: |
gethostbyname($name) |
This function does the reverse of gethostbyaddr(), retrieving the IP address Use this function to |
Code: <?php echo gethostbyname(‘techrepublic.com’); ?> Output: |
ip2long($ip) and long2ip($long) |
These functions convert IP addresses in dotted-quad Use these |
Code: <?php echo ip2long(‘216.239.115.148’); echo long2ip(-655395948); ?> Output: |
checkdnsrr ($host, $type) |
This function checks the DNS for records corresponding to Use this |
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: |
dns_get_record ($host, $type) |
This function returns the DNS record for host $host. The Use this |
Code: <?php $data = dns_get_record(‘techrepublic.com’); print_r($data); ?> Output: [1] => Array ) |
getprotobyname ($num) and getprotobynum ($name) |
These functions retrieve protocol names and numbers from Use this |
Code: <?php echo getprotobyname(81); echo getprotobyname(‘icmp’); ?> Output: |
getservbyname ($service, $protocol) |
This function is used to retrieve the port number for the Use this |
Code: <?php echo getservbyname(‘http’, ‘tcp’); ?> Output: |
inet_ntop ($addr) and inet_pton($addr) |
These functions unpack and pack IP addresses between binary Use this |
Code: <?php $packed = inet_pton(‘192.168.0.1’); $unpacked = inet_ntop($packed); echo $unpacked; ?> Output: |
syslog ($level, $msg) |
This function logs the message $msg to the system logging device with warning level $level. Use this |
Code: <?php define_syslog_variables(); openlog(‘mylog’, LOG_NDELAY, LOG_LOCAL0); syslog(LOG_DEBUG, ‘This is a debug message’); closelog(); ?> |