Here is a little script that I CANNOT get to work! It is crazy!
It is a problem with the mysql_fetch_array but I can't understand why. When the page is visited you see: Unknown and no errors at all.
When I run "SELECT * FROM geo_ip WHERE '$ipnum' >= begin_num AND '$ipnum' <= end_num" manually from PhpMyAdmin with my $ipnum it works fine and outputs two rows.
I don't understand why this script doesn't work at all?
I am using the geo_ip database.
Does anyone know what is going on?
function connect_me(){
$DB_USER = 'REMOVED';
$DB_PASSWORD = 'REMOVED';
$DB_HOST = 'REMOVED';
$dbc = mysql_connect ($DB_HOST, $DB_USER, $DB_PASSWORD) or die(mysql_error());
mysql_select_db('REMOVED') or die(mysql_error());
mysql_query("SET NAMES `utf8`") or die(mysql_error());
if($ERROR_REPORTING == 1){ do_error($error,2);}
}
function disconnect_me(){
@mysql_close($dbc);
@mysql_close();
}
function check_ip(){
$ipParts = explode(".", $_SERVER["REMOTE_ADDR"]);
if ($ipParts[0] == "165" && $ipParts[1] == "21") {
if (getenv("HTTP_CLIENT_IP")) {
$ip = getenv("HTTP_CLIENT_IP");
} elseif (getenv("HTTP_X_FORWARDED_FOR")) {
$ip = getenv("HTTP_X_FORWARDED_FOR");
} elseif (getenv("REMOTE_ADDR")) {
$ip = getenv("REMOTE_ADDR");
}
} else {
return $_SERVER["REMOTE_ADDR"];
}
return $ip;
}
function get_country(){
$user_ip = check_ip();
$temp = explode('.',$user_ip);
$ipnum = 16777216*$temp[0] + 65536*$temp[1] + 256*$temp[2] + $temp[3];
$qry = mysql_query("SELECT * FROM `geo_ip` WHERE '$ipnum' >= `begin_num` AND '$ipnum' <= `end_num`") or die(mysql_error());
$get_country = mysql_fetch_array($qry) or die(mysql_error());
if ($get_country['country_name']=="") { return 'Unknown'; } else { return $get_country['country_name'].':'.$get_country['iso_code']; }
}
$country_info = explode(':',get_country());
echo $country_info;
disconnect_me();
PS, checkip works fine (tested with echo)