PHP,是否可以包含不在文件顶部的php文件来解决header()函数的限制?

时间:2013-08-09 18:03:03

标签: php html header include

我为这个微不足道的问题道歉,但我一直在使用header()php函数重定向到页面时遇到问题。更具体地说,当他/她试图查看不存在的个人资料页面时,我正在努力重定向用户。我的问题是我总是包含一个包含会话开始的头文件和一些显示基本头的html。这是否意味着我不能使用header()函数重定向到包含此头文件的脚本中的页面? 我认为解决问题的一种方法可能是将标题的html部分拆分为单独的文件,首先包含标题脚本,然后编写我的配置文件脚本,最后包含标题的html部分。这是不好的做法吗? profile.php脚本如下:

<?php include("inc/incfiles/header.inc.php"); ?>
<?php

if(isset($_GET['u'])) {

    //check user exists

    $username = mysql_real_escape_string($_GET['u']);

    if (ctype_alnum($username)) {

        $check = mysql_query("SELECT username, email FROM users WHERE username = '$username'");

        if (mysql_num_rows($check)===1) {

            $get = mysql_fetch_assoc($check); //execute query and store in array

            $username = $get['username'];

            $email = $get['email'];

        }

        else {
            header("Location: index.php");

            die();

        }

    }

    else {

        echo "username has to be alphanumeric";

    }

}

else {

    echo "error";

}

?>


<h2> Profile page of <?php echo "$username";?>


<h3> Email: <?php echo "$email";?>

header.inc.php文件:

<?php

include ("inc/scripts/mysql_connect.inc.php");

//start the session


session_start();

//Checks whether the user is logged in

$user = $_SESSION["user_login"];

if (!isset($SESSION["user_login"])) {

//header("Location: index.php");

//exit();


}

else

{

    header("location: home.php");

}


?>

<?php


//Login Scripts has to be at the top to make sure header() redirecting works


if (isset($_POST["user_login"]) && isset($_POST["password_login"])) {

$user_login = preg_replace('#[^A-Za-z0-9]#i','', $_POST["user_login"]); //filter user login text

$password_login = preg_replace('#[^A-Za-z0-9]#i','', $_POST["password_login"]); //filter user password text

$md5password_login = md5($password_login);

$sql = mysql_query("SELECT id FROM users WHERE username='$user_login' AND 
password='$md5password_login' LIMIT 1"); //query the user


//Check for user's existence

$userCount = mysql_num_rows($sql); //count number of rows

if ($userCount == 1) {

    while ($row = mysql_fetch_array($sql)) {

        $id = $row["id"];

    }


    $_SESSION["user_login"] = $user_login;

    $_SESSION["password_login"] = $md5password_login;



header("Location: home.php");


    exit();


}

else {

    echo "That information is incorrect, try again";

}

}



?>



<html>

<head>

    <link href = "css/main.css" rel = "stylesheet" type = "text/css">

    <title> title </title>

</head>

<body>

    <div class = "wrapper">

        <div id = "header">

            <div class = "logo">

                <img src = "img/Logo.png">

        </div>

            <div id = "login-header">


                    <form action = "index.php" method ="post" name = "form1" id = "form1">

                            <div class = "input-wrapper"><input type = "text" size = "25" name = "user_login" id = "user_login" placeholder = ">Username" ></div>

                            <div class = "input-wrapper"><input type = "password" size = "25" name = "password_login" id = "password_login" placeholder = "Password" ></div>

                            <div class = "input-wrapper"><input type = "submit" name = "login" value = "Sign in"></div>

                    </form>


            </div>


        <div id = "menu">

            <a href = "#"></a>

            <a href = "#"></a>

        </div>

    </div>

</div>

1 个答案:

答案 0 :(得分:4)

您可以在任何地方包含文件。问题是 OUTPUT 。如果您要进行header()来电,则无法在通话前执行 ANY 输出。如果你的包含只是吐出输出,而不只是定义函数/变量以供以后使用,那么你必须修改包含以不执行该输出,或者至少缓冲/推迟输出直到以后。 e.g。

<?php

include('some_file_that_causes_output_when_loaded.php');
header('This header will not work');

无论如何都会失败,因为你的include输出了,并且杀死了标题调用。但是如果你修改了include,那么你有更多的东西:

<?php

include('file_that_just_defines_functions.php');
header('This header will work');
function_from_include_that_causes_output();

工作得很好。如果你不能修改代码,那么

<?php

ob_start();
include('some_file_that_causes_output_when_loaded.php');
header('This header will still work, because we're buffering output');
ob_end_clean();