duanqun7761 2014-03-22 16:49
浏览 35
已采纳

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.

  • 写回答

1条回答 默认 最新

  • dp198879 2014-03-22 16:51
    关注

    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" />
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛