Pregunta

I moved my documents to a new host and the headers stopped working(refresh,redirect,etc).They used to work in my old server.I have googled and tried adding a ob_start before sending headers, that did not work.

Here is a part of the code...

if(isset($_GET['reflink']))
                        {
                            echo '<h3>Already Logged In<h3><p>Please logout before registering an account.Redirecting you back to where you came from...';
                            header('Refresh: 3; url="http://www.xacnr.com'.$_GET['reflink'].'"');
                        }

*It used to work before, it must be a problem with the server settings or something :|

¿Fue útil?

Solución

A short answer is:

It is not possible to send http headers after the output of anything else.

So if you want to output headers, you must do this at the beginning of your output. To be even more precise: HTTP-Headers must be the first thing of your output, if you have to send them - before anything else.

Please read the documentation: http://www.php.net/manual/en/function.header.php http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html

Please be also aware of the fact that the Refresh-Header is AFAIK not part of the official HTTP-Standard. It's a jurassic artifact from Netscape which will be still accepted and interpreted by most browsers, but this may change even without special notice.

If you need such a refresh and if you want to stay on the safe side, you should consider using the Meta-Refresh within the HTML-Header.

Please read here: http://en.wikipedia.org/wiki/Meta_refresh

BTW: It's also a bad idea to use unsanitized, unprocessed values from $_GET, $_POST etc. Your example should never be used in any public available environment.

Otros consejos

You do an output (echo) before you send the header:

echo '<h3>Already Logged In<h3><p>Please logout before registering an account.Redirecting you back to where you came from...';
header('Refresh: 3; url="http://www.xacnr.com'.$_GET['reflink'].'"');

The simple but strict rule is:

No output before the header!


From the Docs:

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top