<?
      $query         = "";

   $mysql_handle  = 0;
//217.110.117.18
//db-en.weatheronline.co.uk       28800 IN        A       217.110.117.21
//db-en.weatheronline.co.uk       28800 IN        A       217.110.117.51
//db-en.weatheronline.co.uk       28800 IN        A       217.110.117.57
//db-en.weatheronline.co.uk       28800 IN        A       217.110.117.62
//db-en.weatheronline.co.uk       28800 IN        A       217.110.117.12

   $mysql_host    = "127.0.0.1:9990";
   $mysql_port    = 9990;
   $mysql_user    = "info";
   $mysql_pwd     = "mys2402";
   $mysql_db      = "ip2location";
   $verbindung    = 1;


   if ( isset($_GET["TYP"]) ){
     $typ = $_GET["TYP"];
   }else{
     $typ = "sky";
   }

   $remote_add = $_SERVER["REMOTE_ADDR"];
//     echo "Remote Address: ".$remote_add."<br>";
   if($mysql_handle == 0){
     if( !ConnectToMySQL() ){
//         die("Verbindung zur Datenbank nicht möglich");
       $verbindung = 0;
     }else{
       $object = FindIpCountry($remote_add);
     }
   }  
   CleanUpDB();

// echo "SHORT: ".$object->cc."<br>";
   if ( $typ == "sky" ){
     if ( $object->cc != "UK" && $verbindung == 1 ){
       $_newheader = "Location: /wb/espott-sky.htm";
//       header($_newheader);
     }else{
       $_newheader = "Location: /wb/sky_otp.htm";
//       header($_newheader);
     }
   }
   
   if ( $typ == "js" && $verbindung == 1){
     echo "user_location = '" . $object->cc . "';";
   }else{
     echo "user_location = 'UK';";
   }


   function ConnectToMySQL()
   {
      global $mysql_handle;
      global $mysql_host;
      global $mysql_user;
      global $mysql_pwd;
      global $mysql_port;
      global $mysql_db;

      if(($mysql_handle = mysql_connect($mysql_host, $mysql_user, $mysql_pwd)) == FALSE)
         return FALSE;

      if(mysql_select_db($mysql_db, $mysql_handle) == FALSE)
         return FALSE;

      return TRUE;
   }

   function CleanUpDB()
   {
     global $mysql_handle;
     
     if( $mysql_handle != 0 ){
       mysql_close($mysql_handle);
       $mysql_handle = 0;
     }
   }


   function FindIpCountry($remote_addr)
   {
//      $query         = "";
      $ipnumber      =  0;
      $ip[4];
      $ierg          = -1;
      $ipos          =  0;
      $iremote_addr  =  0;

      global $query;
      
      for($i = 0; $i < strlen($remote_addr) && $iremote_addr < 4; $i++)
      {
         if($remote_addr[$i] == ".")
         {
            $ip[$iremote_addr] = intval(substr($remote_addr, $ipos, $i-$ipos));
            $iremote_addr++;
            $ipos = $i + 1;
         }
      }
      $ip[$iremote_addr] = $remote_addr[$ipos];
      $ipnumber = 16777216 * $ip[0] +
                  65536    * $ip[1] +
                  256      * $ip[2] + $ip[3];

//      $query = "SELECT * FROM `IPCountry`";
//      $query .= " WHERE ipTO > ";
//      $query .= $ipnumber;
//      $query .= " LIMIT 0,1;";
       $query = "SELECT cc, cn";
       $query .= " FROM ip NATURAL JOIN cc";
       $query .= " WHERE ";
       $query .= $ipnumber;
       $query .= " BETWEEN start AND end";

//      echo $ipnumber;

      $result = mysql_query($query);

      if ( (! $result) or mysql_numrows($result) < 1) 
        die("user_location = 'UK' ");
         //"user_location = UKs" );
//         die("Kein Ergebnis");

      $object = mysql_fetch_object($result);
      mysql_free_result($result);
      return $object;
   }


?>
