PHP 搬迁 header 不工作

PHP relocation header not working

我编写了一个 Web 应用程序,它要求您先登录,然后才能访问该应用程序的实际内容。

当用户像这样进入主页时,我将值 'logedIn' 设置为 false:

<?php
    $_SESSION['logedIn'] = "false";
?>

如果用户尝试转到 overview.php,代码会检查 logedIn 的值是否等于 'true'。如果不是,header 将 window 重新定位到登录页面,如下所示:

<?php
session_start();
?>

<html>
<head>
    <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
    <script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
    <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
    <script src="https://jquery-ui.googlecode.com/svn-history/r3982/trunk/ui/i18n/jquery.ui.datepicker-nl.js"></script>
    <script src="../javascript/overview.js"></script>
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link rel="stylesheet" href="../css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="../css/mainpage.css">
    <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">

</head>
<body>

    <?php
        if($_SESSION['logedIn'] != "true") {
            header("Location: ../index.php");
            exit();
        }
    ?>

    <div class="container-fluid z-container">
        <div class="row z-overview-top-menu">
            <div class="col-md-2 z-background-dark z-top-nav z-border-right" id="menu_logo">
                <h3>Logo</h3>
            </div>
            <div class="col-md-2 z-top-nav z-item-hover z-border-right z-selected" id="menu_diary" onclick="changeMenu('diary')">
                <h3>Dagboek</h3>
            </div>
            <div class="col-md-2 z-top-nav z-item-hover z-border-right" id="menu_pazo" onclick="changeMenu('pazo')">
                <h3>Pazo</h3>
            </div>
            <div class="col-md-2 z-top-nav z-item-hover z-border-right" id="menu_counter" onclick="changeMenu('counter')">
                <h3>Tellers</h3>
            </div>
            <div class="col-md-2 z-top-nav z-item-hover z-border-right" id="menu_overview" onclick="changeMenu('overview')">
                <h3>Overzicht</h3>
            </div>
            <div class="col-md-2 z-top-nav z-item-hover" id="menu_comparison" onclick="changeMenu('comparison')">
                <h3>Vergelijking</h3>
            </div>
        </div>
        <div class="row">
            <div class="col-md-2 z-overview-left-menu">
                <ul class="z-left-list" id="userList"></ul>
            </div>
            <div class="col-md-10 z-overview-main-menu">
                <ul id="overviewList" class="z-overview-list-main">
                </ul>
            </div>
        </div>
    </div>
</body>

当我用 echo 替换 header 时,我得到一个响应,表明当我 运行 代码时 header 确实在执行。

此外,任何目录中都没有 .htaccess。

有什么问题吗?

http://php.net/manual/en/function.header.php

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

因为你正在输出:

<html>
<head>
    <script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
    <script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
    <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
    <script src="https://jquery-ui.googlecode.com/svn-history/r3982/trunk/ui/i18n/jquery.ui.datepicker-nl.js"></script>
    <script src="../javascript/overview.js"></script>
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link rel="stylesheet" href="../css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="../css/mainpage.css">
    <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">

</head>
<body>

在header调用之前,会失败。