PHP  
downloads | documentation | faq | getting help | mailing lists | | php.net sites | links 
search for in the  
<muscat_setupcheckdnsrr>
Last updated: Wed, 15 Jan 2003
view the printer friendly version or the printer friendly version with notes or change language to English | Brazilian Portuguese | Chinese (Simplified) | Chinese (Traditional) | Czech | Dutch | Finnish | French | German | Hebrew | Hungarian | Japanese | Korean | Polish | Romanian | Russian | Slovak | Slovenian | Spanish | Swedish | Turkish

LXVI. Funzioni di rete

Sommario
checkdnsrr --  Controlla i record DNS relativi ad un host Internet o indirizzo IP
closelog -- Chiude la connessione al logger di sistema
debugger_off -- Disattiva il debugger interno PHP
debugger_on -- Attiva il debugger interno PHP
define_syslog_variables -- Inizializza tutte le costanti collegate al syslog
dns_check_record -- Synonym for checkdnsrr()
dns_get_mx -- Synonym for getmxrr()
dns_get_record --  Fetch DNS Resource Records associated with a hostname
fsockopen --  Apre una connessione a un socket appartenente a un dominio Internet o Unix
gethostbyaddr --  Ottiene l'host Internet corrispondente a un dato indirizzo IP
gethostbyname --  Ottiene l'indirizzo IP corrispondente a un dato hostname Internet
gethostbynamel --  Ottiene la lista degli indirizzi IP corrispondenti a un dato hostname Internet
getmxrr --  Ottiene i record MX corrispondenti a un dato nome di host Internet
getprotobyname --  Ottiene il numero del protocollo associato al nome del protocollo
getprotobynumber --  Ottiene il nome del protocollo associato al numero del protocollo
getservbyname --  Ottiene il numero di porta associato ad un servizio Internet e ad un protocollo
getservbyport --  Ottiene il servizio Internet corrispondente ad una porta e ad un protocollo
ip2long --  Converts a string containing an (IPv4) Internet Protocol dotted address into a proper address. Converte una stringa contenente un indirizzo di rete del Protocollo Internet (IPv4) in un indirizzo espresso come tipo di dato int.
long2ip --  Converte un indirizzo di rete del Protocollo Internet (IPv4) in una stringa contenente un indirizzo espresso secondo la notazione standard di Internet.
openlog -- Apre una connessione al logger di sistema
pfsockopen --  Apre una connessione persistente Internet o di tipo domain socket Unix
socket_get_status --  Restituisce informazioni su una risorsa socket esistente
socket_set_blocking -- Imposta un socket in modalit� blocking/non-blocking
socket_set_timeout -- Imposta il tempo di timeout per un socket
syslog -- Genera un messaggio del system log
User Contributed Notes
Funzioni di rete
add a note about notes
oliviert at videotron dot ca
18-Feb-1999 09:31

If u wanna do one of these function on windows95, u have to install the lastest winsock before , if else php will crash with a message of wsock32.dll

()

philip at cornado dot c()m
04-Jul-2001 09:18

View a few Networking PHP PEAR classes here :

philip at cornado dot c()m
04-Jul-2001 09:20

More socket functions can be seen here :

philippe-at-cyberabuse.org
15-Dec-2001 02:46

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));
}

philippe-at-cyberabuse.org
12-Oct-2002 10:49

... and this one will do the opposite (o return NULL for invalid netblocks) :

1.0.0.0 1.0.255.255 -> 1.0.0.0/16
1.0.0.0 1.3.255.255 -> 1.0.0.0/14
192.168.0.0 192.168.0.255 -> 192.168.0.0/24

function ip2cidr($ip_start,$ip_end) {
if(long2ip(ip2long($ip_start))!=$ip_start or long2ip(ip2long($ip_end))!=$ip_end) return NULL;
$ipl_start=(int)ip2long($ip_start);
$ipl_end=(int)ip2long($ip_end);
if($ipl_start>0 && $ipl_end<0) $delta=($ipl_end+4294967296)-$ipl_start;
else $delta=$ipl_end-$ipl_start;
$netmask=str_pad(decbin($delta),32,"0","STR_PAD_LEFT");
if(ip2long($ip_start)==0 && substr_count($netmask,"1")==32) return "0.0.0.0/0";
if($delta<0 or ($delta>0 && $delta%2==0)) return NULL;
for($mask=0;$mask<32;$mask++) if($netmask[$mask]==1) break;
if(substr_count($netmask,"0")!=$mask) return NULL;
return "$ip_start/$mask";
}

trevor-hemsley at nospam dot dial dot pipex dot com
16-Oct-2002 06:53

Previous example of IP range to CIDR list does not cope with ranges as well as the perl Net::Netmask range2cidrlist() function. In PHP this looks like

<?
function imask($this)
{
// use base_convert not dechex because dechex is broken and returns 0x80000000 instead of 0xffffffff
return base_convert((pow(2,32) - pow(2, (32-$this)))), 10, 16);
}

function imaxblock($ibase, $tbit)
{
while ($tbit > 0)
{
$im = hexdec(imask($tbit-1));
$imand = $ibase & $im;
if ($imand != $ibase)
{
break;
}
$tbit--;
}
return $tbit;
}

function range2cidrlist($istart, $iend)
{
// this function returns an array of cidr lists that map the range given
$s = explode(".", $istart);
// PHP ip2long does not handle leading zeros on IP addresses! 172.016 comes back as 172.14, seems to be treated as octal!
$start = "";
$dot = "";
while (list($key,$val) = each($s))
{
$start = sprintf("%s%s%d",$start,$dot,$val);
$dot = ".";
}
$end = "";
$dot = "";
$e = explode(".",$iend);
while (list($key,$val) = each($e))
{
$end = sprintf("%s%s%d",$end,$dot,$val);
$dot = ".";
}
$start = ip2long($start);
$end = ip2long($end);
$result = array();
while ($end > $start)
{
$maxsize = imaxblock($start,32);
$x = log($end - $start + 1)/log(2);
$maxdiff = floor(32 - floor($x));
$ip = long2ip($start);
if ($maxsize < $maxdiff)
{
$maxsize = $maxdiff;
}
array_push($result,"$ip/$maxsize");
$start += pow(2, (32-$maxsize));
}
return $result;
}
?>

add a note about notes
<muscat_setupcheckdnsrr>
Last updated: Wed, 15 Jan 2003
show source | credits | mirror sites
Copyright © 2001-2003 The PHP Group
All rights reserved.
This mirror generously provided by: /
Last updated: Sun Jan 19 09:13:02 2003 CET