Funções de Network

Índice

  • checkdnsrr — Verifica os registros DNS correspondentes para o nome do host ou endereço IP informado
  • closelog — Fecha conexão com o registrador do sistema
  • dns_check_record — Sinônimo de checkdnsrr
  • dns_get_mx — Sinônimo de getmxrr
  • dns_get_record — Busca registros de recursos DNS associados a um nome de host
  • fsockopen — Abre uma conexão socket de domínio Unix ou de Internet
  • gethostbyaddr — Obtém nome do servidor de Internet correspondente ao endereço de IP fornecido
  • gethostbyname — Obtém o endereço IPv4 correspondente a um determinado nome de host da Internet
  • gethostbynamel — Obtém uma lista de endereços IPv4 correspondentes a um determinado nome de host da Internet
  • gethostname — Obtém o nome do host
  • getmxrr — Obtém registros MX correspondentes a um determinado nome de host da Internet
  • getprotobyname — Obtém o número do protocolo associado ao nome do protocolo
  • getprotobynumber — Obtém o nome do protocolo associado ao número do protocolo
  • getservbyname — Obtém o número da porta associado a um serviço e protocolo de Internet
  • getservbyport — Obtém o serviço de Internet que corresponde à porta e ao protocolo
  • header — Envia um cabeçalho HTTP bruto
  • header_register_callback — Chama uma função de cabeçalho
  • header_remove — Remove cabeçalhos definidos anteriormente
  • headers_list — Retorna uma lista de cabeçalhos de resposta enviados (ou prontos para enviar)
  • headers_sent — Verifica se ou onde os cabeçalhos foram enviados
  • http_clear_last_response_headers — Clears the stored HTTP response headers
  • http_get_last_response_headers — Retrieve last HTTP response headers
  • http_response_code — Obtém ou define o código de resposta HTTP
  • inet_ntop — Converte um endereço de Internet compactado em uma representação legível
  • inet_pton — Converte um endereço IP legível em sua representação de endereço de Internet compactado
  • ip2long — Converte uma string contendo um endereço com pontos do protocolo da Internet (IPv4) em um número inteiro longo
  • long2ip — Converte um endereço inteiro longo em uma string no formato padrão com pontos da Internet (IPv4)
  • net_get_interfaces — Obtém interfaces de rede
  • openlog — Abre conexão com o registrador do sistema
  • pfsockopen — Abra uma conexão persistente de soquete de domínio Unix ou de Internet
  • request_parse_body — Read and parse the request body and return the result
  • setcookie — Envia um cookie
  • setrawcookie — Envie um cookie sem codificar em URL o valor do cookie
  • socket_get_status — Sinônimo de stream_get_meta_data
  • socket_set_blocking — Sinônimo de stream_set_blocking
  • socket_set_timeout — Sinônimo de stream_set_timeout
  • syslog — Gera uma mensagem de registro do sistema
adicione uma nota

Notas Enviadas por Usuários (em inglês) 5 notes

up
37
claudiu at cnixs dot com
17 years ago
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"); ?>
up
10
Anton Avramov - lukav at lukav dot com
7 years ago
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));
}
?>
up
4
Anonymous
8 years ago
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)));
}
?>
up
2
David GASTALDIN
17 years ago
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;
}
up
2
philippe-at-cyberabuse.org
23 years ago
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));
}
To Top