« Previous entry | Next entry » Browse > Snippets

Skip to comments (7) CIDR Classless Inter-Domain Routing
Posted by TheJohnDoe2005 on Oct 20 2005 @ 10:16  :: 3394 unique visits

PHP:CIDR Classless Inter-Domain Routing


Sample Image of the Program

http://www.freephotopost.com/uploads/f3dd57f252.png

Description
Nothing much, just a very simple script that generates out the CIDR Block Prefix from /1 to /32 showing the totally number of host addresses for each block. Also taking 0.0.0.0 as the default, it also shows the range in IP Address.

Installation
Step 1
- Copy the code below and paste it to your fave text editor. Save or rename the file with a .php extension and run it with your web server.

Information
Programing Type : Web Application
Programing Lang : Php / Html
Software Needed : Apache Server 2.0.55 / PHP 5.05

Code Difficulty Rating
Level : Beginner

About
Coded by : TheJohnDoe2005
Dated Created : 20th October 2005
Email Address : Email Me

Thanks and Credit
Thank you CodePost for hosting such a wonderful site.

METHOD 1 CODE
The reason why i wrote in this format is because i wanted the output html code to look cluttered together.

CODE: PHP
<?php
    $StartClass = 1/512;
    $StartHost  = 1/2;
    $StartClassDemoinator = array(32=>256, 31=>128, 30=>64, 29=>32, 28=>16, 27=>8, 26=>4, 25=>2);

    $table .= "<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" ".
          ""http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> " .
          "<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> " .
          "<head><title>CIDR - Classless Inter-Domain Routing</title> " .
          "<meta http-equiv="content-language" content="en-us" /> " .
          "<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /> ".
          "<meta name="Description" content="TheJohnDoe Production © | Better To Be Unknown Then Known" /> " .
          "<meta name="Author" content="The JohnDoe2005 . ©Copyright 1998 - 2005" /> " .
          "<style type="text/css">img {border:0px}</style> " .
          "</head><body>";
    $table .= "<table cellspacing="0" cellpadding="0" border="0" width="90%">";
    $table .= "<tr><th>CIDR Block Prefix</th><th>Number of Equivalent Class C</th>" .
          "<th>Number of Host Addresses</th><th>Start Address</th><th>End Address</th></tr>";
   
    for($i=32; $i>=1; $i--)
    {
        $temp1 = $StartClass * 2;
        $temp2 = $StartHost * 2;
        $table .= "<tr>";
        $table .= "<td align="center">/$i</td>";

        if($temp1 < 1)
            $table .= "<td align="center">1/$StartClassDemoinator[$i]</td>";
        else
            $table .= "<td align="center">$temp1</td>";

        $table .= "<td align="center">$temp2</td>";
        $table .= "<td align="right">0.0.0.0--</td>";
        $table .= "<td align="left">" . long2ip($temp2) . "</td>";
        $table .= "</tr>";
        $StartClass = $temp1;
        $StartHost  = $temp2;
    }

    $table .= "</table><p>Created By TheJohnDoe2005. Dated 20th October 2005</p> " .
              "<p><a href="http://validator.w3.org/check?uri=referer"> " .
              "<img src="http://www.w3.org/Icons/valid-xhtml10"alt="Valid XHTML 1.0 Strict" height="31" width="88" /> " .
              "</a></p></body></html>";
    echo $table;
?>


METHOD 1 OUTPUT SOURCE
This is the output where if I use Method 1. I don't have to worry about how the structure of the html will look like. It will always look neat and tidy. But there are some

Disadvantages

- It is frustrating in writing the code while special entites for example a quotation mark (") needs a backslash (/") input in order for the code to run.

- It is also very time consuming if you are not very proficient in your html as you import the codes from your html editor (ie Microsoft FontPage).

- Debugging of source would be harder to read as there are some many backslashes. (In fact you could avoid using alot of quotation mark(") if you are not bothered in writing the code in compliance with XHTML strict.)

- Viewing of output source for debugging is harder as the codes are cluttered together.

Advantages

- It is definitely neat and tidy. Codes are chunk Together.

- Makes it harder for those who have little knowledge of html to read the code as it is not structured. (Well, structured or not structured, you could copy the damn code to Microsoft Fontpage and click on the "Reformat Html", it will automatically structure it back for you.)

- Reduced file size as there is little empty spaces. Method 1 (6KB) compared to Method 2 (8KB). This could save alot if your codes get bigger.

1. Output of Method 1 SOURCE
http://www.freephotopost.com/uploads/647002261e.png

METHOD 2 CODE
This is the initial code that i have wrote before method 1. There is no need to worry about the backlashes.

CODE: PHP
<?php
    $StartClass = 1/512;
    $StartHost  = 1/2;
    $StartClassDemoinator = array(32=>256, 31=>128, 30=>64, 29=>32, 28=>16, 27=>8, 26=>4, 25=>2);

    $table .= <<< EOD
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>CIDR - Classless Inter-Domain Routing</title>
<meta http-equiv="content-language" content="en-us" />
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<meta name="Description" content="TheJohnDoe Production © | Better To Be Unknown Then Known" />
<meta name="Author" content="The JohnDoe2005 . ©Copyright 1998 - 2005" />
<style type="text/css">img {border:0px}</style>
</head>
<body>
    <table cellspacing="0" cellpadding="0" border="0" width="90%">
        <tr>
            <th>CIDR Block Prefix</th><th>Number of Equivalent Class C</th>
            <th>Number of Host Addresses</th><th>Start Address</th>
            <th>End Address</th>
        </tr>
EOD;
   
    for($i=32; $i>=1; $i--)
    {
        $temp1 = $StartClass * 2;
        $temp2 = $StartHost * 2;

        $table .= <<< EOD
       
        <tr>
            <td align="center">/$i</td>
EOD;

        if($temp1 < 1)
        {
            $table .= <<< EOD

            <td align="center">1/$StartClassDemoinator[$i]</td>
EOD;
        }
        else
        {
            $table .= <<< EOD

            <td align="center">$temp1</td>
EOD;
        }

        $long = long2ip($temp2);
        $table .= <<< EOD
       
            <td align="center">$temp2</td>
            <td align="right">0.0.0.0--$long</td>
            <td align="left"></td>
        </tr>
EOD;
        $StartClass = $temp1;
        $StartHost  = $temp2;
    }

    $table .= <<< EOD
   
    </table>
    <p>Created By TheJohnDoe2005. Dated 20th October 2005</p>
        <p><a href="http://validator.w3.org/check?uri=referer">
    <img src="http://www.w3.org/Icons/valid-xhtml10"alt="Valid XHTML 1.0 Strict" height="31" width="88" />
        </a></p>
</body>
</html>
EOD;
    echo $table;
?>


METHOD 2 OUTPUT SOURCE
Structure is there. Kinda neat.

2. Output of Method 2 SOURCE
http://www.freephotopost.com/uploads/411758dc8b.png

Conclusion
At the end of the day, its up to the programmer personal preference of choice to write the code depending on which output he wants. They all still ended up with the same output but the only differences is how it is display in the source part.

7 comments posted so far
Add your own »

1. On Oct 20 2005 @ 20:44 guest wrote:

$table .= "<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" ".

You can use ' instead of " at the openings and closings so you don't have to use those slashes in the html code.

2. On Oct 20 2005 @ 21:21 Erik wrote:

'' is also faster than "" because '$var' doesn't work so the compilers doesn't have to check strings with '' for variables.

3. On Oct 21 2005 @ 02:32 guest wrote:

The difference between ' and " is very small, for something like this I wouldn't be bothered about it (however not having to escape things is nice).

Why haven't you placed the header and footer HTML outside the PHP block?  Isn't that the whole idea of PHP?  :)

4. On Oct 21 2005 @ 08:48 TheJohnDoe2005 wrote:

"Why haven't you placed the header and footer HTML outside the PHP block?  Isn't that the whole idea of PHP?  :)"
quote from guest written on Oct 21 2005 @ 02:32

Well you could do that, but the output wouldn't look nice if for example the code between the header and the footer are not able to generate out and you  just get the top and the bottom displaying instead without the body....that wouldn't be nice at all. Viewer will get to view some distorted tables and paragraph of text over stretch as the code is not completed or the data are missing.

My policy is that either you display as a whole or don't display at all. I could add codes in between and check if the data are completely available before displaying them out. If you put outside the PHP code, you might not be able to do that as the header is already generated out before the php block. Whether the php block succeed or not, the footer will still generate.

Progammers are just like fashion designer, each individual got their own styles of writing and nobody can judge them right or wrong. But this only applies if your code produces an output that is satisfactory or correct.

5. On Oct 21 2005 @ 09:22 Buzzard wrote:

I see where you are coming from (all or nothing approach) esp. since PHP doesn't make it very easy handling errors.  Using heredoc makes it look better, so I'll stop complaining :-)

6. On Apr 14 2009 @ 13:32 guest wrote:

Find information about tiffany ,gucci ,chanel and other jewelry online shopping at online shopping ,
jewelry,craft,antique,daily news online collection at Online Collector ,Tiffany Jewelry including Tiffany Necklaces,Tiffany Rings, and tiffany bracelets…
Guide To Buy Tiffany Product ,   fashion jewelry provide,Tiffany,Oxette,Swarovski,CHANEL Jewelry Information
Find the discount gucci shoes



Louis Vuitton is luxury gifts, French fashion, the replica Louis Vuitton Handbag is woman best friend.Monogram Groom.
Offers Discount Louis Vuitton handbags and Louis Vuitton bags and all other designer handbags,free global fast shipping,low price and top quality.Monogram Jokes,Monogram Suede cheap Louis Vuitton
Louis Vuitton.

Looking For Gucci Shoes ? Gucci Store provide gucci Mens shoes,gucci Womens shoes
Wonderful Gucci shoes sale Gucci men's shoes and Gucci women's shoes at discount Gucci Shoes prices.
cheap gucci Shoes
Gucci Shoes and gucci clothing Spring - Summer 2009, Prada Shoes and prada clothing from the Latest Collection 2009 and Dolce Gabbana Clothing 2009
Gucci Loafers
Gucci Sneakers

Louis Vuitton Handbags
UGGs
Louis Vuitton Handbags
Gucci Shoes
Louis Vuitton
UGG Boots
Louis Vuitton Handbags
gucci shoes
Monogram Groom
Discount Louis Vuitton
UGG Boots

Add a new comment

Name:
Password: (leave empty for anonymous comment)
 
View formatting tags Comment: