簡體   English   中英

用php重定向到上一頁

[英]Redirect to previous page with php

提交按鈕位於contact-us.html ,代碼位於contact-us.php

我試圖使用header('Location: example'); 沒有運氣...(我也嘗試使用頁面的URL: header('Location: http://www.example.com/contact-us.html');

有什么想法嗎?

<?php

if($_POST["submit"]) {

    $recipient="example@gmail.com";
    $subject="Form to email message";
    $sender=$_POST["firstname"];
    $senderlast=$_POST["lastname"];
    $senderemail=$_POST["senderemail"];
    $message=$_POST["message"];
    $mailBody="First Name: $sender\nLast Name: $senderlast\nEmail: $senderemail\n\nMessage: $message";

    header('Location: /contact-us.html'); /*Something Wrong?*/

    mail($recipient, $subject, $mailBody, "From: $sender <$senderemail>")
}

您是否嘗試過尋找引薦來源? PHP具有此功能。

header('Location: ' . $_SERVER['HTTP_REFERER'])

這意味着,如果它來自cnotact.html,它將看起來它是如何到達並返回的。 如果您在另一頁上有第二個聯系表,則易於使用。

我需要補充一點,盡管這可能無法通過安全頁面起作用。 (因此,如果您通過https運行),則可能會劫持標頭(尤其是在瀏覽器未發送請求的情況下)。

這是我的處理方式:

我建議使用$_SESSION這樣存儲以前的網址:

page1.php

<?php
    $_SESSION['previous'] = 'http://'. $_SERVER[HTTP_HOST]. $_SERVER[REQUEST_URI];

page2.php

<?php
    if ($_SESSION['previous'] !== '') {
        header('Location: '. $_SESSION['previous']);
    }

$_SESSION的工作方式類似於Cookie,但效果要好得多。 由於僅使用數組,因此更易於使用-可以在此處找到更多信息: http : //uk1.php.net/manual/en/reserved.variables.session.php

您確定具有submit名稱屬性的按鈕嗎? 如果是,也許試試這個:

<?php
if($_POST["submit"]) {

    $recipient="example@gmail.com";
    $subject="Form to email message";
    $sender=$_POST["firstname"];
    $senderlast=$_POST["lastname"];
    $senderemail=$_POST["senderemail"];
    $header = "MIME-Version: 1.0" . "\r\n";
    $header .= "Content-type:text/html;charset=UTF-8" . "\r\n";
    $header .= 'From: $sender <$senderemail>' . "\r\n";
    $message=$_POST["message"];
    $mailBody="First Name: $sender\nLast Name: $senderlast\nEmail: $senderemail\n\nMessage: $message";


    if(mail($recipient, $subject, $mailBody, $header)){

        header('Location:contact-us.html'); /*Something Wrong?*/
    }
}

如果沒有,那么您的代碼將永遠不會進入if塊

嘗試使用JavaScript window.location重定向注意:wrap window.location.href('contact-us.html'); 進入腳本標簽

例如:

echo "<script>window.location.href('contact-us.html');</script>";
header('Location:  contact-us.html'); /*Something Wrong?

暫無
暫無

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

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