'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="[email protected]";
    $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>")
}


Solution 1:[1]

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

Solution 2:[2]

Are you sure you have a button with the submit name attribute? if yes maybe try this :

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

    $recipient="[email protected]";
    $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

Solution 3:[3]

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>";

Solution 4:[4]

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

Solution 5:[5]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 user1
Solution 3 Machavity
Solution 4
Solution 5 ehsan karimi