PHP Network Functions

Beginner
5 min read
Updated: Nov 2026
37 function guides
Function index

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

Beginner path.

Start with checkdnsrr() and gethostbyname()—then learn header(), setcookie(), and ip2long(), and finish with fsockopen() and openlog().

Aliases and PHP 8.4.

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:

PHP
$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 functions

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

FunctionDescriptionTutorial
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 functions

Map protocol names and numbers, and map internet service names to ports (and the reverse).

FunctionDescriptionTutorial
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 functions

Send, remove, and inspect response headers, set the status code, and (in PHP 8.4) read headers from the last HTTP wrapper response.

FunctionDescriptionTutorial
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

Cookies

2 functions

Set cookies as response headers. Prefer setcookie() for normal values; use setrawcookie() when you must skip URL-encoding.

FunctionDescriptionTutorial
setcookie()Send a cookie to the browser (URL-encodes the value).Try it
setrawcookie()Send a cookie without URL-encoding the value.Try it

IP address conversion

5 functions

Convert between dotted IPv4 text and integers, pack and unpack IPv4/IPv6 addresses, and list local network interfaces.

FunctionDescriptionTutorial
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 functions

Open TCP (or SSL) streams with fsockopen() / pfsockopen(). The socket_* names are aliases of stream helpers for status, blocking mode, and timeout.

FunctionDescriptionTutorial
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 functions

Open a connection to the system logger, write a message, and close the connection when you are done.

FunctionDescriptionTutorial
closelog()Close the connection to the system logger.Try it
openlog()Open a connection to the system logger.Try it
syslog()Write a message to the system logger.Try it

Request body

1 function

Parse a multipart or URL-encoded request body in PHP 8.4 and newer.

FunctionDescriptionTutorial
request_parse_body()Parse the request body into post and files data (PHP 8.4).Try it

Frequently Asked Questions

They are the built-in PHP functions for DNS and host lookups, protocol and service name tables, HTTP headers and cookies, IP address conversion, TCP sockets, syslog, and (in PHP 8.4) reading last HTTP response headers and parsing a request body. This index links to a full tutorial for each one.
The Network page teaches the ideas: DNS helpers, headers and cookies, IP conversion, sockets, and syslog, with three fixed examples. This index lists every function tutorial so you can open checkdnsrr(), header(), ip2long(), fsockopen(), and the rest.
Start with checkdnsrr() and gethostbyname() for DNS, then header() and setcookie() for web responses, then ip2long() and long2ip() for IPv4. Learn fsockopen() and openlog() after those basics. Alias pages such as dns_check_record() teach the same idea as their primary names.
dns_check_record() is an alias of checkdnsrr(). dns_get_mx() and getmxrr() are the same MX lookup (either name works). socket_get_status(), socket_set_blocking(), and socket_set_timeout() are aliases of stream_get_meta_data(), stream_set_blocking(), and stream_set_timeout(). Prefer the primary name in new code.
http_get_last_response_headers() and http_clear_last_response_headers() let you inspect or clear headers from the last HTTP stream-wrapper request. request_parse_body() parses multipart or URL-encoded bodies into post and files style data. Confirm function_exists() if you support older PHP versions.
Open checkdnsrr() next (it is the first guide in sidebar order), or jump to header() and setcookie() if you are building a web response. Read PHP Network first if the groups below still feel unfamiliar.

Start with checkdnsrr()

Open the next guide—syntax, examples, and FAQs.

checkdnsrr() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

7 people found this page helpful