Search code examples
phpip-addressipv4

How I can add IP Address From start to end to Database?


I want to add IP address from start to end to database such as

IP Address (Start) 192.168.0.0
IP Address (End) 192.168.0.10

So it should add to database like

192.168.0.1
192.168.0.2
192.168.0.3
192.168.0.4
192.168.0.5
192.168.0.6
192.168.0.7
192.168.0.8
192.168.0.9
192.168.0.10

Thanks so much I have no idea.


Solution

  • Quick and dirty, it will take care if the range spreads across octets

    $start = '192.168.0.1';
    $end = '192.168.1.255';
    
    $startint = ip2long($start);
    $endint = ip2long($end);
    
    while ($startint <= $endint) {
     echo long2ip($startint++); // replace echo with your DB insert
    }