简体   繁体   English

用php重定向到上一页

[英]Redirect to previous page with php

The submit button is in contact-us.html , the code is in contact-us.php . 提交按钮位于contact-us.html ,代码位于contact-us.php

I tried to use the header('Location: example'); 我试图使用header('Location: example'); with no luck... (I tried using the URL of the page as well: header('Location: http://www.example.com/contact-us.html'); ) 没有运气...(我也尝试使用页面的URL: header('Location: http://www.example.com/contact-us.html');

Any thoughts? 有什么想法吗?

<?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>")
}

Have you tried to look towards the referer? 您是否尝试过寻找引荐来源? PHP has a function for that. PHP具有此功能。

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

This means that if it comes from the cnotact.html, it will look how it came there and refers back. 这意味着,如果它来自cnotact.html,它将看起来它是如何到达并返回的。 Easy to use if you have a second contact form on another page. 如果您在另一页上有第二个联系表,则易于使用。

I need to add that though that this may not work via secure pages. 我需要补充一点,尽管这可能无法通过安全页面起作用。 (so if your run it via https) and its possible that the header may be able to be hijacked (especially if the browser did not send the request). (因此,如果您通过https运行),则可能会劫持标头(尤其是在浏览器未发送请求的情况下)。

Here's how I'd do it: 这是我的处理方式:

I recommend using $_SESSION to store previous url like this: 我建议使用$_SESSION这样存储以前的网址:

page1.php page1.php

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

page2.php page2.php

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

$_SESSION kind of works like cookies, but a lot better. $_SESSION的工作方式类似于Cookie,但效果要好得多。 Easier to use as it's just using an array - more info can be found here: http://uk1.php.net/manual/en/reserved.variables.session.php 由于仅使用数组,因此更易于使用-可以在此处找到更多信息: http : //uk1.php.net/manual/en/reserved.variables.session.php

Are you sure you have a button with the submit name attribute? 您确定具有submit名称属性的按钮吗? if yes maybe try this : 如果是,也许试试这个:

<?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 no then your code never enters the if block 如果没有,那么您的代码将永远不会进入if块

try using JavaScript window.location to redirect Note : wrap window.location.href('contact-us.html'); 尝试使用JavaScript window.location重定向注意:wrap window.location.href('contact-us.html'); into script tag 进入脚本标签

example : 例如:

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