Is there is a way to redirect pages in PHP without using header('Location:'.''); becouse it always make problems with sessions (:Cannot send session cache limiter - headers already sent )
- It's never make any problem with sessions. "Location" HTTP header is the only proper way. Though you'll get your lame answer anyway, just because this site sucksYour Common Sense– Your Common Sense2010-11-28 13:37:22 +00:00Commented Nov 28, 2010 at 13:37
- @Col. Shrapnel That's so true. Unexperienced coders tend to always get rid of the symptoms rather than the cause. We need to teach them to get this straight.aefxx– aefxx2010-11-28 13:43:45 +00:00Commented Nov 28, 2010 at 13:43
- 1@Col. Shrapnel You frequent this site quite a bit for someone with a strong hatred for it. @Sudantha You have a couple of fairly accurate answers to choose from.Russell Dias– Russell Dias2010-11-28 13:44:36 +00:00Commented Nov 28, 2010 at 13:44
- just turn off error reporting completely, one guy I went to uni with thought that was a valid solution......martynthewolf– martynthewolf2010-11-28 14:00:25 +00:00Commented Nov 28, 2010 at 14:00
3 Answers
There are other options for redirecting in HTML (like using META or JavaScript). But it’s not a proper HTTP redirection.
So instead of looking for an alternative you should better get the HTTP variant working. In this case you can only modify the HTTP header if it hasn’t been sent already. And that happens whenever you do some output (either explicitly or implicitly). You can prevent that by buffering the output calling the output control’s ob_start before any output had happened, possibly directly at the start of the script file you have requested:
<?php ob_start(); // rest of the file Make sure that there is nothing before the opening PHP tag <?php like blank lines or a UTF BOM.
3 Comments
ob_start before anything else. And make sure that there is nothing before the opening <?php. Take a look at the position mentioned in the error message to see where the output has started and fix it there.No, without header you're out of luck (you could use Apache's mod_rewrite but that would happen before your file is even touched).
Why not use ob_start (output buffering). See the PHP manual for a quick-start.
Good luck
Comments
As has been mentioned before use ob_start() right at the very top of your application page. Do not implement the dodgy javascript functions mentioned in other answers, I'd say they are extremely bad practice. PHP has been around for a while now so its tested and works, you might have erroneous white space or be outputting something to the browser then trying to modify header information which you cant do. So:
use ob_start check for erroneous white space and delete it