简体   繁体   English

Php重定向到网页(html)

[英]Php Redirect to a webpage (html)

I've seen the use of header in the php to redirect to a page. 我已经看到在php中使用标头重定向到页面。

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):

<?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. 在您已经回显任何输出后,您不能调用header() HTTP requests contain header information and data. HTTP请求包含标头信息和数据。 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. PHP有一个名为headers_sent的函数来检查标头是否已经发送过。 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> : 您可以考虑编写一个函数来发布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