Network I/O - PHP

The network I/O functions send information directly over the Internet Protocol, or they fetch information about Internet hosts.

boolean checkdnsrr(string host, string type)
The checkdnsrr function checks DNS records for a host. The type argument defines the type of records for which to search. Valid types are listed. If type is not specified, checkdnsrr checks for MX records. You may wish to read the man page for named, the Internet domain name server daemon.

integer fsockopen(string hostname, integer port, integer eror_number, string error_description, double timeout)
The fsockopen begins a network connection as a file stream, returning a file descriptor suitable for use by fputs, fgets, and other file-stream functions discussed earlier in this chapter. A connection is attempted to the hostname at the given port. The hostname may also be a numerical IP address. The hostname may also be the path to a UNIX domain socket, in which case port should be set to 0. Some operating systems, specifically Windows, don't support UNIX domain sockets.


If an error occurs, FALSE is returned and the optional error_number and error_description arguments are set. They must be passed by reference, which means adding an ampersand (&) prior to the dollar sign. If the error number returned is zero, an error occurred before PHP tried to connect. This may indicate a problem initializing the socket.
The optional timeout argument will set the number of seconds PHP will wait for a connection to be established. You may specify fractions of a second as well, if you wish. The pfsockopen adds persistence to the fsockopen functionality.

//tell browser not to render this
header("Content-type: text/plain");
//try to connect to Web server,
//timeout after 60 seconds
&$error_number, &$error_description,
//set nonblocking mode
set_socket_blocking($fp, FALSE);
// tell server we want root document
fputs($fp, "GET / HTTP/1.0");
fputs($fp, " ");
//print next 4K
print(fgets($fp, 4096));
//close connection
//$connect was false
print("An error occurred<BR> ");
print("Number: $error_number<BR> ");
Core PHP Programming
IT-SC book 204
print("Description: $error_description<BR> ");

string gethostbyaddr(string ip_address)
The gethostbyaddr function returns the name of the host specified by the numerical IP address. If the host cannot be resolved, the address is returned.


string gethostbyname(string hostname)
The gethostbyname function returns the IP address of the host specified by its name. It is possible a domain name resolves to more than one IP address. To get each one, use


array gethostbynamel(string hostname)
The gethostbynamel function returns a list of IP addresses that a given hostname resolves to.

for($index = 0; $index count($hosts); $index++)
print("$hosts[$index] BR> ");

boolean getmxrr(string host, array mxhost, array weight)
The getmxrr function gets mail-exchanger DNS records for a host. Hostnames will be added to the array specified by the mxhost argument. The optional weight array is assigned with the weight for each host. The return value signals whether the operation was successful.

for($index=0; $index count($mxrecord); $index++)
print(" - ");
print("BR> ");

integer getprotobyname(string name)
The getprotobyname function returns the number associated with a protocol.

string getprotobynumber(integer protocol)
The getprotobynumber function returns the name of a protocol given its number.

integer getservbyname(string service, string protocol)
The getservbyname function returns the port used by a service. The protocol argument
must be tcp or udp.

//check which port ftp uses
$port = getservbyname("ftp", "tcp");
print("port $port<BR> ");

string getservbyport(integer service, string protocol)
The getservbyport function returns the name of the service that uses a specified port. The protocol argument must be tcp or udp.

//check which service uses port 25
$service = getservbyport(25, "tcp");
print("$serviceBR> ");

boolean mail(string recipient, string subject, string body, string additional_headers)
The mail function sends email. Under UNIX it runs the sendmail shell command. Under Windows it makes a connection to an SMTP server. The mail is sent to the address specified in the recipient argument. You may specify multiple recipients by separating them with commas. You must also provide a subject and a message body.Optionally, you may provide additional headers in the fourth argument. Each extra header should be separated by a single newline character. If the mail is sent successfully, true is returned. On Windows, Date: and From: headers are added to the message automatically, unless you supply them yourself.

There are a few directives in php.ini for configuring this function. For Windows you can set the name of the SMTP host using the SMTP directive, and you can set the default From: header with the sendmail_from directive. It's valid, of course, to point to an SMTP server on the localhost. For UNIX, you may specify the path to your sendmail executable, which may have an acceptable default compiled in already. You can't set up PHP on UNIX to send mail directly to a remote SMTP host. You can configure sendmail to relay messages to a specific host, but the instructions are outside the scope of this text.

//define who is to receive the mail
//(in this case, root of the localhost)
$mailTo = "root@" . $SERVER_NAME;
//set the subject
$mailSubject = "Testing Mail";
//build body of the message
$mailBody = "This is a test of PHP's mail function. ";
$mailBody .= "It was generated by PHP version ";
$mailBody .= phpversion();
//add a from header
if(mail($mailTo, $mailSubject, $mailBody, $mailHeaders))
print("Mail successfull sent to $mailTo.");
print("Mail could not be sent to $mailTo.");

integer pfsockopen(string hostname, integer port, integererror_number, string error_description, double timeout)
The pfsockopen function operates identically to fsockopen, except that connections are cached. Connections opened with pfsockopen are not closed when a script terminates. They persist with the server process.

boolean set_socket_blocking(integer file_descriptor, booleanmode)
The set_socket_blocking function sets whether a file stream is blocking. In nonblocking mode, calls to functions that get information from the stream will return immediately with whatever data are in the input buffer. Blocking mode forces execution to halt until sufficient data are received.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd Protection Status

PHP Topics