<?
   $mysql_handle  = 0;
   $mysql_host    = "db-client.wetteronline.de";
   $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);
     }
   }  

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


   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 FindIpCountry($remote_addr)
   {
      $query         = "";
      $ipnumber      =  0;
      $ip[4];
      $ierg          = -1;
      $ipos          =  0;
      $iremote_addr  =  0;

      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;";

//      echo $ipnumber;

      $result = mysql_query($query);

      if (!$result)
         die("Kein Ergebnis");

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


?>