簡體   English   中英

在 php 中運行兩個標頭?

[英]Run two headers in php?

是否有在 php 中執行兩個標頭的解決方案? 第一個 header 是執行 php 腳本,第二個 header 是重新加載當前的 ZE1BFD4ZZACEE366 頁面。 如果沒有,還有其他方法嗎? 按下按鈕,調用submitGen function。 是否可以在兩個 header 函數之間添加延遲,以便它們可以從同一個腳本運行?

<?php
if (isset($_POST['submitGen'])) {
    header('Location:Page2.php');
    header('Refresh: 0');
}
?>

...
<tr>
    <form action="" method="post" role="form">
        <td>
            <input type="submit" name="submitGen" class="btn btn-primary" value = "Gen">
        </td>
    </form>
</tr>

不可能,header function 發送 HTTP 響應。 每個請求只能發送 1 個響應,這就是 HTTP 協議的工作原理(大部分情況下)。 客戶端請求 1 個資產/文檔/某物,服務器為該調用提供 1 個響應。

如果您想在客戶端觸發事件后刷新當前頁面,請考慮使用 Javascript 在表單提交事件上使用location.reload()之類的方法執行當前頁面的刷新。

我會推薦一個兩步的解決方法。

思路是在第一次請求時發送redirect,然后設置一個session變量,所以在第二次請求時發送刷新。

<?php
session_start();
if(isset($_POST['submitGen']))
    {
        $_SESSION['do'] = 'refresh';
        header('Location:Page2.php');
    }
if($_SESSION['do'] == 'refresh')
    {
        unset($_SESSION['do']);
        header("Refresh: 0");
    }

?>

目前還不清楚 Page2.php 做了什么,但請考慮:

1) Page2.php 僅是服務器代碼 - output 對客戶端沒有任何影響:

<?php
if(isset($_POST['submitGen']))
    {
        //header('Location:Page2.php');
        //header("Refresh: 0");
        require 'Page2.php';
    }

?>

...

2) Page2.php 有 output 需要發送到客戶端 (cookies, javascript 等)

<?php
if(isset($_POST['submitGen']))
    {
        header('Location:Page2.php?autoreturn=true');
        //Catch the autoreturn argument in Page2.php
        //die die die always after a header location..
        die();
    }

?>

提交表單后,運行 Javascript function 獲取Page2.php並在完成后重新加載。

這是我剛剛在我的服務器上測試過的概念驗證,並且完全按照您的要求工作。 當用戶點擊提交按鈕時, Page2.php在后台加載 Page2.php 然后重新加載Page1.php

第1頁.php:

<script>
function do_submit() {
        fetch('Page2.php')
        .then(function() {
                console.log('Got Page2.php! Reloading...');
                location.reload()
        })
        .catch(function() {
                console.log('Page2.php failed')
        });

        return false;
}
</script>

<table>
<tr>
<form action="" method="post" role="form" onsubmit="return do_submit()">
<td>
    <input type="submit" name="submitGen" class="btn btn-primary" value = "Gen">
</td>
</form>
</tr>
</table>

第2頁.php:

$fp = fopen( 'log.txt', 'a' );
if ( $fp ) {
        fwrite( $fp, date("Y-m-d h:i:si\n") );
        fclose($fp);
        echo "Done!\n";
} else {
        echo "Error opening file\n";
}

注意log.txt必須是 web 服務器可寫的,否則腳本將無法打開它進行寫入。

我在我的 web 服務器上對此進行了測試,它完全符合您的要求:它執行Page2.php (由寫入log.txt的日期/時間證明,然后在瀏覽器中重新加載Page1.php

可能沒有辦法像上面提到的那樣發送兩個標頭,但是。

如果您需要在顯示 Page2.php 之后重新加載,並且 Page2.php 上沒有 Javascript/用戶輸入,您可以添加

echo "<script>location.reload()</script>";

Page2.php的末尾 - 這是一個 js 代碼,它會在到達時重新加載頁面。 即使在 IE 中也可以使用,但需要啟用 JS。

讓表單將其內容加載到<iframe>中。 使用 JS 檢測<iframe>何時加載,然后重新加載主頁面:

<form action="" method="post" role="form" onsubmit="return doIt()">
  <input type="submit" name="submitGen" class="btn btn-primary" value = "Gen">
</form>

<iframe id="dest" src=""></iframe>

處理表單提交的腳本:

function doId() {
  var iframe = document.getElementById('dest')
  iframe.addEventListener('load', function() {
    location.reload(); // reload current page
  })
  iframe.src = 'Page2.php'
  return false; // Prevent form from submitting
}

有一種方法可以發送,您可以在單個 header 中執行兩項任務

header("Refresh:0;url=Page2.php");

這里發生的情況是您當前所在的頁面首先被刷新,然后 header 的第二部分完成,它將您重定向到提到的另一個網頁。

注意:向網頁發送多個header()不是一個好主意,因為如果它們屬於同一類型,則只會執行最后提到的header

為 1 個請求設置多個標頭的正確方法:

<?php
    if(isset($_GET['submit'])) {
        header("Location:Page2.php");
        header("Refresh:0");
    }
?>           

注意:在這個示例中,我們重定向到Page2.php因為Refresh:0無用的!

說沒用並不意味着Page1.php不會 output header: Refresh:0

從不使用: Location + Refresh相同的請求,除非您知道自己在做什么。


Page1 將 output 兩個標題,但您不會注意到與常規 web 瀏覽器有任何區別。

如果您在 Chrome 中進行調試,您將看到此代碼為 Page1 設置了兩個標題: 在此處輸入圖像描述


是否有在 php 中執行兩個標頭的解決方案?

是的,如下所示,您可以根據需要設置任意數量的 header:

<?php
    if(isset($_GET['submit'])) {

        if(isset($_GET['count'])) {
            if($_GET['count'] === '5') {
                header("Location:Page2.php");
            } else {
                sleep(1); // HERE Instead of SLEEP run your magic code!
                $count = $_GET['count'] + 1;
                header("Location:Page1.php?submit=1&count=$count");
            }
        } else {
            header("Location:Page1.php?submit=1&count=1");
        }
    }

    header("Connection:Close");
    header("Content-Type:Unknown Cyborg Coding");
    header("Keep-Alive:timeout=60, max=300");
    header("X-Powered-By:Cyborg-Powered MAGIC Servers");
    header("Language:Chineeeeese");
    header("Bots:Are my Friends!");
    header("Hacks:Are COOL!");
    header("Knowledge:Is GOOD!");
    header("StackOverflow:Is GOOD");
    header("Refresh:5"); // This header will be SET but will not make any difference since we use Location Redirect!
    // Refresh Header AND Location Header SHOULD NOT be set for same request unless you KNOW what you are doing. 

    echo "This lines will never been seen UNLESS you use cURL or other approch to DISABLE FOLLOW-REDIRECTS!<br/>\n";
    echo "Enjoy Coding!";
?>

同樣,Header 在這里Refresh不會有任何區別。 但是在第 1 頁重新加載 5 次后,它將重定向到第 2 頁!

請參閱此標頭和請求:

在此處輸入圖像描述


第一個 header 是執行 php 腳本,第二個 header 是重新加載當前的 ZE1BFD4ZZACEE366 頁面。

為此,您不需要設置 2 header,但是如果您想多次重新加載 Page1 直到您從其他腳本獲得一些結果,然后當腳本的結果成功時將 go 設置為 Page2,那么只需更改上面代碼中的 if 條件。

代碼示例:

<?php
    if(isset($_GET['submit'])) {
        $results = shell_exec("curl -k 'https://api4.eu'"); // here set the script you want.
        if (strpos($results, 'Realtime API Exchange') !== false) { header("Location:Page2.php"); }
        else { sleep(3); header("Location:Page1.php?submit=1"); }
    }
?>

或者更好地避免sleep和使用:

<?php
    if(isset($_GET['submit'])) {
        $results = shell_exec("curl -k 'https://api4.eu'"); // here set the script you want.
        if (strpos($results, 'Realtime API Exchange') !== false) { header("Location:Page2.php"); }
        else { header("Refresh:3"); }
    }
?>

暫無
暫無

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

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