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();
}
}
}
?>
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.
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.
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).
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.
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.
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.
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/">';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With