Php重定向到网页(html)

I've seen the use of header in the php to redirect to a page.

I've used the method in the code below and the a redirect error as given below the code.

I need to know what went wrong with the code.

Here is the code (php) :

<?php
/**
 * Created by PhpStorm.
 * User: Priyabrata
 * Date: 3/18/14
 * Time: 8:58 PM
 */


function connect_to_db($uid, $pass){
    $con=mysqli_connect("localhost", "root", "12345", "MyDB");
    if (mysqli_connect_errno()){
        echo "Failed to establish link!!";
    }else{
        echo mysqli_connect_error();
    }
    $result=mysqli_query($con, "SELECT `pwd` from `login_table` where `uid` = "."'".$uid."'");
    if (!$result){
        echo "Error!!";
        exit();
    }
    while($xx = mysqli_fetch_array($result)){
        if ($xx['pwd'] == hash("MD5", $pass)){
            header("location:../Html/Login-Existing-Users1_success.htm");
            exit();
        }else{
            echo "Invalid Login Credentials!!";
            exit();
        }
    }
    mysqli_close($con);
}

function validate_credentials(){
    if ((isset($_POST["username"]))&&(isset($_POST["password"]))){
        $uid = $_POST["username"];
        $pwd = $_POST["password"];
        if (($uid == "")||($pwd == "")){
            echo "Please enter User name and password";
        }else{
            //Check user name and password
            connect_to_db($uid, $pwd);
        }
    }else{
        echo "Please enter User name and password";
    }
}

validate_credentials();

Here is the error I get, instead of a redirect when uploaded to the server:

Warning: Cannot modify header information - headers already sent by (output started at /home/opticfhb/public_html/helpvssupport.net/login.php:19) in /home/opticfhb/public_html/helpvssupport.net/login.php on line 27

Additional Info : This works perfectly in my local machine and creates the problem in the server.

You can't call header() after you've already echoed any output. HTTP requests contain header information and data. Any output is part of the data, and data comes after all the headers have already been sent.

PHP has a function called headers_sent to check whether or not the headers have already been sent or not. You might consider writing a function that issues the Location header if they haven't been, or echo out some simple redirect HTML if they have -- perhaps by including this tag in the <head>:

<meta http-equiv="refresh" content="0;http://www.website.com/redirect/url" />