PHP NETWORKING

by mani 2010-09-29 11:27:16

1.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.

<?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
)

2.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.

<?php
echo gethostbyaddr('216.239.115.148');
?>

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


3.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.

<?php
echo gethostbyname('techrepublic.com');
?>

Output:
216.239.115.148

4.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.

<?php
echo ip2long('216.239.115.148');
echo long2ip(-655395948 );
?>

Output:
-655395948
216.239.115.148


5.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.

<?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


6.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.

<?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
)

)


7.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.

<?php
echo getprotobyname(81);
echo getprotobyname('icmp');
?>

Output:
vmtp
1

8.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.

<?php
echo getservbyname('http', 'tcp');
?>

Output:
80


9.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.

<?php
$packed = inet_pton('192.168.0.1');
$unpacked = inet_ntop($packed);
echo $unpacked;
?>

Output:
192.168.0.1

10.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.

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

Tagged in:

1261
like
0
dislike
0
mail
flag

You must LOGIN to add comments