Add all IP Address to MySQL Database – PHP Script

Internet Protocol is known as IP and one day all device will be controlled  with IP address. I tried to add all IPV4 IP address into MySQL database with php script and yes, I  did it. But, it takes a lot of space! I added IP address to MySQL database from 1.0.0.0 to 1.1.255.255 and it takes 300MB + space! Anyway if you need to print all IP address or y0u wish to add them into MySQL database, follow the following  simple php script.

Well, First, We will see how to print ip address


<?php 
echo "IP address <br>";

for ($l = 1; $l <= 255; $l++) {
  for ($k = 0; $k <= 255; $k++) {
	  for ($j = 0; $j <= 255; $j++) {
   		 for ($i = 0; $i <= 255; $i++) {
                  echo "$l.$k.$j.$i <br>";
                  $count+=1;
                 }
         }
   }
}
echo "Total IP $count";
?>

We use 4 loop to print ip address. But there will be a problem as it is set  for 1.0.0.0 to 255.255.255.255. The server will not response after one minute (60 second ) and stop working and print IPs from  1.0.0.0 to 1.4.103.172 , Total 288685  and also


Fatal error: Maximum execution time of 60 seconds exceeded in C:\xampp\htdocs\ip\i.php on line 11

(the project was tested with local server xampp)


It may varies depending on your server.  So, What to do? Change the initial and conditional value of loop and I think you will do it. It will work. For example follow the following

<?php 
echo "IP address <br>";

for ($l = 1; $l <= 1; $l++) {
  for ($k = 0; $k <= 1; $k++) {
	  for ($j = 0; $j <= 255; $j++) {
   		 for ($i = 0; $i <= 255; $i++) {
                  echo "$l.$k.$j.$i <br>";
                  $count+=1;

                 }
         }
   }
}
echo "<br> Total $count<br>";
?>

It will print 1.0.0.0 to 1.1.255.255, Total 131072.

Well, now we will see how to add ip into Database. It's easy... First create a Database say " IPAddress " and then create a table named " ipaddress " into it with the field " id " and " ip ". I do this project on my local server. And the code is following

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Add ip to Database</title>
<script type="text/javascript">
function delayer(){

<?php  
$db_name = "IPAddress";
$table_name = "ipaddress";
//connect to MySQL and select database to use
$connection = @mysql_connect("localhost", "root", "")
or die(mysql_error());
$db = @mysql_select_db($db_name, $connection) or die(mysql_error());

$count=0;
$iv=$_GET['i'];
$jv=$_GET['j'];
$kv=$_GET['k'];
$lv=$_GET['l'];

if($iv=="")
$iv=0;
if($jv=="")
$jv=0;
if($kv=="")
$kv=0;
if($lv=="")
$lv=1;
for ($l = $lv; $l <= 255; $l++) {
  for ($k = $kv; $k <= 255; $k++) {

	  for ($j = $jv; $j <= 255; $j++) {

   		 for ($i = $iv; $i <= 255; $i++) {
			 $count+=1;
    			$ip= "$l.$k.$j.$i ";
				$sql = "INSERT INTO $table_name
				(ip) VALUES ('$ip')";
				$result = @mysql_query($sql,$connection) or die("Database Connection Error");

			}
			 if($j>=255)
				{
					$k+=1;
					$host  = $_SERVER['HTTP_HOST'];
					$uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
					$extra = "ip.php?k=$k&l=$l";
					$newurl="http://$host$uri/$extra";

					echo"window.location = \"$newurl\"
					}

					</script>

					</head>

					<body onLoad=\"setTimeout('delayer()', 1000)\">
					<h2>Prepare to be redirected!</h2>
					<p>Page will redirect to <a href=\"$newurl\">$newurl</a>. If not, Click to the link</p>

					</body>
					</html>";

					exit;

					}

		}		if($k>=255)
				{
					$l+=1;
					$host  = $_SERVER['HTTP_HOST'];
					$uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
					$extra = "ip.php?l=$l";
					$newurl="http://$host$uri/$extra";

					echo"window.location = \"$newurl\"
					}

					</script>

					</head>

					<body onLoad=\"setTimeout('delayer()', 6000)\">
					<h2>Prepare to be redirected!</h2>
					<p>Page will redirect to <a href=\"$newurl\">$newurl</a>. If not, Click to the link</p>

					</body>
					</html>";

					exit;

					}

	}  

}

?>

I added Some JavaScript code and it will auto load after few moment. I did it to avoid the Fatal Error. It will print

Prepare to be redirected!

Page will redirect to http://localhost/ip/ip.php?k=3&l=1. If not, Click to the link


and auto reload.

I hope you will enjoy it.

Leave a Reply

Your email address will not be published. Required fields are marked *

*