|
 |
XCI. Network FunctionsЭти функции всегда доступны. Для использования этих функций не требуется проведение установки,
поскольку они являются частью ядра PHP.
Поведение этих функций зависит от установок в php.ini.
Таблица 1. Network Configuration Options Name | Default | Changeable | Changelog |
---|
define_syslog_variables | "0" | PHP_INI_ALL | |
Для подробного описания констант
PHP_INI_*, обратитесь к документации функции ini_set().
Краткое разъяснение конфигурационных
директив.
- define_syslog_variables
boolean
Whether or not to define the various syslog variables (e.g. $LOG_PID,
$LOG_CRON, etc.). Turning it off is a good idea performance-wise. At
runtime, you can define these variables by calling
define_syslog_variables().
Данное расширение не определяет никакие типы ресурсов.
Перечисленные ниже константы всегда доступны как часть ядра PHP.
Таблица 2. openlog() Options Constant | Description |
---|
LOG_CONS |
if there is an error while sending data to the system logger,
write directly to the system console
| LOG_NDELAY |
open the connection to the logger immediately
| LOG_ODELAY |
(default) delay opening the connection until the first
message is logged
| LOG_NOWAIT | | LOG_PERROR | print log message also to standard error | LOG_PID | include PID with each message |
Таблица 3. openlog() Facilities Constant | Description |
---|
LOG_AUTH |
security/authorization messages (use LOG_AUTHPRIV instead
in systems where that constant is defined)
| LOG_AUTHPRIV | security/authorization messages (private) | LOG_CRON | clock daemon (cron and at) | LOG_DAEMON | other system daemons | LOG_KERN | kernel messages | LOG_LOCAL0 ... LOG_LOCAL7 | reserved for local use, these are not available in Windows | LOG_LPR | line printer subsystem | LOG_MAIL | mail subsystem | LOG_NEWS | USENET news subsystem | LOG_SYSLOG | messages generated internally by syslogd | LOG_USER | generic user-level messages | LOG_UUCP | UUCP subsystem |
Таблица 4. syslog() Priorities (in descending order) Constant | Description |
---|
LOG_EMERG | system is unusable | LOG_ALERT | action must be taken immediately | LOG_CRIT | critical conditions | LOG_ERR | error conditions | LOG_WARNING | warning conditions | LOG_NOTICE | normal, but significant, condition | LOG_INFO | informational message | LOG_DEBUG | debug-level message |
Таблица 5. dns_get_record() Options Constant | Description |
---|
DNS_A | IPv4 Address Resource | DNS_MX | Mail Exchanger Resource | DNS_CNAME | Alias (Canonical Name) Resource | DNS_NS | Authoritative Name Server Resource | DNS_PTR | Pointer Resource | DNS_HINFO | Host Info Resource (See IANA's
Operating System Names
for the meaning of these values) | DNS_SOA | Start of Authority Resource | DNS_TXT | Text Resource | DNS_ANY | Any Resource Record. On most systems
this returns all resource records, however
it should not be counted upon for critical
uses. Try DNS_ALL instead. | DNS_AAAA | IPv6 Address Resource | DNS_ALL | Iteratively query the name server for
each available record type. |
Network Functions
metator at netcabo dot pt
28-Jul-2005 07:34
Ups. The function has a bug, though the example still works with it. Just replace the $_POST variables by the function parameters.
<?
function isIPIn($ip, $net, $mask) {
$ip = ip2long($ip);
$rede = ip2long($net);
$mask = ip2long($mask);
$res = $ip & $mask;
return ($res == $rede);
}
?>
metator at netcabo dot pt
27-Jul-2005 08:22
Regarding samuele's note:
You can get faster code if you apply directly what happens in network devices, such as routers. If you AND (logic operation) the remote ip against the local netmask the result will be the network ip if the remote ip is from the local network. Example:
192.168.0.16 = 11000000.10101000.00000000.00010000
& 255.255.255.0 = 11111111.11111111.11111111.00000000
--------------------------------------------------------------
192.168.0.0 = 11000000.10101000.00000000.00000000
And now the code. My example uses a html form where you place the values you want to test:
<HTML><HEAD><TITLE>Check IP</TITLE>
</HEAD><BODY>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
Hope you find this useful.
IP to check: <input type="text" name="ip"> <br>
Local network ip: <input type="text" name="net"> <br>
Local netmask: <input type="text" name="mask"> <br>
<input type="submit" name="check" value="Check it!">
</form>
<?php
function isIPIn($ip, $net, $mask) {
$ip = ip2long($_POST['ip']);
$rede = ip2long($_POST['net']);
$mask = ip2long($_POST['mask']);
$res = $ip & $mask;
return ($res == $rede);
}
if (isset($_POST['check'])) {
echo isIPIn($_POST['ip'], $_POST['net'], $_POST['mask']) ? "IP IN.": "IP OUT.";
}
?>
</BODY><HTML>
nexxer at rogers dot com
24-Feb-2005 05:16
In Trevor Hemsley's translation of the perl range2cidr function, the
while ($end > $start)
condition should be
while ($end >= $start)
otherwise it won't work for /32s, ie if you feed range2cidr("1.2.3.4", "1.2.3.4").
-- nex
samuele at norsam dot org
25-Nov-2003 09:01
To find if an IP is in a net/mask (very fast):
<?php
function isIPIn($ip,$net,$mask) {
$lnet=ip2long($net);
$lip=ip2long($ip);
$binnet=str_pad( decbin($lnet),32,"0","STR_PAD_LEFT" );
$firstpart=substr($binnet,0,$mask);
$binip=str_pad( decbin($lip),32,"0","STR_PAD_LEFT" );
$firstip=substr($binip,0,$mask);
return(strcmp($firstpart,$firstip)==0);
}
?>
This function can be compacted, avoiding some variable settings, but the function will not be too clear to read...
Example code, used to made a kind of location service network-based:
<?php
$n = array ( "192.168.0.0/16" => "TUSCANY",
"192.168.1.0/24" => "- Florence",
"192.168.2.0/24" => "- Pisa",
"192.168.3.0/24" => "- Siena",
"192.168.64.0/21" => "- Tuscan Archipelago",
"192.168.64.0/23" => "--- Elba Island",
"192.168.66.0/24" => "--- Capraia Island",
"192.168.67.0/24" => "--- Giannutri Island");
$myip = $HTTP_SERVER_VARS['REMOTE_ADDR'];
$myip = "192.168.2.33";
$myip = "192.168.65.34";
echo "Your position:<br />\n";
foreach ( $n as $k=>$v ) {
list($net,$mask)=split("/",$k);
if (isIPIn($myip,$net,$mask)) {
echo $n[$k]."<br />\n"; }
}
?>
and so on...
anderson at piq dot com dot br
06-Aug-2003 10:10
If you want to get the interface of an IP, based on the local route table, use this.
function GetIfaceforIP($user_ip)
{
$route = "/bin/netstat -rn";
exec($route, $aoutput);
foreach($aoutput as $key => $line)
{
if($key > 1)
{
$line = ereg_replace("[[:space:]]+",",",$line);
list($network, $gateway, $mask, $flags, $mss, $window, $irtt, $iface) = explode(",", $line)
if((ip2long($user_ip) & ip2long($mask)) == ip2long($network))
{
return $iface;
}
}
}
}
02-Apr-2003 08:11
Alternative cidr_conv function - a little easier to follow
function cidr_conv($cidr_address) {
$first = substr($cidr_address, 0, strpos($cidr_address, "/"));
$netmask = substr(strstr($cidr_address, "/"), 1);
$first_bin = str_pad(decbin(ip2long($first)), 32, "0", STR_PAD_LEFT);
$netmask_bin = str_pad(str_repeat("1", (integer)$netmask), 32, "0", STR_PAD_RIGHT);
for ($i = 0; $i < 32; $i++) {
if ($netmask_bin[$i] == "1")
$last_bin .= $first_bin[$i];
else
$last_bin .= "1";
}
$last = long2ip(bindec($last_bin));
return "$first - $last";
}
trevor-hemsley at nospam dot dial dot pipex dot com
16-Oct-2002 11: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)
{
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)
{
$s = explode(".", $istart);
$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;
}
?>
philippe-at-cyberabuse.org
12-Oct-2002 03: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";
}
philippe-at-cyberabuse.org
14-Dec-2001 07: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));
}
| |