0

I currently have a directory on my website of all possible IP addresses. Over the past year it's been pulling the information from a MySQL database, but obviously it's an enormous database to be pulling information from.

I was thinking that since all IP addresses are between 0.0.0.0 to 255.255.255.255 there should be a way to have the database automatically generated without using a database and just based on the page that the person is on.

Each page shows 1002 IP addresses, so the first page would should the first 1-1002 possible IP addresses, the second page would show 1003-2004 IP addresses.

Here's the code I'm currently using for the site:

<?php
    /*
        Place code to connect to your DB here.
    */

 mysql_connect("localhost", "user_name", "password") or die(mysql_error()); 
 mysql_select_db("data_base") or die(mysql_error()); 


    $tbl_name="domain_names";       //your table name
    // How many adjacent pages should be shown on each side?
    $adjacents = 5;

    /* 
       First get total number of rows in data table. 
       If you have a WHERE clause in your query, make sure you mirror it here.
    */
    $query = "SELECT COUNT(*) as num FROM $tbl_name";
    $total_pages = mysql_fetch_array(mysql_query($query));
    $total_pages = $total_pages[num];

    /* Setup vars for query. */
    $targetpage = "ip.php";   //your file name  (the name of this file)
    $limit = 1002;                              //how many items to show per page
    $page = $_GET['page'];
    if($page) 
        $start = ($page - 1) * $limit;          //first item to display on this page
    else
        $start = 0;                             //if no page var is given, set start to 0

    /* Get data. */
    $sql = "SELECT domains FROM $tbl_name LIMIT $start, $limit";
    $result = mysql_query($sql);

    /* Setup page vars for display. */
    if ($page == 0) $page = 1;                  //if no page var is given, default to 1.
    $prev = $page - 1;                          //previous page is page - 1
    $next = $page + 1;                          //next page is page + 1
    $lastpage = ceil($total_pages/$limit);      //lastpage is = total pages / items per page, rounded up.
    $lpm1 = $lastpage - 1;                      //last page minus 1

    /* 
        Now we apply our rules and draw the pagination object. 
        We're actually saving the code to a variable in case we want to draw it more than once.
    */
    $pagination = "";
    if($lastpage > 1)
    {   
        $pagination .= "<div class=\"pagination\">";
        //previous button
        if ($page > 1) 
            $pagination.= "<a href=\"$targetpage?page=$prev\">&lt; previous</a>";
        else
            $pagination.= "<span class=\"disabled\">&lt; previous</span>";  

        //pages 
        if ($lastpage < 7 + ($adjacents * 2))   //not enough pages to bother breaking it up
        {   
            for ($counter = 1; $counter <= $lastpage; $counter++)
            {
                if ($counter == $page)
                    $pagination.= "<span class=\"current\">$counter</span>";
                else
                    $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";                 
            }
        }
        elseif($lastpage > 5 + ($adjacents * 2))    //enough pages to hide some
        {
            //close to beginning; only hide later pages
            if($page < 1 + ($adjacents * 2))        
            {
                for ($counter = 1; $counter < 4 + ($adjacents * 2); $counter++)
                {
                    if ($counter == $page)
                        $pagination.= "<span class=\"current\">$counter</span>";
                    else
                        $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";                 
                }
                $pagination.= "...";
                $pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
                $pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";       
            }
            //in middle; hide some front and some back
            elseif($lastpage - ($adjacents * 2) > $page && $page > ($adjacents * 2))
            {
                $pagination.= "<a href=\"$targetpage?page=1\">1</a>";
                $pagination.= "<a href=\"$targetpage?page=2\">2</a>";
                $pagination.= "...";
                for ($counter = $page - $adjacents; $counter <= $page + $adjacents; $counter++)
                {
                    if ($counter == $page)
                        $pagination.= "<span class=\"current\">$counter</span>";
                    else
                        $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";                 
                }
                $pagination.= "...";
                $pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
                $pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";       
            }
            //close to end; only hide early pages
            else
            {
                $pagination.= "<a href=\"$targetpage?page=1\">1</a>";
                $pagination.= "<a href=\"$targetpage?page=2\">2</a>";
                $pagination.= "...";
                for ($counter = $lastpage - (2 + ($adjacents * 2)); $counter <= $lastpage; $counter++)
                {
                    if ($counter == $page)
                        $pagination.= "<span class=\"current\">$counter</span>";
                    else
                        $pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";                 
                }
            }
        }

        //next button
        if ($page < $counter - 1) 
            $pagination.= "<a href=\"$targetpage?page=$next\">next &gt;</a>";
        else
            $pagination.= "<span class=\"disabled\">next &gt;</span>";
        $pagination.= "</div>\n";       
    }
?>

    <?php
$i = 0;
echo '<table style="table-layout:fixed; width:95%;"><tr>'; 
        while($row = mysql_fetch_array($result))
{
    $i ++;
    if ($i<=3)
    {
      echo '<td style="word-wrap: break-word;">
         <div><center><a style="text-decoration:none;color:#489FDC;" href="http://ip.mysite.com/'.strtolower($row[domains]).'">'.strtolower($row[domains]).'</a></center></div>
       </td>'; 
    }

    else
    {       
      echo '</tr><tr>';
      echo '<td style="word-wrap: break-word;"><div><center><a style="text-decoration:none;color:#489FDC;" href="http://ip.mysite.com/'.strtolower($row[domains]).'">'.strtolower($row[domains]).'</a></center></div></td>'; 
      $i = 0;
    $i++;
   }
}  
echo '</tr></table><br />';
    ?>

<?=$pagination?>

Is what I'm thinking possible to do and is there an easy way that someone can think to do it? It seems like it should be as simple as removing any code related to the database and then basing the numbers off which page that someone is on...

Any help with this would be really appreciated.

user2970202
  • 377
  • 1
  • 7
  • 11
  • You're using an [obsolete API](http://php.net/manual/en/mysqlinfo.api.choosing.php) and using string interop to build queries, which is [risky](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php?rq=1). – Dan Nov 23 '13 at 16:40
  • I think that if you need to show pages of ipv4 addresses it is not optimal to store all of them in a db table, just calculate in php the number of addresses you want to show per page and show them. Then if you reserve the addresses or associate them with some information store only the ones that are reserver or associated with some info and accomodate this logic in your php code. For example ask from the db the persisted addresses within the range of addresses you show on a specific page and present the extra info. – melc Nov 23 '13 at 16:48

2 Answers2

1

IPv4 addresses are actually just 32bit integers. You could work with integers and use long2ip to convert them in 'ip format'.

You begin the page at number ([pagenumber] * [pagesize]), iterate over [pagesize] numbers and print them with long2ip.

Possible pitfalls: You should be ok on a 64bit system - on a 32bit system, you would have to handle 'negative' numbers. Depending on your use case, please also consider that not ALL possible addresses might actually be legit. There are the so-called private ranges (unrouted), multicast etc.

hpoeri
  • 36
  • 6
0

Something along these lines would make this easier.

$perPage = 1002;

$i = ($page - 1 ) * $perPage;
$end = $page * $perPage;


while($i < $end ) {

    $dotIP = long2ip((float)$i); 

    // Do something with $dotIP;
}
Matt R. Wilson
  • 7,268
  • 5
  • 32
  • 48