Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Network Functions

Network Functions

Introduction

Requirements

No external libraries are needed to build this extension.

Installation

There is no installation needed to use these functions; they are part of the PHP core.

Runtime Configuration

The behaviour of these functions is affected by settings in php.ini.

Table 198. Network Configuration Options

Name Default Changeable Changelog
define_syslog_variables "0" PHP_INI_ALL  


For further details and definitions of the PHP_INI_* constants, see the Appendix I, php.ini directives.

Here's a short explanation of the configuration directives.

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().

Resource Types

This extension defines a file pointer resource returned by fsockopen() and pfsockopen().

Predefined Constants

The constants below are always available as part of the PHP core.

Table 199. 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


Table 200. 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


Table 201. 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


Table 202. 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.


Table of Contents

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 — Alias of checkdnsrr()
dns_get_mx — Alias of 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
header — Send a raw HTTP header
headers_list — Returns a list of response headers sent (or ready to send)
headers_sent — Checks if or where headers have been sent
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
setcookie — Send a cookie
setrawcookie — Send a cookie without urlencoding the cookie value
socket_get_status — Alias of stream_get_meta_data()
socket_set_blocking — Alias of stream_set_blocking()
socket_set_timeout — Alias of stream_set_timeout()
syslog — Generate a system log message

Code Examples / Notes » ref.network

dand

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

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


samuele

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...


metator

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">
Local network ip: <input type="text" name="net">
Local netmask: <input type="text" name="mask">
<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>


trevor-hemsley

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


null

Ported Net::Netmask perl module:
http://null.pp.ru/src/php/Netmask.phps


philippe-at-cyberabuse.org

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


philip

More socket functions can be seen here :
http://www.php.net/manual/en/ref.sockets.php


nexxer

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


anderson

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


david gastaldin

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


03-apr-2003 05: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";
}


claudiu

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"); ?>


philippe-at-cyberabuse.org

... 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";
}


Change Language


Follow Navioo On Twitter
.NET Functions
Apache-specific Functions
Alternative PHP Cache
Advanced PHP debugger
Array Functions
Aspell functions [deprecated]
BBCode Functions
BCMath Arbitrary Precision Mathematics Functions
PHP bytecode Compiler
Bzip2 Compression Functions
Calendar Functions
CCVS API Functions [deprecated]
Class/Object Functions
Classkit Functions
ClibPDF Functions [deprecated]
COM and .Net (Windows)
Crack Functions
Character Type Functions
CURL
Cybercash Payment Functions
Credit Mutuel CyberMUT functions
Cyrus IMAP administration Functions
Date and Time Functions
DB++ Functions
Database (dbm-style) Abstraction Layer Functions
dBase Functions
DBM Functions [deprecated]
dbx Functions
Direct IO Functions
Directory Functions
DOM Functions
DOM XML Functions
enchant Functions
Error Handling and Logging Functions
Exif Functions
Expect Functions
File Alteration Monitor Functions
Forms Data Format Functions
Fileinfo Functions
filePro Functions
Filesystem Functions
Filter Functions
Firebird/InterBase Functions
Firebird/Interbase Functions (PDO_FIREBIRD)
FriBiDi Functions
FrontBase Functions
FTP Functions
Function Handling Functions
GeoIP Functions
Gettext Functions
GMP Functions
gnupg Functions
Net_Gopher
Haru PDF Functions
hash Functions
HTTP
Hyperwave Functions
Hyperwave API Functions
i18n Functions
IBM Functions (PDO_IBM)
IBM DB2
iconv Functions
ID3 Functions
IIS Administration Functions
Image Functions
Imagick Image Library
IMAP
Informix Functions
Informix Functions (PDO_INFORMIX)
Ingres II Functions
IRC Gateway Functions
PHP / Java Integration
JSON Functions
KADM5
LDAP Functions
libxml Functions
Lotus Notes Functions
LZF Functions
Mail Functions
Mailparse Functions
Mathematical Functions
MaxDB PHP Extension
MCAL Functions
Mcrypt Encryption Functions
MCVE (Monetra) Payment Functions
Memcache Functions
Mhash Functions
Mimetype Functions
Ming functions for Flash
Miscellaneous Functions
mnoGoSearch Functions
Microsoft SQL Server Functions
Microsoft SQL Server and Sybase Functions (PDO_DBLIB)
Mohawk Software Session Handler Functions
mSQL Functions
Multibyte String Functions
muscat Functions
MySQL Functions
MySQL Functions (PDO_MYSQL)
MySQL Improved Extension
Ncurses Terminal Screen Control Functions
Network Functions
Newt Functions
NSAPI-specific Functions
Object Aggregation/Composition Functions
Object property and method call overloading
Oracle Functions
ODBC Functions (Unified)
ODBC and DB2 Functions (PDO_ODBC)
oggvorbis
OpenAL Audio Bindings
OpenSSL Functions
Oracle Functions [deprecated]
Oracle Functions (PDO_OCI)
Output Control Functions
Ovrimos SQL Functions
Paradox File Access
Parsekit Functions
Process Control Functions
Regular Expression Functions (Perl-Compatible)
PDF Functions
PDO Functions
Phar archive stream and classes
PHP Options&Information
POSIX Functions
Regular Expression Functions (POSIX Extended)
PostgreSQL Functions
PostgreSQL Functions (PDO_PGSQL)
Printer Functions
Program Execution Functions
PostScript document creation
Pspell Functions
qtdom Functions
Radius
Rar Functions
GNU Readline
GNU Recode Functions
RPM Header Reading Functions
runkit Functions
SAM - Simple Asynchronous Messaging
Satellite CORBA client extension [deprecated]
SCA Functions
SDO Functions
SDO XML Data Access Service Functions
SDO Relational Data Access Service Functions
Semaphore
SESAM Database Functions
PostgreSQL Session Save Handler
Session Handling Functions
Shared Memory Functions
SimpleXML functions
SNMP Functions
SOAP Functions
Socket Functions
Standard PHP Library (SPL) Functions
SQLite Functions
SQLite Functions (PDO_SQLITE)
Secure Shell2 Functions
Statistics Functions
Stream Functions
String Functions
Subversion Functions
Shockwave Flash Functions
Swish Functions
Sybase Functions
TCP Wrappers Functions
Tidy Functions
Tokenizer Functions
Unicode Functions
URL Functions
Variable Handling Functions
Verisign Payflow Pro Functions
vpopmail Functions
W32api Functions
WDDX Functions
win32ps Functions
win32service Functions
xattr Functions
xdiff Functions
XML Parser Functions
XML-RPC Functions
XMLReader functions
XMLWriter Functions
XSL functions
XSLT Functions
YAZ Functions
YP/NIS Functions
Zip File Functions
Zlib Compression Functions
eXTReMe Tracker