Network Функции
Содержание
- checkdnsrr — Check DNS records corresponding to a given Internet host name or IP address
- closelog — Close connection to system logger
- define_syslog_variables — Initializes all syslog related constants
- dns_check_record — Alias of checkdnsrr
- dns_get_mx — Alias of getmxrr
- dns_get_record — Fetch DNS Resource Records associated with a hostname
- fsockopen — Open Internet or Unix domain socket connection
- gethostbyaddr — Get the Internet host name corresponding to a given IP address
- gethostbyname — Get the IP address corresponding to a given Internet host name
- gethostbynamel — Get a list of IP addresses corresponding to a given Internet host
name
- getmxrr — Get MX records corresponding to a given Internet host name
- getprotobyname — Get protocol number associated with protocol name
- getprotobynumber — Get protocol name associated with protocol number
- getservbyname — Get port number associated with an Internet service and protocol
- getservbyport — Get Internet service which corresponds to port and protocol
- header — Send a raw HTTP header
- headers_list — Returns a list of response headers sent (or ready to send)
- headers_sent — Checks if or where headers have been sent
- inet_ntop — Converts a packed internet address to a human readable representation
- inet_pton — Converts a human readable IP address to its packed in_addr representation
- ip2long — Converts a string containing an (IPv4) Internet Protocol dotted address into a proper address
- long2ip — Converts an (IPv4) Internet network address into a string in Internet standard dotted format
- openlog — Open connection to system logger
- pfsockopen — Open persistent Internet or Unix domain socket connection
- setcookie — Send a cookie
- setrawcookie — Send a cookie without urlencoding the cookie value
- socket_get_status — Alias of stream_get_meta_data
- socket_set_blocking — Alias of stream_set_blocking
- socket_set_timeout — Alias of stream_set_timeout
- syslog — Generate a system log message
Коментарии
PHP miss CIDR functions.
This one will convert a CIDR like this:
0.0.0.0/16 -> 0.0.0.0 - 0.0.255.255
127.0/16 -> 127.0.0.0 - 127.0.255.255
etc...
function cidrconv($net) {
$start=strtok($net,"/");
$n=3-substr_count($net, ".");
if ($n>0) { for ($i=$n;$i>0;$i--) $start.=".0"; }
$bits1=str_pad(decbin(ip2long($start)),32,"0","STR_PAD_LEFT");
$net=pow(2,(32-substr(strstr($net,"/"),1)))-1;
$bits2=str_pad(decbin($net),32,"0","STR_PAD_LEFT");
for ($i=0;$i<32;$i++) {
if ($bits1[$i]==$bits2[$i]) $final.=$bits1[$i];
if ($bits1[$i]==1 and $bits2[$i]==0) $final.=$bits1[$i];
if ($bits1[$i]==0 and $bits2[$i]==1) $final.=$bits2[$i];
}
return $start." - ".long2ip(bindec($final));
}
A simple and very fast function to check against CIDR.
Your previous examples are too complicated and involves a lot of functions call.
Here it is (only with arithmetic operators and call only to ip2long () and split() ):
<?php
function ipCIDRCheck ($IP, $CIDR) {
list ($net, $mask) = split ("/", $CIDR);
$ip_net = ip2long ($net);
$ip_mask = ~((1 << (32 - $mask)) - 1);
$ip_ip = ip2long ($IP);
$ip_ip_net = $ip_ip & $ip_mask;
return ($ip_ip_net == $ip_net);
}
?>
call example: <?php echo ipCheck ("192.168.1.23", "192.168.1.0/24"); ?>
Here a IP-Range to CIDRs function that I wrote for the purpose of filling my Postfix client.cidr with ripe-ncc data to block spamming from useless countries. Strcmp functions are meant to work around the silly PHP string comparison which inevitably tries compare strings as numbers when possible. I'll make no comment about that fact ... bit I have to bite my tong hard :
function PlageVersCIDRs($ip_min, $ip_max) {
$cidrs = array();
$ip_min_bin = sprintf('%032b', $ip_min);
$ip_max_bin = sprintf('%032b', $ip_max);
$ip_cour_bin = $ip_min_bin;
while (strcmp($ip_cour_bin, $ip_max_bin) <= 0) {
$lng_reseau = 32;
$ip_reseau_bin = $ip_cour_bin;
while (($ip_cour_bin[$lng_reseau - 1] == '0') && (strcmp(substr_replace($ip_reseau_bin, '1', $lng_reseau - 1, 1), $ip_max_bin) <= 0)) {
$ip_reseau_bin[$lng_reseau - 1] = '1';
$lng_reseau--;
}
$cidrs[] = long2ip(bindec($ip_cour_bin)).'/'.$lng_reseau;
$ip_cour_bin = sprintf('%032b', bindec($ip_reseau_bin) + 1);
}
return $cidrs;
}
improved version of philippe-at-cyberabuse.org's answer:
<?php
function cidrconv($net) {
$start = strtok($net,"/");
$n = 3 - substr_count($net, ".");
if ($n > 0)
{
for ($i = $n;$i > 0; $i--)
$start .= ".0";
}
$bits1 = str_pad(decbin(ip2long($start)), 32, "0", STR_PAD_LEFT);
$net = (1 << (32 - substr(strstr($net, "/"), 1))) - 1;
$bits2 = str_pad(decbin($net), 32, "0", STR_PAD_LEFT);
$final = "";
for ($i = 0; $i < 32; $i++)
{
if ($bits1[$i] == $bits2[$i]) $final .= $bits1[$i];
if ($bits1[$i] == 1 and $bits2[$i] == 0) $final .= $bits1[$i];
if ($bits1[$i] == 0 and $bits2[$i] == 1) $final .= $bits2[$i];
}
return array($start, long2ip(bindec($final)));
}
?>
An improved version of claudiu at cnixs dot com not using split and working with the following:
ip: 192.168.101.123, CIRD: 192.168.101.144/24
<?php
function ipCIDRCheck ($IP, $CIDR) {
list ($net, $mask) = explode ('/', $CIDR);
$ip_net = ip2long ($net);
$ip_mask = ~((1 << (32 - $mask)) - 1);
$ip_ip = ip2long ($IP);
return (($ip_ip & $ip_mask) == ($ip_net & $ip_mask));
}
?>