I am having a problem with URL redirection using a mail submission form.

The form is working and sends the mail successfully, the problem lies when I try to redirect the user to a thankyou.php page using header('Location: http://whatever.com/thankyou.php');

When the form is filled in and submitted, the mailform.php page executes but the redirection does not occur, I just get the blank page mailform.php.

Searching the web over my syntax for the header() script looks correct, is there anyway to debug this to see what exactly is occurring? Do I need to do anything on the server side in order to allow this function to work properly?

thanks in advance,

Gym

you can put an echo right before it to check if, first of all, that line of code is ever reached. (Of course this echo will cause the header() to throw an error if they are indeed reached, but it's just testing).

This is a really common problem that occurs when using php to redirect users. The header must be at the top of the page (first thing the browser reads). So i would suggest using the following code instead.

echo("<script>location.href = 'http://whatever.com/thankyou.php';</script>");
This just echos out a javascript redirect. It is a lot easier and more reliable than PHP.

This is a really common problem that occurs when using php to redirect users. The header must be at the top of the page (first thing the browser reads).

Wrong. The header can be anywhere in the script. The only thing is, you shouldn't output anything before the header function(not even a html tag). header("location: http://whatever.com/thankyou.php"); is just as good as echo("<script>location.href = 'http://whatever.com/thankyou.php';</script>"); P.S. header is comparatively faster since its a php function, where as in the other case, it should first 'echo' the redirect script.

Yeah I agree with Naveen. Do your redirect before any echo or print function, <doctype>, or <html> tag. This will also help keep the logic and design separate.

I have tried some PHP redirection methods and they all were giving me this error message: Warning: Cannot modify header information - headers already sent by.... on line...
Then I tried this and it works fine. You can even customize it to the number of seconds it takes before redirection occurs.

<?php
   ob_start();
   echo '<meta http-equiv="refresh" content="1;http://www.yourdomain.com/" />';
   ob_flush();
?>

The number "1" before the URL denotes the delay time before redirection.

I Know that this is an old post, but just to add asomething, in case someone wants to know...

make sure there is no output before your redirect:

1. <?php
2. header('location:http://www.google.com');

will work, while:

1.
2. <?php
3. header('location:http://www.google.com');

will likely give an error. also, so will

1. <?php
2. ob_start();
3. ob_flush();
4. header('location:http://www.google.com');

Unlikely scenario, but still

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.