downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

checkdnsrr> <ncurses_wvline
[edit] Last updated: Mon, 01 Nov 2010

view this page in

XCVIII. ネットワーク関数

要件

外部ライブラリを必要としません。

インストール手順

PHP コアに含まれるため、 追加のインストール無しで使用できます。

実行時設定

php.ini の設定により動作が変化します。

表 1. ネットワーク設定オプション(Network)

名前デフォルト変更の可否変更の履歴
define_syslog_variables"0"PHP_INI_ALL 
PHP_INI_* 定数の詳細および定義については 付録G を参照してください。

以下に設定ディレクティブに関する 簡単な説明を示します。

define_syslog_variables boolean

様々なsyslog変数(例: $LOG_PID、$LOG_CRON、等)を定義するかどうか。 これをoffにするのは、性能面では良い考えです。 実行時にdefine_syslog_variables()をコールする ことにより、これらを定義することができます。

リソース型

リソース型は定義されていません。

定義済み定数

以下の定数は、PHP コアに含まれており、常に利用可能です。

表 2. openlog()オプション

定数説明
LOG_CONS システムロガーにデータを送信する際にエラーが発生した場合、 システムコンソールに直接書き込む。
LOG_NDELAY ロガーにただちに接続をオープンする。
LOG_ODELAY (デフォルト) 最初のメッセージが記録されるまで接続のオープンを 遅延させる。
LOG_NOWAIT 
LOG_PERROR標準エラー出力にもログメッセージを出力する
LOG_PID各メッセージにプロセスIDを含める

表 3. openlog() ファシリティ

定数説明
LOG_AUTH セキュリティ/認証メッセージ(LOG_AUTHPRIVが定義されているシステムであれば 代わりにそれを使用してください)
LOG_AUTHPRIVセキュリティ/認証メッセージ (private)
LOG_CRON時刻デーモン (cron and at)
LOG_DAEMONその他のシステムデーモン
LOG_KERNカーネルメッセージ
LOG_LOCAL0 ... LOG_LOCAL7ローカルで使用できるようリザーブされている。Windowsでは使用できない。
LOG_LPRラインプリンタサブシステム
LOG_MAILメールサブシステム
LOG_NEWSUSENETニュースサブシステム
LOG_SYSLOGsyslogdによって内部で生成されたメッセージ
LOG_USER一般ユーザーレベルのメッセージ
LOG_UUCPUUCPサブシステム

表 4. syslog()プロパティ(降順)

定数説明
LOG_EMERGシステムは使用不可
LOG_ALERTアクションを直ちに起こすことが必要
LOG_CRIT危機的な条件
LOG_ERRエラー条件
LOG_WARNING警告条件
LOG_NOTICE正常、しかし、注意すべき条件
LOG_INFO情報メッセージ
LOG_DEBUGデバッグレベルメッセージ

表 5. dns_get_record()オプション

定数説明
DNS_AIPv4アドレスリソース
DNS_MXMail Exchangerリソース
DNS_CNAMEエイリアス(Canonical Name)リソース
DNS_NSAuthoritative Name Serverリソース
DNS_PTRポインタリソース
DNS_HINFOホスト情報リソース(これらの値の意味については、IANA の Operating System Names を参照ください)
DNS_SOA認証リソースの開始
DNS_TXTテキストリソース
DNS_ANY 全てのリソースレコード。多くのシステムでは、 これは、全てのリソースレコードを返します。 しかし、危機的な状況には対応できません。代わりに DNS_ALLを試してください。
DNS_AAAAIPv6アドレスリソース
DNS_ALL 利用可能なレコード型毎のネームサーバへの反復クエリ。
目次
checkdnsrr --  指定したインターネットホスト名もしくは IP アドレスに対応する DNS レコードを検索する
closelog -- システムログへの接続を閉じる
debugger_off -- PHPの内部デバッガを無効にする(PHP 3)
debugger_on -- PHPの内部デバッガを有効にする
define_syslog_variables -- syslogに関係する全ての定数を初期化する
dns_check_record -- checkdnsrr()と同義
dns_get_mx -- getmxrr()と同義
dns_get_record --  ホスト名に関連する DNS リソースレコードを取得する
fsockopen --  インターネットもしくはUNIXドメインのソケット接続をオープンする
gethostbyaddr --  指定したIPアドレスに対応するインターネットホスト名を取得する
gethostbyname --  インターネットホスト名に対応するIPアドレスを取得する
gethostbynamel --  指定したインターネットホスト名に対応するIPアドレスのリストを取得 する。
getmxrr --  指定したインターネットホスト名に対応するMXレコードを取得する
getprotobyname --  プロトコル名のプロトコル番号を得る
getprotobynumber -- プロトコル番号が指すプロトコル名を得る
getservbyname --  インターネットサービスおよびプロトコルが関連するポート番号を得る
getservbyport --  ポートおよびプロトコルに対応するインターネットサービスを得る
header -- 生の HTTP ヘッダを送信する
headers_list -- 送信した (もしくは送信される予定の) レスポンスヘッダの一覧を返す
headers_sent -- ヘッダが既に送信されているかどうかを調べる
inet_ntop --  パックされたインターネットアドレスを、人間が読める形式に変換する
inet_pton --  人間が読める形式の IP アドレスを、パックされた形式に変換する
ip2long --  (IPv4) インターネットプロトコルドット表記のアドレスを適当なアドレ スを有する文字列に変換する
long2ip --  (IPv4) インターネットアドレスをインターネット標準ドット表記に変換 します。
openlog -- システムログへの接続をオープンする
pfsockopen --  持続的な Internet または Unix ドメインソケット接続をオープンする
setcookie -- クッキーを送信する
setrawcookie -- 値を URL エンコードせずにクッキーを送信する
socket_get_status -- stream_get_meta_data() のエイリアス
socket_set_blocking -- stream_set_blocking() のエイリアス
socket_set_timeout -- stream_set_timeout() のエイリアス
syslog -- システムログのメッセージを生成する


checkdnsrr> <ncurses_wvline
[edit] Last updated: Mon, 01 Nov 2010
 
add a note add a note User Contributed Notes ネットワーク関数
eicto@NOSPAM 15-Mar-2012 10:37
That function return ip ranges
<?php

function iprange($ip,$mask=24,$return_array=FALSE) {
   
$corr=(pow(2,32)-1)-(pow(2,32-$mask)-1);
   
$first=ip2long($ip) & ($corr);
   
$length=pow(2,32-$mask)-1;
    if (!
$return_array) {
    return array(
       
'first'=>$first,
       
'size'=>$length+1,
       
'last'=>$first+$length,
       
'first_ip'=>long2ip($first),
       
'last_ip'=>long2ip($first+$length)
        );
    }
   
$ips=array();
    for (
$i=0;$i<=$length;$i++) {
       
$ips[]=long2ip($first+$i);
    }
    return
$ips;
}

$test1=iprange('192.168.5.12',18);
$test2=iprange('192.168.1.12',28,TRUE);
echo
print_r($test1,1)."\n".print_r($test2,1)."\n";
----------------
<?
Array
(
    [
first] => 3232235520
   
[size] => 16384
   
[last] => 3232251903
   
[first_ip] => 192.168.0.0
   
[last_ip] => 192.168.63.255
)

Array
(
    [
0] => 192.168.1.0
   
[1] => 192.168.1.1
   
[2] => 192.168.1.2
   
[3] => 192.168.1.3
   
[4] => 192.168.1.4
   
[5] => 192.168.1.5
   
[6] => 192.168.1.6
   
[7] => 192.168.1.7
   
[8] => 192.168.1.8
   
[9] => 192.168.1.9
   
[10] => 192.168.1.10
   
[11] => 192.168.1.11
   
[12] => 192.168.1.12
   
[13] => 192.168.1.13
   
[14] => 192.168.1.14
   
[15] => 192.168.1.15
)

?>
David GASTALDIN 21-Jun-2007 08:12
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;
}
claudiu at cnixs dot com 22-Apr-2007 02:52
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"); ?>
dand at ddchosting.com 08-Apr-2006 02:28
When I was working on a project I ran into this problem with redirecting. My solution is as follows:
header("Refresh: 5; url=../main/main.php?".session_id()."");

This allowed me to pass the session_id() which is used throughout site to make sure user has loged in.
I hope this helps!
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) {
  
//doesn't check for the return value of ip2long
  
$ip = ip2long($ip);
  
$rede = ip2long($net);
  
$mask = ip2long($mask);
 
  
//AND
  
$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

/**
 * @param string $ip IP to check in dotted decimal format
 * @param string $net Network IP in dotted decimal format
 * @param string $mask Netmask in dotted decimal format
 * @returns true if the ip belongs to the network, false otherwise
 **/
function isIPIn($ip, $net, $mask) {
   
//doesn't check for the return value of ip2long
   
$ip = ip2long($_POST['ip']);
   
$rede = ip2long($_POST['net']);
   
$mask = ip2long($_POST['mask']);
   
   
//AND
   
$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");

// Normally you should use the following line
$myip = $HTTP_SERVER_VARS['REMOTE_ADDR'];
// This is first example: returns Tuscany/Pisa
$myip = "192.168.2.33";
// This is second example: returns Tuscany/T.Arch./Elba
$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)
{
// 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;
}
?>
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));
}

 
show source | credits | sitemap | contact | advertising | mirror sites