0

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 )

4
  • 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 sucks Commented 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. Commented 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. Commented 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...... Commented Nov 28, 2010 at 14:00

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

im already using ob_start but i dunno where exactly to use it
Or better yet, just send the header before outputting anything. It's not like the user is going to see it anyway. Using output buffering for this feels like a dirty hack, concealing a lack of structure.
@Sudantha: Call 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.
2

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

1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.