21

This contact.php form was working to handle a submit and then redirect to a new page and then all of a sudden just stopped working. I have tried adding error handling and also moving header to the top in front of all other, but neither works. The form is still submitting the data as expected, it's just the redirect that doesn't work. Any ideas would be appreciated.

<?php 
include 'config.php';
$post = (!empty($_POST)) ? true : false;
if($post)
{
    $email = trim($_POST['email']);
    $subject = "Downloaded Course Units";
    $error = '';
    if(!$error)
    {
        $mail = mail(WEBMASTER_EMAIL, $subject, $message, 
        "From: ".$email."\r\n"
        ."Reply-To: ".$email."\r\n"
        ."X-Mailer: PHP/" . phpversion());
        if($mail)
        {
            echo 'OK';
            header('location: http://www.google.com.au/');
            exit();
        }
    }
}
?>
ʇolɐǝz ǝɥʇ qoq
  • 669
  • 1
  • 14
  • 30
dee_styles
  • 211
  • 1
  • 2
  • 3
  • 1
    Please remove exit(); – Arunkumar Srisailapathi Nov 25 '14 at 09:55
  • 1
    you may find this useful: -> http://stackoverflow.com/questions/768431/how-to-make-a-redirect-in-php – Marco Mura Nov 25 '14 at 09:55
  • 1
    PHP headers are the right way to do this. Javascript / Meta tags are not as reliable. Remove the echo 'OK'; line (and any other page output generating lines) otherwise the header redirect will not work (unless you have output buffering on in the php.ini)... exit() is required otherwise script execution will continue! – Paul Norman Nov 25 '14 at 12:00

8 Answers8

31

Use javascript.

Instead of

header('location: http://www.google.com.au/');

Use,

?>
<script type="text/javascript">
window.location.href = 'http://www.google.com.au/';
</script>
<?php

It will redirect even if something is output on your browser.

But, one precaution is to be made: Javascript redirection will redirect your page even if there is something printed on the page.

Make sure that it does not skip any logic written in PHP.

Pupil
  • 23,528
  • 5
  • 42
  • 64
23

Replace the header('location: http://www.google.com.au/'); line with the below code to redirect in php without using header function.

$URL="http://yourwebsite.com/";
echo "<script type='text/javascript'>document.location.href='{$URL}';</script>";
echo '<META HTTP-EQUIV="refresh" content="0;URL=' . $URL . '">';

If you're wondering that why I have used both Meta tag and JavaScript to Redirect, then the answer is very simple.

If JavaScript is Disabled in the Browser, then meta tag will redirect the page.

Heraclitus
  • 80
  • 11
Shubham Kumar
  • 1,193
  • 13
  • 5
7

header not working after include, echo. try again without include, echo. OR instead of function header use

echo '<meta http-equiv="refresh" content="0; URL=http://www.google.com.au/">';
Javlon Tulkinov
  • 542
  • 1
  • 5
  • 20
4

I solved this with:

function GoToNow ($url){
    echo '<script language="javascript">window.location.href ="'.$url.'"</script>';
}

Use : GoToNow ('http://example.com/url-&error=value');

3

If you want to redirect to another page after html code then use location.href javascript method.

Refer to this sample code:

<html>
    <head>
        <title> Using the href property of the Location object</title>
    </head>
    <body>
        <script language="JavaScript">
        <!--
            function show(){
                 document.location.href ="http://www.java2s.com";
            }
        -->
        </script>
        <form name="form1">
            <br>
            <input type="button" name="sethref" value="Set href" onClick='show()'>
            <br>
        </form>
    </body>
    </html>
Skully
  • 2,160
  • 3
  • 17
  • 30
jay.jivani
  • 1,526
  • 1
  • 15
  • 32
0

In addition to the above answers, you can use the following JavaScript code for both the document and the main window, as well as in iframes:

<script type="text/javascript" >
        window.open('http://www.google.com.au/', '_self');
</script>

_self Opens the linked document in the same frame as it was.

In the above command, the values of the second parameter are similar to the values of the target attribute in the tag A(<a></a>) in HTML, which can use the following values in addition to the _self value:

_blank Opens the linked document in a new window or tab

_parent Opens the linked document in the parent frame

_top Opens the linked document in the full body of the window

framename Opens the linked document in a named frame

sidoco
  • 89
  • 9
0

You should use the exit() function to stop every execution of script below your redirect function if you're using javascript. Else, if someone off the javascript in their browser, the statements below will be executed.

Saugat Jonchhen
  • 362
  • 5
  • 16
0

Just a quick script using HEREDOC with PHP which allows you to store the redirection script into a variable and is quite readable and improve reusability.

    $homePage = "'http://localhost:8000/'";
    $redirect = <<<REDIRECT
    <script type="text/javascript">
        window.location.href = $homePage;
    </script>
    REDIRECT;

That later in your code, as soon as you need spit out with echo

echo $redirect;

Even better, we can add it in a function called redirect with some default variables too.

function redirect(string $slug = '/', string $root='http://localhost:8000'): string
{
    return <<<REDIRECT
        <script type="text/javascript">
            window.location.href = '$root$slug' ;
        </script>
        REDIRECT;
}

Go to default http://localhost:8000/

 echo redirect();

Go to Google Maps

echo redirect(slug : '/maps', root: 'https://www.google.com');
Federico Baù
  • 3,772
  • 4
  • 18
  • 27