簡體   English   中英

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相關的問題。 考慮到它是 HTML標記之前啟動而不是在HTML標記之后啟動的, header.php的問題是什么導致了這一點?

<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 ,則假設您也將其包含在要重定向的頁面中,則可能是問題所在。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM