Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect in PHP without use of header method

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();
        }
    }
}
?>
like image 899
dee_styles Avatar asked Nov 25 '14 09:11

dee_styles


People also ask

How do I automatically redirect in PHP?

To set a permanent PHP redirect, you can use the status code 301. Because this code indicates an indefinite redirection, the browser automatically redirects the user using the old URL to the new page address.

How can I redirect a URL to another URL in PHP?

Redirection from one page to another in PHP is commonly achieved using the following two ways: Using Header Function in PHP: The header() function is an inbuilt function in PHP which is used to send the raw HTTP (Hyper Text Transfer Protocol) header to the client.

Which function is used to redirect a page in PHP?

The header function in PHP can be used to redirect the user from one page to another. It is an in-built function that sends raw HTTP header to the destination (client).

How redirect to another page after submitting a form in PHP?

Now in PHP, redirection is done by using header() function as it is considered to be the fastest method to redirect traffic from one web page to another. The main advantage of this method is that it can navigate from one location to another without the user having to click on a link or button.


3 Answers

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.

like image 195
Pupil Avatar answered Oct 11 '22 09:10

Pupil


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.

like image 37
Shubham Kumar Avatar answered Oct 11 '22 11:10

Shubham Kumar


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/">';
like image 35
Javlon Tulkinov Avatar answered Oct 11 '22 11:10

Javlon Tulkinov