PHP Network Functions

By | August 23, 2020
PHP Network Functions

PHP allows you many network functions that will provide you access to access the internet feature into the PHP. These functions come with core PHP and no installation is required.

PHP Network functions

Below is the list of the PHP Network functions-

Vamware

1. checkdnsrr()

this function will check for the DNS records for the corresponding host.

Syntax

checkdnsrr(host, type)

2. closelog()

This function will close the connection of system logger and this function will not take any parameter.

Syntax

closelog()

Example-

<?php
function funct($text) {
openlog("phperrors");
syslog(LOG_ERR, $text);
closelog();
}
?>

3. define_syslog_variables()

This function was deprecated and removed in PHP 5.4 and allows you to Initialize the variables used in Syslog functions

4. dns_check_record()

This function is an alias of checkdnsrr() and will take two parameters- host and type, where type can be-

  • A
  • MX (default)
  • NS
  • SOA
  • PTR
  • CNAME
  • AAAA
  • A6
  • SRV
  • NAPTR
  • TXT
  • ANY

Syntax

dns_check_record(host, type)

Example

<?php
$dom="XXX.com";
if(dns_check_record($dom,"MX")) {
  echo "Passed";
} else {
  echo "Failed";
}
?>

Output

passed

5. dns_get_mx()

This function is an alias of getmxrr() and accepts three parameters- host, mxhosts and weight where the first two parameters are mandatory and the last one is optional.

Syntax

dns_get_mx(host, mxhosts, weight)

Example

<?php
$dom="XXX.com";
if(dns_get_mx($dom,$mx_data)){
  foreach($mx_data as $key=>$value){
    echo "$key => $value <br>";
  }
}
?>

Output

0 => XXX.com

6. dns_get_record()

This function will get you the DNS resource records which are associated with the provided hostname where the first parameter is mandatory else are optional.

Syntax

dns_get_record(hostname, type, authns, addtl, raw)

Example

<?php
print_r(dns_get_record("XXX.com", DNS_MX));
?>

Output

Array ( [0] => Array ( [host] => XXX.com [class] => IN [ttl] => 1270 [type] => MX [pri] => 10 [target] => XXX.com ) )

7. fsockopen()

This function will open an Internet or Unix domain socket connection for the provided hostname. It will accept five parameters where the first parameter is mandatory and else parameters are optional.

Syntax

fsockopen(hostname, port, errno, errstr, timeout)

Example

<?php
$f_op = fsockopen("www.XXX.com", 80, $errno, $errstr, 20);
if (!$f_op) {
  echo "$errstr ($errno)<br>";
} else {
  $out = "GET / HTTP/1.1\r\n";
  fwrite($f_op, $out);
  while (!feof($f_op)) {
    echo fgets($f_op, 128);
  }
  fclose($f_op);
}
?>

8. gethostbyaddr()

This function will return the domain name for a provided IP address which is a mandatory parameter.

Syntax-

gethostbyaddr(ipaddress)

Example

<?php
$host_detail = gethostbyaddr($_SERVER["REMOTE_ADDR"]);
echo $host_detail;
?>

9. gethostbyname()

This function will provide the IPv4 address for a given domain/hostname

Syntax

gethostbyname(hostname)

Example

<?php
$data = gethostbyname("www.XXX.com");
echo $data;
?>

Output

141.0.173.173

10. gethostbyname()

This function will return the hostname of your local machine.

Syntax

gethostname()

Example

<?php
$hostdata = gethostbyname("www.XXX.com");
print_r($hostdata);
?>

Output

Array ( [0] => 141.0.173.173 )

11. getmxrr()

This function will return the MX records for the provided internet hostname

Syntax-

getmxrr(host, mxhosts, weight)

Example

<?php
$dom="XXX.com";
if(getmxrr($dom,$mx_details)){
  foreach($mx_details as $key=>$value){
    echo "$key => $value <br>";
  }
}
?>

Output

0 => XXX.com

12. getprotobyname()

This function will allow you to return the protocol number for a specified protocol name

Syntax

getprotobyname(protocol name)

Example

<?php
$protocol = getprotobyname("tcp");
echo $protocol;
?>

Output

6

13. getprotobynumber()

This function will allow you to return the protocol name for a specified protocol number

Syntax-

getprotobynumber(protocol number)

Example

<?php
$pro_name = getprotobynumber(6);
echo $pro_name;
?>

Output

tcp

14. getservbyname()

This function will return the port number for specified Internet service and protocol and both the parameters are mandatory for providing the output.

Syntax-

getservbyname(service, protocol)

Example

<?php
$port_num = getservbyname("http", "tcp");
echo $port_num;
?>

Output

80

15. getservbyport()

This function will return the Internet service for a specified port and protocol where both the parameters are required.

Syntax

getservbyport(port, protocol)

Example

<?php
$ser_name = getservbyport(80, "tcp");
echo $ser_name;
?>

Output

http

16. header_register_callback()

This function will allow you to call a header function

Syntax

header_register_callback(callback)

17. header_remove()

This function will allow you to remove an HTTP header that is previously set with the header() function

Syntax

header_remove(headername)

Example

<?php
header("Pragma: no-cache");
header_remove("Pragma");
?>

18. header()

This function will allow you to send a raw specified HTTP header to a client. This function will take three parameters where the first two are mandatory and the last parameter is optional.

Syntax

header(header, replace, http_response_code)

Example

<?php
header("Pragma: no-cache");
//header_remove("Pragma");
?>

19. headers_list()

This function will allow you to return a list of response headers which will be sent to the browser

Syntax

headers_list()

Example

<?php
header("X-Test: foo");
header("Content-type: text/plain");
?>
<html>
<body>
<?php
var_dump(headers_list());
?>
</body>
</html>

Output

array(2)
{
[0]=> string(11) "X-Test: foo"
[1]=> string(24) "Content-type: text/plain"
}

20. headers_sent()

This function will allow you to check if/where headers have been sent

Syntax

headers_sent(file,line)

Example

<?php
if (!headers_sent()) {
  header("Location: https://www.XXX.com/");
  exit;
}
?>

21. http_response_code()

This function will allow you to set or return the status code of the HTTP response.

Syntax

http_response_code(code)

Example

<?php
http_response_code(404);
?>

Output

Page not found

22. inet_ntop()

This function will convert a 32bit IPv4 or 128bit IPv6 address into a readable format where the address which is provided is mandatory.

Syntax

inet_ntop(address)

Example

<?php
$addr_data = chr(117) . chr(4) . chr(1) . chr(1);
$exp = inet_ntop($addr_data);
echo $exp;
?>

Output

117.4.1.1

23. inet_pton()

This function will allow you to convert a readable IP address into a packed 32bit IPv4 or 128bit IPv6 format which is the vice versa of the inet_ntop() function.

Syntax

inet_pton(address)

Example

<?php
$addr = inet_pton("115.23.85.4");
echo $addr;
?>

Output

s*U*

24. ip2long()

This function will convert a provided IPv4 address into a long integer

Syntax

ip2long(address)

Example

<?php
$addr = gethostbyname("www.XXX.com");
$out = "https://www.XXX.com/, https://" . $addr . "/, and https://" . sprintf("%u", ip2long($addr)) . "/";
echo $out;
?>

Output

https://www.XXX.com/, https://141.0.173.173/, and https://2365631917/

25. long2ip()

This function will allow you to convert a long integer address into a string in IPv4 format and this function is vice versa of the ip2long() function.

Syntax

long2ip(address)

Example

<?php
echo(long2ip(2365631917));
?>

Output

141.0.173.173

26. openlog()

This function will open the connection of system logger. This function will have three parameters and all are mandatory.

Syntax

openlog(ident, option, facility)

Example

<?php
function _log($text) {
openlog("phperrors");
syslog(LOG_ERR, $text);
closelog();
}
?>

27. pfsockopen()

This function will allow you to open a persistent Internet or Unix domain socket connection on the basis of the provided hostname. This function will have five parameters among which the first parameter is mandatory while others are optional.

Syntax

pfsockopen(hostname, port, errno, errstr, timeout)

Example

<?php
$f_op = pfsockopen("www.XXX.com", 80, $errno, $errstr, 20);
if (!$f_op) {
  echo "$errstr ($errno)<br>";
} else {
  $out = "GET / HTTP/1.1\r\n";
  $out .= "Host: www.XXX.com\r\n";
  $out .= "Connection: Close\r\n\r\n";
  fwrite($f_op, $out);
  while (!feof($f_op)) {
    echo fgets($f_op, 128);
  }
  fclose($f_op);
}
?>

Output

HTTP/1.1 302 Found Content-Type: text/html; charset=utf-8 Location: https://www.XXX.com/ Server: Caddy Date: Tue, 28 Jul 2020 13:38:53 GMT Content-Length: 43 Connection: close Found.

28. setcookie()

This function will allow you to define a cookie which needs to be sent along with the rest of the HTTP headers. This function has one mandatory parameter- a name which specifies the cookie name.

Syntax

setcookie(name, value, expire, path, domain, secure, httponly);

Example

<!DOCTYPE html>
<?php
$c_name = "user";
$c_value = "Jacob";
setcookie($c_name, $c_value, time() + (86400 * 30), "/"); 
?>
<html>
<body>
<?php
if(!isset($_COOKIE[$c_name])) {
    echo   $c_name . "' is not set!";
} else {
    echo $c_name . "' is set!<br>";
    echo   $_COOKIE[$c_name];
}
?>
</body>
</html>

Output

PHP Warning: Cannot modify header information – headers already sent by (output started at /workspace/Main.php:1) in /workspace/Main.php on line 5

29. setrawcookie()

This function will allow you to define a cookie that does not have URL encoding and will be sent along with the rest of the HTTP headers

Syntax

setrawcookie(name, value, expire, path, domain, secure);

Example

<?php
$cookie_name = "jacob";
$cookie_value = "Hello";
setrawcookie($cookie_name, $cookie_value);
?>
<html>
<body>
<?php
echo "Cookie is set.";
?>
</body>
</html>
?>

Output

Cookie is set.

30. socket_get_status()

This function works as an alias of stream_get_meta_data() function.

31. socket_set_blocking()

This function will work as an alias of stream_set_blocking() function.

32. socket_set_timeout()

This function will work as an alias of stream_set_timeout() function.

33. syslog()

This function will allow you to generate a system log message.

Example

<?php
function func($text) {
openlog("phperrors");
syslog(LOG_ERR, $text);
closelog();
}
?>

Leave a Reply

Your email address will not be published. Required fields are marked *