Php重定向到网页(html)

时间:2014-03-22 16:49:26

标签: php url redirect

我已经看到在php中使用标题重定向到页面。

我已经使用了下面代码中的方法以及代码下面给出的重定向错误。

我需要知道代码出了什么问题。

这是代码(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();

以下是我收到的错误,而不是上传到服务器时的重定向:

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

其他信息:这在我的本地计算机上运行良好,并在服务器中产生问题。

1 个答案:

答案 0 :(得分:2)

在您回复任何输出后,您无法呼叫header()。 HTTP请求包含标头信息和数据。任何输出都是数据的一部分,数据在所有标题都已发送之后出现。

PHP有一个名为headers_sent的函数来检查标头是否已经发送过。您可以考虑编写一个发布Location头的函数(如果它们尚未发出),或者如果它们有一些简单的重定向HTML,则可以考虑 - 可能通过在<head>中包含此标记:

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