function i2c_go() { global $COUNTRY; // Get the real IP of the user $ipnum = i2c_realip(); // User already has a country detected with this IP stored if (!empty($_COOKIE['COUNTRY']) && strpos($_COOKIE['COUNTRY'], ',') !== FALSE) { list($COUNTRY, $storedip) = explode(",", $_COOKIE['COUNTRY']); if ($storedip == $ipnum) { return TRUE; } } // Convert the IP number to some useable form for searching $ipn = (double) sprintf("%u", ip2long($ipnum)); // Find the index to start search from $idx = i2c_search_in_index($ipn); // If we were unable to find any helpful entry // in the index do not search, as it would take // a very long time. It is an error, if we have // not found anything in the index if ($idx !== FALSE) { $country = i2c_search_in_db($ipn, $idx); } else { $country = 'NA'; } // Set global variable for further usage $COUNTRY = $country; // Set the country in a cookie for a week return setcookie("COUNTRY", "{$country},{$ipnum}", 60 * 60 * 24 * 7); }
function i2c_get_country($ip = false) { if ($ip === false) { $ip = $_SERVER['REMOTE_ADDR']; } $ipn = (double) sprintf("%u", ip2long($ip)); $idx = i2c_search_in_index($ipn); if ($idx !== false) { $country = i2c_search_in_db($ipn, $idx); } else { $country = 'N0'; } return $country; }