20

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();
        }
    }
}
?>
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
dee_styles
  • 209
  • 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
  • 2
    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

9 Answers9

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,834
  • 6
  • 44
  • 66
22

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,221
  • 14
  • 5
6

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
  • 560
  • 1
  • 5
  • 21
3

I solved this with:

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

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

2

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,882
  • 3
  • 20
  • 31
jay.jivani
  • 1,560
  • 1
  • 16
  • 33
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
  • 99
  • 10
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
  • 356
  • 5
  • 16
-1

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ù
  • 6,013
  • 5
  • 30
  • 38
-1

You can use this short code in a php form, which first checks if headers have been sent using php code, and if they are, redirect using a JavaScript code echo, if headers have not been sent, php redirects to the page stored in the $URL variable. Enter the URL of the page you want to redirect to in the first line only: $URL = "your_url.html; the variable $URL will be used in the rest of the code to represent your URL:

$URL = "http://your_url.html";

   if( headers_sent() ) 

 { 

  echo("<script>location.href='$URL'</script>"); 

 } else { 

    header("Location: $URL");
 }

exit;
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 07 '23 at 06:15
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/34643580) – Yogendra Jul 08 '23 at 09:20