<?
   $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    = "db-en.weatheronline.co.uk:9990";
   $mysql_port    = 9990;
   $mysql_user    = "info";
   $mysql_pwd     = "mys2402";
   $mysql_db      = "ip2location";
   $verbindung    = 1;



/*
   if($mysql_handle == 0)
      if(!ConnectToMySQL()){
        $_newheader = "Location: /wb/sky_google.htm";
//        $_newheader = "Location: /wb/sky_euroclick.htm";
        header($_newheader);
      }
//         die("Verbindung zur Datenbank nicht möglich");

   $remote_add = $_SERVER["REMOTE_ADDR"];
//   echo "Remote Address: ".$remote_add."<br>";


   $object = FindIpCountry($remote_add);

   srand((double)microtime()*1000000);
   $wb_rotation20 = rand()%20;
    
   if ( $object->countrySHORT == "UK" ){
*/
   if ( !checkCookie() ){
     $_newheader = "Location: /wb/sky_otp.htm";
   }else{
     $_newheader = "Location: /wb/blind.htm";
   }
   header($_newheader);
/*
   }else{
     if ( $wb_rotation20 < 21 ){
       if ( $object->countrySHORT == "FR" ){
         $_newheader = "Location: /wb/sky_advertising_FR.htm";
         header($_newheader);
       }else if ( $object->countrySHORT == "ES" ){
         $_newheader = "Location: /wb/sky_advertising_ES.htm";
         header($_newheader);
       }else{
//         $_newheader = "Location: /wb/sky_euroclick.htm";
         $_newheader = "Location: /wb/sky_google.htm";
         header($_newheader);
       }
     }else{
       $_newheader = "Location: /wb/sky_google.htm";
       header($_newheader);
     }
   }

   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;";
       $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("Kein Ergebnis");

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

*/

  function checkCookie()
  {
    if( !isset($_COOKIE["WOMEMBER"]) ) return false;
    $cookval =  $_COOKIE["WOMEMBER"];
                    
    //                $endtime = time() - 10; // + COOKIEDURATION;
    //        $key = $endtime . "geheim" . LANGUAGE;
    //        $md5key = md5($key);
    //        $cookval = "TIME=" . $endtime . "&LANG=" . LANGUAGE . "&SESSION=" . $md5key;
                       
    if( $cookval )
    {
      $acookval = explode ( "&", $cookval );
      $acooktime = explode ( "=", $acookval[0]);
                                                                     
      //        echo "time() = " . time();
      //        echo "cook   = " . $acooktime[1];
      if ( time() <= $acooktime[1] ){
        return true;
      }else{
        return false;
      }
    }
    return false;
  }
?>
