【问题标题】:Redirect to previous page with php使用php重定向到上一页
【发布时间】:2017-07-27 09:22:24
【问题描述】:

提交按钮在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>")
}

【问题讨论】:

  • 您想将用户发送回contact us.html吗?发送电子邮件后的页面?
  • 是的!当按下按钮时,它会转到 php 页面并停留在那里
  • 邮件是否发送?可能是 if($_POST['submit'])false 并且标题从未设置。
  • 是的,邮件工作正常,我可以接收所有电子邮件。只有重定向位不起作用...
  • 我希望你确实意识到邮件头是在邮件功能之前发送的。这意味着如果可行,您的邮件将永远不会被发送

标签: php html post


【解决方案1】:

您是否尝试过寻找推荐人? PHP 有一个功能。

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

这意味着如果它来自 cnotact.html,它将查看它是如何到达那里并返回的。如果您在另一个页面上有第二个联系表,则易于使用。

我需要补充一点,尽管这可能无法通过安全页面进行。 (因此,如果您通过 https 运行它)并且标头可能会被劫持(尤其是在浏览器未发送请求的情况下)。

【讨论】:

  • 这是由用户代理设置的。不是所有的用户代理都会设置这个,有些提供修改 HTTP_REFERER 作为一个特性的能力。简而言之,它不能真正被信任。
  • 我知道这一点,(因此我修改了答案)但是这个问题的答案就是这样
【解决方案2】:

您确定您有一个带有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 块

【讨论】:

    【解决方案3】:

    尝试使用 JavaScript window.location 重定向 注意:将window.location.href('contact-us.html'); 包装到脚本标签中

    例子:

    echo "<script>window.location.href('contact-us.html');</script>";
    

    【讨论】:

      【解决方案4】:

      我会这样做:

      我建议使用$_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

      【讨论】:

        【解决方案5】:
        header('Location:  contact-us.html'); /*Something Wrong?
        

        【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-09-09
        • 1970-01-01
        • 2013-01-09
        • 2013-07-24
        • 2017-02-08
        • 2017-04-30
        相关资源
        最近更新 更多