Justin Ward
Justin Ward

Reputation: 885

PHP redirect on submit not working

Many thanks in advance for even attempting to present a solution. I'm having trouble with a simple html contact form. The form's method is set to post and it's action is sent to a php file. At the end of the PHP file the form is sent to, there is a basic location:redirection.

ex) header("Location: ../index.htm");

I configured my php.ini file (Apache) to display the php error(s). This is what I'm shown:

"Cannot modify header information - headers already sent by (output started at /home2/jwarddes/public_html/newTest/php/contact.php:2) in /home2/jwarddes/public_html/newTest/php/contact.php on line 20"

Line 20 of my code is my header("location:... redirect. This appears to be fine, yet something keeps throwing an error. Needless to say, I'm stumped.

Could someone please try their hand at a solution or kindly nudge me in the right direction?

Thanks!

Upvotes: 0

Views: 417

Answers (6)

Josh J
Josh J

Reputation: 78

I have had this problem many times before, I've seen solutions here and there which haven't worked. In my case I have put it down to the fact that I (php include) my pages into an index, which already has a header attribute.

The way I have worked around this is to use the meta method, echo '<meta http-equiv="refresh" content="0;url=<URL>" />';

To make your page look a little better, you can add a one or two second delay and do something like...

if ($ = $) { echo '(H1)Redirecting you to...(/H1)'; echo '<meta http-equiv="refresh" content="1;url=<URL>" />'; }

Upvotes: 2

Oliver M Grech
Oliver M Grech

Reputation: 3171

that is a common problem.

make sure you do not have any output (even a simple space at the top of the page) as that will send the headers prior to your redirect.

Possible Solutions are the following.

ob_start();

in the top of your page and

ob_end_flush(); 

at the bottom might solve your issue.

Also, I have this special function below which do a very smart redirect, try it out and let me know :)

/**
 * redirect()
 * 
 * Attempts to redirect the user to another page.
 * It tries to PHP header redirect, then JavaScript redirect, then try http redirect.
 * The following redirection method is only attempted if the previous redirection method attempt has failed  
 *  
 * @param mixed $url To be redirected to
 * @return nothing
 */
function redirect($url)
{
        if (!headers_sent())
        { //If headers not sent yet... then do php redirect
                header('Location: ' . $url);
                exit;
        } else
        { //If headers are sent... do java redirect... if java disabled, do html redirect.
                echo '<script type="text/javascript">';
                echo 'window.location.href="' . $url . '";';
                echo '</script>';
                echo '<noscript>';
                echo '<meta http-equiv="refresh" content="0;url=' . $url . '" />';
                echo '</noscript>';
                exit;
        }
} //==== End -- Redirect

Upvotes: 1

Matt Whitehead
Matt Whitehead

Reputation: 1801

From the manual: PHP header()

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.

Upvotes: 1

Saqib
Saqib

Reputation: 1129

its most probably because of a space at the starting or ending tag of check that I had the very similar problems when working on...

Upvotes: 0

Vijay Joseph
Vijay Joseph

Reputation: 492

There should not be any output (echo) send to browser before this line:

header("Location: ../index.htm");

Post your code, so that I can find out the exact issue

Upvotes: 1

NSF
NSF

Reputation: 2549

You can't have anything printed out to the browser before using the redirect.

Anyway if you really want to do that you can do such trick:

echo "<script>location.href='...';</script>"

But that's not recommended as whatever you print out before you redirect cannot be seen by the user so why bother letting it out?

Upvotes: 0

Related Questions