Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Nov 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Function Returning 1 Record

    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

    Code:
    <?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;	
    		
    	}	
    }

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,853
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    You could echo out all the intermediate results before the return statement to trace out the issue, like
    PHP Code:
    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($value0$sep_pos));
                
    $whois_data[$title] = trim(substr($value$sep_pos 1));
            }

            return 
    $whois_data;
        } 
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •