10 important PHP network information functions

Source: Internet
Author: User
Tags mail exchange reverse dns
PHP is so tightly integrated with web pages that it is easy to forget that this language can also be used to execute other functions-data encryption, window application and interface programming are three examples. Since network programming is the most troublesome field for most developers, it is worthwhile to take a moment to look at the built-in network functions of PHP and learn about the functions they provide, and how it helps you complete the next project.

PHP is so tightly integrated with web pages that it is easy to forget that this language can also be used to execute other functions-data encryption, window application and interface programming are three examples. Since network programming is the most troublesome field for most developers, it is worthwhile to take a moment to look at the built-in network functions of PHP and learn about the functions they provide, and how it helps you complete the next project.

The following table (table A) lists the 10 most interesting and useful network functions in PHP. it is the starting point for you to understand PHP network programming functions, it also provides inspiration for you to enter areas that are not typically involved by most network developers. Each function has a description and usage example to show you when to apply these functions.


Function Description Instance
 

Getmxrr

($ Hostname,

$ Mxhosts)

This function is used to restore the MX (mail exchange record) host name of a specific host.

Use this function to confirm the email host of a domain, usually starting with verifying a mailbox in that domain.

Code:

$ Hosts = array ();

$ Ret = getmxrr

('Techrepublic. com', $ hosts );

If ($ ret ){

Print_r ($ hosts );

} Else {

Echo 'MX retrieval failed ';

}

?>

 

Output:

Array (

C10-mail.cnet.com

C12-mail.cnet.com

)

Gethostbyaddr
($ Ip)
This function is used to restore the host name related to an IP address.
Use this function to perform a reverse DNS lookup and assign a name to an IP address, for example, the IP address that records your network server logs.

Code:
Echo gethostbyaddr
('1970. 239.115.148 ');
?>

Output:
C10-sha-redirect-
Lb.cnet.com

Gethostbyname
($ Name)
This function is opposite to the gethostbyaddr () function to restore the IP address related to the host.
Use this function to perform a standard DNS lookup to obtain the host name related to the IP address-for example, when the suspicious domain is automatically blacklisted.

Code:
Echo gethostbyname
('Techrepublic. com ');
?>

Output:
216.239.115.148

P2long ($ ip)
And
Long2ip ($ long)
These functions convert the IP addresses represented by four numbers less than 255 connected by the symbol into integers or perform inverse operations.
These functions are used when you need to express IP addresses in integer format (usually used for digital computing) or use IP addresses to represent digital formats.

Code:
Echo ip2long
('1970. 239.115.148 ');
Echo long2ip (-655395948 );
?>

Output:
-655395948
216.239.115.148

Checkdnsrr
($ Host, $ type)
This function checks the $ type record that matches the $ host in DNS. if found, the Boolean true value is returned.
Use this function to check whether a specific DNS record type exists in the host.

Code:
$ Ret = checkdnsrr
('Techrepublic. com', SOA );
If ($ ret ){
Echo 'soa records
Exist for host ';
} Else {
Echo 'soa records do
Not exist for host ';
}
?>

Output:
SOA records exist for host

Dns_get_record
($ Host, $ type)
This function returns the DNS record of the $ host. The optional $ type parameter can only be used to restore subsets that match a specific type.
Use this function to restore detailed DNS records of a special host.

Code:
$ Data = dns_get_record
('Techrepublic. com ');
Print_r ($ data );
?>

Output:
Array
(
[0] => Array
(
[Host] =>
Techrepublic.com
[Type] => MX
[Pri] = & gt; 500
[Target] =>
C10-mail.cnet.com
[Class] => IN
[Ttl] = & gt; 10756
)
[1] => Array
(
[Host] =>
Techrepublic.com
[Type] => NS
[Target] =>
Ns3.cnet.com
[Class] => IN
[Ttl] = & gt; 7885
)
)

Getprotobyname
($ Num)
And
Getprotobynum
($ Name)
These functions restore the protocol name and number from the/etc/protocols file of the pan system.
Use these functions to restore system protocol information by name or number.

 
Code:
Echo getprotobyname (81 );
Echo getprotobyname
('Icmp ');
?>
 

Output:
Vmtp
1

Getservbyname
($ Service,
$ Protocol)
This function uses the $ protocol to restore the port number of $ service from the/etc/services file of the pan system.
Use this function to automatically obtain the port information for running system services.

Code:
Echo getservbyname ('http ',
'Tcp ');
?>

Output:
80

Inet_ntop
($ Addr)
And
Inet_pton
($ Addr)
These functions convert IP addresses between binary and readable addresses.
Use this function to convert Ipv4/Ipv6 address strings to binary expressions.

Code:
$ Packed = inet_pton
('1970. 168.0.1 ');
$ Unpacked = inet_ntop
($ Packed );
Echo $ unpacked;
?>

Output:
192.168.0.1

Syslog
($ Level, $ msg)
This function uses the warning level $ level to record $ msg information to the system log device.
Use this function to publish a wildcard system error or warning.
Code:
Define_syslog_variables ();
Openlog ('mylog ',
LOG_NDELAY, LOG_LOCAL0 );
Syslog (LOG_DEBUG, 'This is
A debug message ');
Closelog ();
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.