I need a way to pull in data from an external source (probably use htp://hostip.info) in order to assign country specific IP ranges to roles.

CommentFileSizeAuthor
#1 code snippet.php_.txt1.62 KBjonfrancisskydiver

Comments

jonfrancisskydiver’s picture

StatusFileSize
new1.62 KB

sirkitree,

I have included a code snippet that will pull data from the hostip.info site. Note the _get_url function--it may more precisely answer your question. the first function will pull the information from host.info and utilize Drupal's caching functions.

Jonathan

sirkitree’s picture

Cool thanks. I drupalized it ;)

/**
 * Find where this IP is from in the world
 */
function ipauth_get_geo_ip($ip = '', $reset = FALSE) {
	global $user;
	
	// If $ip is empty, use remote addr
	if (!strlen($ip)) {
		$ip = $_SERVER['REMOTE_ADDR'];
	}
	if ($ip == '127.0.0.1') {
		$ip = "216.227.213.170";  // hostip.info server's ip.
	}

	// First off, we only want the subnet
	$subnet = substr($ip, 0, strrpos($ip, '.'));	
	
	// Check if it's cached
	$cache_file = $subnet .'.sub';
	$cache = cache_get("ipauth:". $cache_file, 'cache');

	if (isset($cache->data)) {
		$geo_cache = unserialize($cache->data);

		// Has the cache expired?
		if (time() > $geo_cache['expire']) {
			// Delete the cache, and unset it.
			cache_clear_all("ipauth:". $cache_file, 'cache', TRUE);
			unset ($geo_cache);
		}
	}
	
	if (!isset($geo_cache) || $reset) {
		// Expire after 1 week
		$expire = time() + (60 * 60 * 24 * 7);
		$alltext = '';
		$alltext = drupal_http_request('http://api.hostip.info/get_html.php?ip='. $subnet .'.1');	  
		$tokens = split("[:\n]", $alltext->data);	
		$geo_cache = array($tokens[0] => $tokens[1], $tokens[2] => $tokens[3], 'expire' => $expire);

		// Now write the cache
		cache_set("ipauth:". $cache_file, 'cache', serialize($geo_cache), $expire);
	}
		
	// Show the output
	return $geo_cache;
}

I'll commit this after I get a select box in that will allow a user to choose a country and it'll automatically fill in the range.

sirkitree’s picture

So I just realized that a range for a country changes all the time and that hostip.info doesn't give that info out anyway.

My original idea was to be able to select a country from a dropdown and that would give you the IP ranges of that country. Turns out that list is GINORMOUS and would just be chaff.

Instead I think what I'm going to do is this:
When a visitor comes to the site, do a lookup in my hook_init() to get their country code and give them a role with the name of that country code. This assumes that Drupal already has a role with this country code's name. However, since I'm currently only dealing with one other country than US, it should be simple. I'll probably just use what this module has in it's hook_init() and modify it to get the country code instead of looking up any entries.

Does anyone want me to commit the function above to the module?

jonfrancisskydiver’s picture

IP to Country or IP2Nation modules may keep track of the changing IP address assignments. I haven't taken a look at those modules yet. I agree that hostip.info may not be reliable enough for some Drupal users because I've been noticing that hostip.info does provide country information (http://api.hostip.info/country.php
?), but there are still a number of IP address that hostip.info doesn't have information for. Perhaps "ip to country" or "IP2Nation" would help out.