I have a complex PHP script where in multiple parts I have a header_redirect
. For example, to change the language, etc. Everything works as it should, but I noticed that especially on mobile devices, I can see the code of the page rendered (HTML only, no CSS) before it redirects. Or even on desktop devices, but on those I get only a blank page and can see the page rendered only if I view the source code and hit cancel really fast before it redirects.
I know, it's most likely because the coding structure of the page is wrong and I use header_redirect
somewhere in the middle of the code instead of checking it before any content is rendered. But is there an easy way to turn off this output in any way without rewriting the entire script?
I checked in php.ini and
output_buffering
Is set to 'off', but maybe there's another setting?
You do it the wrong way. Output buffering needs to be set on (That this is bad practise, you already know).
//Starts Buffering
ob_start();
//Your Code here
//...
//...
//Sends the Buffered Content to the browser, if redirected first, this will never shown
ob_end_flush();
flush();
But I think you have something like a meta or javascript redirect. If it is a PHP Header, you should get a error like
Can not send headers, already send on line xy...
Maybe the root of your Problem is some Cache directive.
You redirect with PHP this way:
header('HTTP/1.1 302 Found');
header('location: /Target.php');
Or 301
if it is permanent.