简体   繁体   中英

Redirect to previous page with php

The submit button is in contact-us.html , the code is in contact-us.php .

I tried to use the 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'); )

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.

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. 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).

Here's how I'd do it:

I recommend using $_SESSION to store previous url like this:

page1.php

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

page2.php

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

$_SESSION kind of works like cookies, but a lot better. 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

Are you sure you have a button with the submit name attribute? 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

try using JavaScript window.location to redirect Note : 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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM