Network functions allow us to interact with internet protocols. We can use them to read the content of remote pages, send emails, or even create our own mini-servers using sockets.
PHP supports low-level programming with sockets. The example below displays the logic of a server listening on a
specific port. Warning: Most shared hosting providers block the socket_create function for security
reasons. It is best to test these examples on a local server (XAMPP) or a VPS.
file_get_contents for external URLs for better
error handling.Network programming in PHP enables the creation of robust applications that communicate with other services and APIs.
These functions are part of the PHP core and allow the manipulation of information before the server sends output to the browser. No additional installation is required.
| Function | Description | Example |
|---|---|---|
checkdnsrr() |
Check DNS records corresponding to a given Internet host name or IP address. | echo checkdnsrr("google.com", "MX") ? "Exists" : "Does not exist"; |
closelog() |
Closes the connection to the system logger. | openlog("myApp", LOG_PID, LOG_USER); syslog(LOG_INFO, "Test"); closelog(); |
dns_check_record() |
Checks DNS records corresponding to a given Internet host name or IP address. | echo dns_check_record("google.com", "A") ? "Found" : "Not found"; |
dns_get_mx() |
Get MX records corresponding to a given Internet host name. | dns_get_mx("google.com", $mx_hosts); print_r($mx_hosts); |
dns_get_record() |
Fetch DNS resource records associated with a hostname. | $dns = dns_get_record("google.com", DNS_A); print_r($dns[0]); |
fsockopen() |
Open Internet or Unix domain socket connection. | $fp = @fsockopen("www.google.com", 80, $errno, $errstr, 5); echo $fp ? "Open" : "Closed"; |
gethostbyaddr() |
Get the Internet host name corresponding to a given IP address. | echo gethostbyaddr("8.8.8.8"); // Outputs: dns.google |
gethostbyname() |
Get the IPv4 address corresponding to a given Internet host name. | echo gethostbyname("google.com"); // Outputs the IP address |
gethostbynamel() |
Get a list of IPv4 addresses corresponding to a given Internet host name. | $ips = gethostbynamel("google.com"); print_r($ips); |
gethostname() |
Gets the standard host name for the local machine. | echo gethostname(); // Outputs the current server hostname |
getmxrr() |
Get MX records corresponding to a given Internet host name. | getmxrr("google.com", $mx); echo "Found MX count: " . count($mx); |
getprotobyname() |
Get protocol number associated with protocol name. | echo getprotobyname("tcp"); // Outputs: 6 |
getprotobynumber() |
Get protocol name associated with protocol number. | echo getprotobynumber(6); // Outputs: tcp |
getservbyname() |
Get port number associated with an Internet service and protocol. | echo getservbyname("www", "tcp"); // Outputs: 80 |
getservbyport() |
Get Internet service associated with port and protocol. | echo getservbyport(80, "tcp"); // Outputs: http |
header() |
Send a raw HTTP header. | header("Content-Type: text/plain"); // Sets plain text output |
header_register_callback() |
Call a header function. | header_register_callback(function(){ echo "Headers sent!"; }); |
header_remove() |
Remove HTTP headers previously set with header(). | header("X-Test: Custom"); header_remove("X-Test"); |
headers_list() |
Returns a list of response headers sent (or ready to send). | $headers = headers_list(); print_r($headers); |
headers_sent() |
Checks if or where headers have been sent. | echo headers_sent() ? "Headers already sent" : "Headers still waiting"; |
http_response_code() |
Get or Set the HTTP response status code. | echo http_response_code(404); // Sets code to 404 Not Found |
inet_ntop() |
Converts a packed internet address to a human-readable representation. | echo inet_ntop(chr(127).chr(0).chr(0).chr(1)); // Outputs: 127.0.0.1 |
inet_pton() |
Converts a human-readable IP address to its packed in_addr representation. | echo bin2hex(inet_pton("127.0.0.1")); // Outputs the binary hex |
ip2long() |
Converts a string containing an IPv4 dotted address into a long integer. | echo ip2long("127.0.0.1"); // Outputs: 2130706433 |
long2ip() |
Converts an long integer address into a string in IPv4 dotted format. | echo long2ip(2130706433); // Outputs: 127.0.0.1 |
openlog() |
Opens a connection to the system logger for a program. | openlog("System_Borut", LOG_PID, LOG_USER); syslog(LOG_NOTICE, "Startup"); |
pfsockopen() |
Open persistent Internet or Unix domain socket connection. | $fp = @pfsockopen("localhost", 80); echo $fp ? "Persistently open" : "No connection"; |
setcookie() |
Send a cookie. | setcookie("user", "John", time() + 3600); // Valid for 1 hour |
socket_get_status() |
Alias of stream_get_meta_data(). | $fp = fsockopen("google.com", 80); print_r(socket_get_status($fp)); |
socket_set_blocking() |
Alias of stream_set_blocking(). | $fp = fsockopen("google.com", 80); socket_set_blocking($fp, false); |
stream_get_meta_data() |
Retrieves header/meta data from streams/file pointers. | $fp = fsockopen("google.com", 80); print_r(stream_get_meta_data($fp)); |
syslog() |
Generate a system log message. | syslog(LOG_WARNING, "Warning: High system load detected!"); |