首頁 php教程 PHP源码 php版 子网掩码计算器

php版 子网掩码计算器

May 25, 2016 pm 05:07 PM

php代码

<!--
PHP Subnet Calculator v1.3.
Copyright 06/25/2003 Raymond Ferguson ferguson_at_share-foo.com.
Released under GNU GPL.
Special thanks to krischan at jodies.cx for ipcalc.pl http://jodies.de/ipcalcThe presentation and concept was mostly taken from ipcalc.pl.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
		"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <title>PHP Subnet Calculator</title>
  <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
  <meta name="GENERATOR" content="Quanta Plus">
</head>
<body bgcolor="#D3D3D3">
<center>
<form method="post" action="<?php print $_SERVER[&#39;PHP_SELF&#39;] ?> ">
<BR><BR>
<table width="95%" align=center cellpadding=2 cellspacing=2 border=0>
  <tr><td align="center" bgcolor="#999999">
     <b><A HREF="http://sourceforge.net/projects/subntcalc/">PHP Subnet Calculator</A></b>
  </td></tr>
</table>
<BR>
<table>
  <tr>
        <td>IP &amp; Mask or CIDR:   </td>
        <td><input type="text" name="my_net_info" value="" size="31" maxlength="32"></td>
        <td>    <input type="submit" value="Calculate" name="subnetcalc">
                         
             </td>
  </tr>
</table></form><br>

<?php
//Start table
print "<table cellpadding=\"2\">\n<COL span=\"4\" align=\"left\">\n" ;

$end=&#39;</table><br><br><br><br><br><table width="95%" align=center cellpadding=2 cellspacing=2 border=0>
      <tr><td bgcolor="#999999"></td><tr><td align="center"><a href="http://validator.w3.org/check/referer">
      <img border="0" src="http://www.w3.org/Icons/valid-html401" alt="Valid HTML 4.01!" height="31" width="88"></a>
      </td></tr></table></center></body></html>&#39;;

if (empty($_POST[&#39;my_net_info&#39;])){
	tr(&#39;Use IP & CIDR Netmask: &#39;, &#39;10.0.0.1/22&#39;);
	tr(&#39;Or IP & Netmask:&#39;,&#39;10.0.0.1 255.255.252.0&#39;);
	tr(&#39;Or IP & Wildcard Mask:&#39;,&#39;10.0.0.1 0.0.3.255&#39;);
	print $end ;
	exit ;
}

$my_net_info=rtrim($_POST[&#39;my_net_info&#39;]);


//if (! ereg(&#39;^([0-9]{1,3}\.){3}[0-9]{1,3}(( ([0-9]{1,3}\.){3}[0-9]{1,3})|(/[0-9]{1,2}))$&#39;,$my_net_info)){
if (! preg_match("/^([0-9]{1,3}\.){3}[0-9]{1,3}(( ([0-9]{1,3}\.){3}[0-9]{1,3})|(\/[0-9]{1,2}))$/",$my_net_info)){
	tr("Invalid Input.");
	tr(&#39;Use IP & CIDR Netmask: &#39;, &#39;10.0.0.1/22&#39;);
	tr(&#39;Or IP & Netmask:&#39;,&#39;10.0.0.1 255.255.252.0&#39;);
	tr(&#39;Or IP & Wildcard Mask:&#39;,&#39;10.0.0.1 0.0.3.255&#39;);
	print $end ;
	exit ;
}

//if (ereg("/",$my_net_info)){  //if cidr type mask
if (preg_match("/\//",$my_net_info)){  //if cidr type mask
	$dq_host = strtok("$my_net_info", "/");
	$cdr_nmask = strtok("/");
	if (!($cdr_nmask >= 0 && $cdr_nmask <= 32)){
		tr("Invalid CIDR value. Try an integer 0 - 32.");
		print "$end";
		exit ;
	}
	$bin_nmask=cdrtobin($cdr_nmask);
	$bin_wmask=binnmtowm($bin_nmask);
} else { //Dotted quad mask?
    $dqs=explode(" ", $my_net_info);
	$dq_host=$dqs[0];
	$bin_nmask=dqtobin($dqs[1]);
	$bin_wmask=binnmtowm($bin_nmask);
	//if (ereg("0",rtrim($bin_nmask, "0"))) {  //Wildcard mask then? hmm?
	if (preg_match("/0/",rtrim($bin_nmask, "0"))) {  //Wildcard mask then? hmm?
		$bin_wmask=dqtobin($dqs[1]);
		$bin_nmask=binwmtonm($bin_wmask);
		if (ereg("0",rtrim($bin_nmask, "0"))){ //If it&#39;s not wcard, whussup?
			tr("Invalid Netmask.");
			print "$end";
			exit ;
		}
	}
	$cdr_nmask=bintocdr($bin_nmask);
}

//Check for valid $dq_host
//if(! ereg(&#39;^0.&#39;,$dq_host)){
if(! preg_match("/^0\./",$dq_host)){
	foreach( explode(".",$dq_host) as $octet ){
 		if($octet > 255){ 
			tr("Invalid IP Address");
			print $end ;
			exit;
		}
	
	}
}

$bin_host=dqtobin($dq_host);
$bin_bcast=(str_pad(substr($bin_host,0,$cdr_nmask),32,1));
$bin_net=(str_pad(substr($bin_host,0,$cdr_nmask),32,0));
$bin_first=(str_pad(substr($bin_net,0,31),32,1));
$bin_last=(str_pad(substr($bin_bcast,0,31),32,0));
$host_total=(bindec(str_pad("",(32-$cdr_nmask),1)) - 1);

if ($host_total <= 0){  //Takes care of 31 and 32 bit masks.
	$bin_first="N/A" ; $bin_last="N/A" ; $host_total="N/A";
	if ($bin_net === $bin_bcast) $bin_bcast="N/A";
}

//Determine Class
//if (ereg(&#39;^0&#39;,$bin_net)){
if (preg_match("/^0/",$bin_net)){
	$class="A";
	$dotbin_net= "<font color=\"Green\">0</font>" . substr(dotbin($bin_net,$cdr_nmask),1) ;
//}elseif (ereg(&#39;^10&#39;,$bin_net)){
}elseif (preg_match("/^10/",$bin_net)){
	$class="B";
	$dotbin_net= "<font color=\"Green\">10</font>" . substr(dotbin($bin_net,$cdr_nmask),2) ;
//}elseif (ereg(&#39;^110&#39;,$bin_net)){
}elseif (preg_match("/^110/",$bin_net)){
  	$class="C";
	$dotbin_net= "<font color=\"Green\">110</font>" . substr(dotbin($bin_net,$cdr_nmask),3) ;
//}elseif (ereg(&#39;^1110&#39;,$bin_net)){
}elseif (preg_match("/^1110/",$bin_net)){
  	$class="D";
	$dotbin_net= "<font color=\"Green\">1110</font>" . substr(dotbin($bin_net,$cdr_nmask),4) ;
	$special="<font color=\"Green\">Class D = Multicast Address Space.</font>";
}else{
  	$class="E";
	$dotbin_net= "<font color=\"Green\">1111</font>" . substr(dotbin($bin_net,$cdr_nmask),4) ;
	$special="<font color=\"Green\">Class E = Experimental Address Space.</font>";
}

//if (ereg(&#39;^(00001010)|(101011000001)|(1100000010101000)&#39;,$bin_net)){
if (preg_match("/^(00001010)|(101011000001)|(1100000010101000)/",$bin_net)){
  	 $special=&#39;<a href="http://www.ietf.org/rfc/rfc1918.txt">( RFC-1918 Private Internet Address. )</a>&#39;;
}

// Print Results
tr(&#39;Address:&#39;,"<font color=\"blue\">$dq_host</font>",
	&#39;<font color="brown">&#39;.dotbin($bin_host,$cdr_nmask).&#39;</font>&#39;);
tr(&#39;Netmask:&#39;,&#39;<font color="blue">&#39;.bintodq($bin_nmask)." = $cdr_nmask</font>",
	&#39;<font color="red">&#39;.dotbin($bin_nmask, $cdr_nmask).&#39;</font>&#39;);
tr(&#39;Wildcard:&#39;, &#39;<font color="blue">&#39;.bintodq($bin_wmask).&#39;</font>&#39;,
	&#39;<font color="brown">&#39;.dotbin($bin_wmask, $cdr_nmask).&#39;</font>&#39;);
tr(&#39;Network:&#39;, &#39;<font color="blue">&#39;.bintodq($bin_net).&#39;</font>&#39;,
	"<font color=\"brown\">$dotbin_net</font>","<font color=\"Green\">(Class $class)</font>");
tr(&#39;Broadcast:&#39;,&#39;<font color="blue">&#39;.bintodq($bin_bcast).&#39;</font>&#39;,
	&#39;<font color="brown">&#39;.dotbin($bin_bcast, $cdr_nmask).&#39;</font>&#39;);
tr(&#39;HostMin:&#39;, &#39;<font color="blue">&#39;.bintodq($bin_first).&#39;</font>&#39;,
	&#39;<font color="brown">&#39;.dotbin($bin_first, $cdr_nmask).&#39;</font>&#39;);
tr(&#39;HostMax:&#39;, &#39;<font color="blue">&#39;.bintodq($bin_last).&#39;</font>&#39;,
	&#39;<font color="brown">&#39;.dotbin($bin_last, $cdr_nmask).&#39;</font>&#39;);
@tr(&#39;Hosts/Net:&#39;, &#39;<font color="blue">&#39;.$host_total.&#39;</font>&#39;, "$special");
print "$end";

function binnmtowm($binin){
	$binin=rtrim($binin, "0");
	//if (!ereg("0",$binin) ){
	if (!preg_match("/0/",$binin) ){
		return str_pad(str_replace("1","0",$binin), 32, "1");
	} else return "1010101010101010101010101010101010101010";
}

function bintocdr ($binin){
	return strlen(rtrim($binin,"0"));
}

function bintodq ($binin) {
	if ($binin=="N/A") return $binin;
	$binin=explode(".", chunk_split($binin,8,"."));
	for ($i=0; $i<4 ; $i++) {
		$dq[$i]=bindec($binin[$i]);
	}
        return implode(".",$dq) ;
}

function bintoint ($binin){
        return bindec($binin);
}

function binwmtonm($binin){
	$binin=rtrim($binin, "1");
	//if (!ereg("1",$binin)){
	if (!preg_match("/1/",$binin)){
		return str_pad(str_replace("0","1",$binin), 32, "0");
	} else return "1010101010101010101010101010101010101010";
}

function cdrtobin ($cdrin){
	return str_pad(str_pad("", $cdrin, "1"), 32, "0");
}

function dotbin($binin,$cdr_nmask){
	// splits 32 bit bin into dotted bin octets
	if ($binin=="N/A") return $binin;
	$oct=rtrim(chunk_split($binin,8,"."),".");
	if ($cdr_nmask > 0){
		$offset=sprintf("%u",$cdr_nmask/8) + $cdr_nmask ;
		return substr($oct,0,$offset ) . "   " . substr($oct,$offset) ;
	} else {
	return $oct;
	}
}

function dqtobin($dqin) {
        $dq = explode(".",$dqin);
        for ($i=0; $i<4 ; $i++) {
           $bin[$i]=str_pad(decbin($dq[$i]), 8, "0", STR_PAD_LEFT);
        }
        return implode("",$bin);
}

function inttobin ($intin) {
        return str_pad(decbin($intin), 32, "0", STR_PAD_LEFT);
}

function tr(){
	echo "\t<tr>";
	for($i=0; $i<func_num_args(); $i++) echo "<td>".func_get_arg($i)."</td>";
	echo "</tr>\n";
}
?>
登入後複製
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1664
14
CakePHP 教程
1423
52
Laravel 教程
1318
25
PHP教程
1268
29
C# 教程
1248
24