PHP Network Functions

About This Index
This page is the PHP Network Functions index for the tutorial track. Each row links to a full guide with syntax, examples, and FAQs.
Learn how DNS, headers, cookies, IP conversion, sockets, and syslog fit together on /php/network. Use this page when you need a specific function such as checkdnsrr(), header(), or ip2long().
Start with checkdnsrr() and gethostbyname()—then learn header(), setcookie(), and ip2long(), and finish with fsockopen() and openlog().
Alias pages are included so the sidebar list is complete. Prefer the primary name in new code. The PHP 8.4 additions are marked in the tables below.
How to Call a PHP Network Function
Network helpers are global functions—call them directly. Set headers and cookies before any body output; close socket streams when you are done:
$ok = checkdnsrr('example.com', 'A'); // bool
$ip = gethostbyname('localhost'); // string
$num = ip2long('127.0.0.1'); // int|false
header('Content-Type: text/plain; charset=UTF-8');
http_response_code(200);
setcookie('theme', 'dark', ['path' => '/']);
$fp = fsockopen('127.0.0.1', 80, $errno, $errstr, 5.0);
if (is_resource($fp)) {
// socket_set_timeout($fp, 5); // alias of stream_set_timeout()
fclose($fp);
}
openlog('app', LOG_PID, LOG_USER);
syslog(LOG_INFO, 'ready');
closelog(); PHP Network Functions Table
All function guides in this PHP tutorial series (37 tutorials in 8 categories).
DNS & host lookup
9 functionsAsk DNS about a name, resolve host names to IP addresses, reverse-look up an address, and read MX or full DNS records. Includes aliases of the primary DNS helpers.
| Function | Description | Tutorial |
|---|---|---|
checkdnsrr() | Check whether a DNS record type exists for a host. | Try it |
dns_check_record() | Alias of checkdnsrr(): same DNS existence check. | Try it |
dns_get_mx() | Get MX records for a host (same idea as getmxrr()). | Try it |
dns_get_record() | Fetch DNS resource records for a host as arrays. | Try it |
gethostbyaddr() | Get the host name for an IP address. | Try it |
gethostbyname() | Resolve a host name to one IPv4 address. | Try it |
gethostbynamel() | Resolve a host name to a list of IPv4 addresses. | Try it |
gethostname() | Return the host name of this machine. | Try it |
getmxrr() | Alias-style MX lookup; pairs with dns_get_mx(). | Try it |
Protocol & service names
4 functionsMap protocol names and numbers, and map internet service names to ports (and the reverse).
| Function | Description | Tutorial |
|---|---|---|
getprotobyname() | Get the protocol number for a protocol name. | Try it |
getprotobynumber() | Get the protocol name for a protocol number. | Try it |
getservbyname() | Get the port for a service name and protocol. | Try it |
getservbyport() | Get the service name for a port and protocol. | Try it |
HTTP headers & response
8 functionsSend, remove, and inspect response headers, set the status code, and (in PHP 8.4) read headers from the last HTTP wrapper response.
| Function | Description | Tutorial |
|---|---|---|
header() | Send a raw HTTP header to the client. | Try it |
header_register_callback() | Register a callback that runs when headers are sent. | Try it |
header_remove() | Remove a previously set header. | Try it |
headers_list() | List the response headers prepared so far. | Try it |
headers_sent() | Check whether headers have already been sent. | Try it |
http_clear_last_response_headers() | Clear stored headers from the last HTTP wrapper response (PHP 8.4). | Try it |
http_get_last_response_headers() | Get headers from the last HTTP wrapper response (PHP 8.4). | Try it |
http_response_code() | Get or set the HTTP response status code. | Try it |
IP address conversion
5 functionsConvert between dotted IPv4 text and integers, pack and unpack IPv4/IPv6 addresses, and list local network interfaces.
| Function | Description | Tutorial |
|---|---|---|
inet_ntop() | Convert a packed IP address to a human-readable string. | Try it |
inet_pton() | Convert a human-readable IP address to packed binary. | Try it |
ip2long() | Convert an IPv4 address to a long integer. | Try it |
long2ip() | Convert a long integer back to an IPv4 address. | Try it |
net_get_interfaces() | List network interfaces and their addresses. | Try it |
Sockets & streams
5 functionsOpen TCP (or SSL) streams with fsockopen() / pfsockopen(). The socket_* names are aliases of stream helpers for status, blocking mode, and timeout.
| Function | Description | Tutorial |
|---|---|---|
fsockopen() | Open a network socket connection as a stream. | Try it |
pfsockopen() | Open a persistent network socket connection. | Try it |
socket_get_status() | Alias of stream_get_meta_data() for a stream. | Try it |
socket_set_blocking() | Alias of stream_set_blocking() for a stream. | Try it |
socket_set_timeout() | Alias of stream_set_timeout() for a stream. | Try it |
Syslog
3 functionsOpen a connection to the system logger, write a message, and close the connection when you are done.
Request body
1 functionParse a multipart or URL-encoded request body in PHP 8.4 and newer.
| Function | Description | Tutorial |
|---|---|---|
request_parse_body() | Parse the request body into post and files data (PHP 8.4). | Try it |
Frequently Asked Questions
Start with checkdnsrr()
Open the next guide—syntax, examples, and FAQs.
7 people found this page helpful
