 <?php

//$mode = isset($_POST['mode']) ? $_POST["mode"]  : "read";
//$actual = isset($_POST['url']) ? $_POST["url"]  : "";

   switch( $mode ) {

      case "start":
         $sql = "select sql_no_cache Url from configuracion where Id = 1";
         $result = $conn->query( $sql );
         if( $result && $result->num_rows > 0) {
            if($row = $result->fetch_assoc()) {
               $Url = $row["Url"];
               if( $Url != "" && $Url != "contableEjercicios.php" ) {
                  mysql_free_result( $result );
//                  echo $Url;
                  $target = "Location: " . $Url;
                  header( $target );
               }
            }     
         }     
         if( $result && is_resource( $result ) ) mysql_free_result( $result );
         break;

      case "read":
         $sql = "select sql_no_cache Url from configuracion where Id = 1";
//       echo $sql ."<br/>";
         $result = $conn->query( $sql );
         if( $result && $result->num_rows > 0) {
            if($row = $result->fetch_assoc()) {
               $Url = $row["url"];
               if( $Url != "" ) {
                  mysql_free_result( $result );
                  if( $actualUrl != $Url ) {
                     $sql = "update configuracion set Url='" . $actualUrl . "' where Id = 1";
                     $result = $conn->query( $sql );
                     if( $result && is_resource( $result ) ) mysql_free_result( $result );
                     header("Location: " . $Url );
                  }
               }
               else {
                  $sql = "update configuracion set Url='" . $actualUrl . "' where Id = 1";
                  $result = $conn->query( $sql );
                  if( $result && is_resource( $result ) ) mysql_free_result( $result );
               }
            }     
         }     
         if( $result && is_resource( $result ) ) mysql_free_result( $result );
         break;

      case "update":
         if( $url != "" ) {
            $sql = "update configuracion set Url='" . $actualUrl . "' where Id = 1";
            $result = $conn->query( $sql );
            if( $result && is_resource( $result ) ) mysql_free_result( $result );
         }     
         break;
   }
?>

