PHP头重定向工作不正确


PHP header redirect not working correctly

我试图重定向用户到一个新的页面,一旦他们的登录凭据已被确认,但由于某种原因重定向不会工作。它似乎与我的header.php文件有关。当我在之前包含重定向时,它可以工作;但是,如果我尝试将它包含在这个文件之后,它不起作用。

我已经尝试添加重定向到header.php本身,当我把它直接包含在<html>标签之前,它的工作(好吧,我说的工作,但它给出了一个错误,这比它做其他;Firefox has detected that the server is redirecting the request for this address in a way that will never complete.)。

我已经检查了所有文件的空白等,这使我找到了与header.php相关的问题。header.php的问题是什么?考虑到它在 HTML标签之前启动,而不是在之后放置 ?

<html>
<head>
    <title><?php echo $pageTitle; ?></title>
    <link rel="stylesheet" href="<?php echo BASE_URL; ?>css/style.css" type="text/css">
    <link rel="stylesheet" href="<?php echo BASE_URL; ?>css/property.css" type="text/css">
    <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Oswald:400,700" type="text/css">
    <link rel="shortcut icon" href="<?php echo BASE_URL; ?>favicon.ico">
</head>
<body>
    <div class="header">
        <div class="wrapper">
            <h1 class="branding-title"><a href="<?php echo BASE_URL; ?>">Some title</a></h1>
            <ul class="nav">
                <?php
                  /* list items with a class of "on" indicate the current section; those links 
                   * are underlined in the CSS to communicate that back to the site visitor;
                   * the $section variable is set in each individual file
                   */
                ?>
                <li class="shirts <?php if ($section == "shirts") { echo "on"; } ?>"><a href="<?php echo BASE_URL; ?>shirts/">Shirts</a></li>
                <li class="contact <?php if ($section == "contact") { echo "on"; } ?>"><a href="<?php echo BASE_URL; ?>contact/">Contact</a></li>
                <li class="search <?php if ($section == "search") { echo "on"; } ?>"><a href="<?php echo BASE_URL; ?>search/">Search</a></li>
                <li class="cart"><a target="paypal" href="XYZ">Shopping Cart</a></li>
            </ul>
        </div>
    </div>
    <div id="content">

当我把它放在标签前面时,它可以工作,但在其他地方不起作用。

这实际上是你的问题。头只能在发送正文之前设置。你只需要保持重定向在任何页面内容之上。

HTTP响应的结构如下:

HTTP/1.1 200 OK
Header-1: bacon
Header-2: more bacon
<html>
<head>
  <title>Bacon</title>
...

重定向是一个HTTP头,特别是Location头。你不能在你已经发送了数据后设置这个(你可以,但这很奇怪和复杂)。您还应该在代码的某个地方发现错误,假设您在某个地方有错误报告。

关于你的另一个问题……

Firefox检测到服务器正在以一种永远不会完成的方式重定向此地址的请求。

这是因为你在一个循环中重定向(即重定向到重定向到自身的重定向)。如果你的重定向是在header.php中,假设你在你重定向的页面上也包括它,那可能是问题。