...

View Full Version : Function Returning 1 Record



pauldr
11-05-2009, 01:06 PM
I have this function that is suppose to do multiple record lookups on a block of IP addresses. However, the function only returns the last record of the and skips the previous records. I'm posting the code to see if anyone has possible ideas on how to resolve the issue.

Thank you,
Paul


<?php

//Uses whois to get IP information
class Whois
{
//performs actual lookup on $ip
public function lookup($ip = "")
{
$cmd = "whois " . $ip;
$result = shell_exec($cmd);

$result = explode("\n", $result);
$whois_data = array();

//Format data
foreach ($result as $value) {
//Skip empty lines
if ($value == '') {
continue;
}

$sep_pos = strpos($value, ':');
$title = trim(substr($value, 0, $sep_pos));
$whois_data[$title] = trim(substr($value, $sep_pos + 1));
}

return $whois_data;
}

// Convert an IP address string into a signed integer
public function iptoint($ip)
{
$octet = explode(".", $ip);
return (($octet[0] * 16777216) + ($octet[1] * 65536) + ($octet[2] * 256) + $octet[3]);
}

// Convert an IP address string into a numeric string to support unsigned integers and array indices
public function iptostr($ip)
{
$octet = explode(".", $ip);
return (string)(($octet[0] * 16777216) + ($octet[1] * 65536) + ($octet[2] * 256) + $octet[3]);
}

// Convert signed integer back into an IP address array
public function inttoip($value)
{
$octet[0] = floor($value / 16777216);
$value -= $octet[0] * 16777216;

$octet[1] = floor($value / 65536);
$value -= $octet[1] * 65536;

$octet[2] = floor($value / 256);
$octet[3] = $value - ($octet[2] * 256);

return $octet;
}

public function mineipdata($startip,$endip)
{
if ($this->iptoint($startip) < $this->iptoint($endip)) {

do {
$whois = $this->lookup($startip);
$ipdata = explode("-",$whois['NetRange']);
$netstart = $ipdata[0];
$netend = $ipdata[1];

$netendint = $this->iptoint($netend);
$netendip = $this->inttoip($netendint + 1);

$nextip = implode(".", $netendip);

$nextipint = $this->iptoint($nextip);
$stopipint = $this->iptoint($endip);

$startipint = $this->iptoint($nextip);
$startip = implode(".", $this->inttoip($startipint + 1));

//Initialize Array
$ipdata = array();

$ipdata = $this->lookup($nextip);

} while ($nextipint <= $stopipint);

}

else {

$ipdata = NULL;
}

return $ipdata;

}
}

abduraooft
11-05-2009, 01:16 PM
You could echo out all the intermediate results before the return statement to trace out the issue, like
public function lookup($ip = "")
{
$cmd = "whois " . $ip;
$result = shell_exec($cmd);

$result = explode("\n", $result);
echo '<pre>';
print_r($result);
echo '</pre>';
$whois_data = array();

//Format data
foreach ($result as $value) {
//Skip empty lines
if ($value == '') {
continue;
}

$sep_pos = strpos($value, ':');
echo '<br>'.$title = trim(substr($value, 0, $sep_pos));
$whois_data[$title] = trim(substr($value, $sep_pos + 1));
}

return $whois_data;
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum