How To Use Network Functions In PHP Example

PHP 8 , PHP

Nicesnippets

881

15-07-2021


How To Use Network Functions In PHP Example

Hi guys,

Today i will explained how to use php Network functions. This example is so easy to use in php.

This functions is provided to the default php language. So you can easily to use in your php code. Today i will explaind checkdnsrr(),closelog(),dns_check_record(),dns_get_mx(),dns_get_record(),fsockopen() to explainded.

So let's start to the example.

checkdnsrr() Function


The checkdnsrr() function checks DNS records for type corresponding to host.

<?php

$domain="w3schools.com";

if(checkdnsrr($domain,"MX")) {

echo "Passed";

} else {

echo "Failed";

}

?>

Output

Passed

closelog() Function

The closelog() functionoselog() Function

The closelog() function closes the connection of system logger

<?php

function _log($text) {

openlog("phperrors", LOG_PID | LOG_PERROR);

syslog(LOG_ERR, $text);

closelog();

}

?>

dns_check_record() Function

The dns_check_record() function is an alias of the checkdnsrr() function.

<?php

$domain="w3schools.com";

if(dns_check_record($domain,"MX")) {

echo "Passed";

} else {

echo "Failed";

}

?>

Output

Passed

dns_get_mx() Function

The dns_get_mx() function is an alias of the checkdnsrr() function.

<?php

$domain="w3schools.com";

if(dns_get_mx($domain,$mx_details)){

foreach($mx_details as $key=>$value){

echo "$key => $value <br>";

}

}

?>

Output

0 => alt2.aspmx.l.google.com

1 => alt1.aspmx.l.google.com

2 => feedback-smtp.eu-west-1.amazonses.com

3 => alt4.aspmx.l.google.com

4 => alt3.aspmx.l.google.com

5 => aspmx.l.google.com

dns_get_record() Function

The dns_get_record() function gets the DNS resource records associated with the specified hostname.

<?php

print_r(dns_get_record("w3schools.com", DNS_MX));

?>

Output

Array

(

[0] => Array

(

[host] => w3schools.com

[class] => IN

[ttl] => 32

[type] => MX

[pri] => 5

[target] => alt2.aspmx.l.google.com

)

[1] => Array

(

[host] => w3schools.com

[class] => IN

[ttl] => 32

[type] => MX

[pri] => 5

[target] => alt1.aspmx.l.google.com

)

[2] => Array

(

[host] => w3schools.com

[class] => IN

[ttl] => 32

[type] => MX

[pri] => 10

[target] => feedback-smtp.eu-west-1.amazonses.com

)

[3] => Array

(

[host] => w3schools.com

[class] => IN

[ttl] => 32

[type] => MX

[pri] => 10

[target] => alt4.aspmx.l.google.com

)

[4] => Array

(

[host] => w3schools.com

[class] => IN

[ttl] => 32

[type] => MX

[pri] => 10

[target] => alt3.aspmx.l.google.com

)

[5] => Array

(

[host] => w3schools.com

[class] => IN

[ttl] => 32

[type] => MX

[pri] => 1

[target] => aspmx.l.google.com

)

)

fsockopen() Function

The fsockopen() function opens an Internet or Unix domain socket connection.

<?php

$fp = fsockopen("www.w3schools.com", 80, $errno, $errstr, 20);

if (!$fp) {

echo "$errstr ($errno)<br>";

} else {

$out = "GET / HTTP/1.1\r\n";

$out .= "Host: www.w3schools.com\r\n";

$out .= "Connection: Close\r\n\r\n";

fwrite($fp, $out);

while (!feof($fp)) {

echo fgets($fp, 128);

}

fclose($fp);

}

?>

Output

Cache-Control: public

Content-Security-Policy: frame-ancestors 'self' https://mycourses.w3schools.com;

Content-Type: text/html; charset=UTF-8

Date: Thu, 15 Jul 2021 09:33:36 GMT

Location: https://www.w3schools.com/

Server: Microsoft-IIS/10.0

X-Content-Security-Policy: frame-ancestors 'self' https://mycourses.w3schools.com;

X-Powered-By: ASP.NET

Content-Length: 149

Connection: close

Object Moved

This document may be found here

Now you can check your own.

I hope it can help you...


Recommended Posts