LXXXVIII. Network Functions




本函数库作为 PHP 内核的一部分,不用安装就能使用。


这些函数的行为受 php.ini 的影响。

表格 1. Network Configuration Options

有关 PHP_INI_* 常量进一步的细节与定义参见附录 G


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

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_PERRORprint log message also to standard error
LOG_PIDinclude PID with each message

表格 3. openlog() Facilities

LOG_AUTH security/authorization messages (use LOG_AUTHPRIV instead in systems where that constant is defined)
LOG_AUTHPRIVsecurity/authorization messages (private)
LOG_CRONclock daemon (cron and at)
LOG_DAEMONother system daemons
LOG_KERNkernel messages
LOG_LOCAL0 ... LOG_LOCAL7reserved for local use, these are not available in Windows
LOG_LPRline printer subsystem
LOG_MAILmail subsystem
LOG_NEWSUSENET news subsystem
LOG_SYSLOGmessages generated internally by syslogd
LOG_USERgeneric user-level messages
LOG_UUCPUUCP subsystem

表格 4. syslog() Priorities (in descending order)

LOG_EMERGsystem is unusable
LOG_ALERTaction must be taken immediately
LOG_CRITcritical conditions
LOG_ERRerror conditions
LOG_WARNINGwarning conditions
LOG_NOTICEnormal, but significant, condition
LOG_INFOinformational message
LOG_DEBUGdebug-level message

表格 5. dns_get_record() Options

DNS_AIPv4 Address Resource
DNS_MXMail Exchanger Resource
DNS_CNAMEAlias (Canonical Name) Resource
DNS_NSAuthoritative Name Server Resource
DNS_PTRPointer Resource
DNS_HINFOHost Info Resource (See IANA's Operating System Names for the meaning of these values)
DNS_SOAStart of Authority Resource
DNS_TXTText Resource
DNS_ANYAny 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_AAAAIPv6 Address Resource
DNS_ALLIteratively query the name server for each available record type.
checkdnsrr --  Check DNS records corresponding to a given Internet host name or IP address
closelog -- Close connection to system logger
debugger_off -- Disable internal PHP debugger (PHP 3)
debugger_on -- Enable internal PHP debugger (PHP 3)
define_syslog_variables -- Initializes all syslog related constants
dns_check_record -- Synonym for checkdnsrr()
dns_get_mx -- Synonym for 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
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
socket_get_status -- 别名 stream_get_meta_data()
socket_set_blocking -- 别名 stream_set_blocking()
socket_set_timeout -- 别名 stream_set_timeout()
syslog -- Generate a system log message

add a note add a note User Contributed Notes
dand at ddchosting.com
09-Apr-2006 05: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 10: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);
$res = $ip & $mask;
   return (
$res == $rede);
metator at netcabo dot pt
28-Jul-2005 11: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: = 11000000.10101000.00000000.00010000
& = 11111111.11111111.11111111.00000000
-------------------------------------------------------------- = 11000000.10101000.00000000.00000000

And now the code. My example uses a html form where you place the values you want to test:

   <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!">

 * @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']);
$res = $ip & $mask;
   return (
$res == $rede);

if (isset(
$_POST['check'])) {
isIPIn($_POST['ip'], $_POST['net'], $_POST['mask']) ? "IP IN.": "IP OUT.";
nexxer at rogers dot com
25-Feb-2005 09: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("", "").

-- nex
samuele at norsam dot org
26-Nov-2003 01:01
To find if an IP is in a net/mask (very fast):
function isIPIn($ip,$net,$mask) {
$binnet=str_pad( decbin($lnet),32,"0","STR_PAD_LEFT" );
$binip=str_pad( decbin($lip),32,"0","STR_PAD_LEFT" );

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:

= array ( ""  => "TUSCANY",
""  => "- Florence",
""  => "- Pisa",
""  => "- Siena",
"" => "- Tuscan Archipelago",
"" => "--- Elba Island",
"" => "--- Capraia Island",
"" => "--- Giannutri Island");

// Normally you should use the following line
// This is first example: returns Tuscany/Pisa
$myip = "";
// This is second example: returns Tuscany/T.Arch./Elba
$myip = "";

"Your position:<br />\n";
foreach (
$n as $k=>$v ) {
   if (
isIPIn($myip,$net,$mask)) {
$n[$k]."<br />\n"; }

and so on...
null at tty dot net dot ru
13-Oct-2003 07:51
Ported Net::Netmask perl module:
anderson at piq dot com dot br
07-Aug-2003 01: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;
03-Apr-2003 12: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];
     $last_bin .= "1";

  $last = long2ip(bindec($last_bin));

  return "$first - $last";
trevor-hemsley at nospam dot dial dot pipex dot com
17-Oct-2002 02: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);

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

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;
$start += pow(2, (32-$maxsize));
13-Oct-2002 06:49
... and this one will do the opposite (o return NULL for invalid netblocks) : -> -> ->

function ip2cidr($ip_start,$ip_end) {
if(long2ip(ip2long($ip_start))!=$ip_start or long2ip(ip2long($ip_end))!=$ip_end) return NULL;
if($ipl_start>0 && $ipl_end<0) $delta=($ipl_end+4294967296)-$ipl_start;
else $delta=$ipl_end-$ipl_start;
if(ip2long($ip_start)==0 && substr_count($netmask,"1")==32) return "";
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";
15-Dec-2001 11:46
PHP miss CIDR functions.

This one will convert a CIDR like this: -> -
127.0/16 -> -

function cidrconv($net) {
$n=3-substr_count($net, ".");
if ($n>0) { for ($i=$n;$i>0;$i--) $start.=".0"; }
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));
philip at cornado dot c()m
05-Jul-2001 05:20
More socket functions can be seen here :