繁体   English   中英

Php重定向到网页(html)

[英]Php Redirect to a webpage (html)

我已经看到在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

附加信息:这在我的本地计算机上完美运行,并在服务器中创建问题。

在您已经回显任何输出后,您不能调用header() HTTP请求包含标头信息和数据。 任何输出都是数据的一部分,数据在所有标头都已发送之后出现。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM