5

How can I redirect in PHP with this setup below without getting header output errors, I understand that nothing can be printed to the browser before a header is set, I am looking for a solution, not an explanation of why it happens please.

<?PHP
// include header
include ('header.inc.php');



// In my body section file if this is a page that requires a user be logged in then
// I run a function validlogin($url-of-page-we-are-on); inside of that file
//the function is below, it outputs a redirect to login page if not logged in

// include body of page we want
include ('SOME-FILE-HERE.php');



// include footer
include ('footer.inc.php');



// here is the function that is in the body pages, it is only called on a page that we require a logged in user so there are hundreds of pages that do have this and a bunch that don't, it's on a page to page basis
function validlogin($url) {
    if ($_SESSION['auto_id'] == '') {
        $msg = 'Please login';
        $_SESSION['sess_login_msg'] = $msg;
        $_SESSION['backurl'] = $url;
        $temp = '';
        header("Location: /");
        exit();
    }
}
?>

I would like to user php's header function and not a meta or javascript redirect

Also maintainning a list of pages that require login or not is not an option here if possible

7 Answers 7

10

Use ob_start() in the first line even befor the include. so you can set headers anytime.

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

2 Comments

ob_start() does solve this problem but I hate using it, I guess I need to understand how it works better, it requires the whole page be saved to memory instead of just displaying it correct?
not directly. php uses a outputbuffer at all the times. but it works diffrent. the normal buffer can be flush'ed to browser at any times. Using outputbuffering has only one downside, you can't use flush() thats all.
2

Can't you just do this:

<?php
validlogin($url); // call the function here
include ('header.inc.php');
include ('SOME-FILE-HERE.php');
include ('footer.inc.php');
?>

Or, put the include files in every one of the "SOME-FILE-HERE"-type files, if that's possible, so you end up with:

<?php
validlogin($url); // call the function here
include ('header.inc.php');
?>

<h1>Page heading</h1>
...page content etc...

<?php
include ('footer.inc.php');
?>

1 Comment

sorry II had another question regarding the same issue, I didnt realize I was on this one, anyways that is the way I ended up doing it earliar and it worked pretty well
2

use { echo '<META HTTP-EQUIV="Refresh" Content="0; URL=process.php">';}

Comments

0

As long as you have no script output before the header() function you should be fine. Check there are no echo's or whitespace. Also putting ob_start() at the beginning can help. sometimes there is invisible whitespace - changing the format of your document to ANSI or Unicode may help!

As a note (although I think you already know) header does not terminate the script so the exit() (which you have) is a definite requirement.

3 Comments

there IS output, In my example my whole entire header file is output before I know if I am on a page that needs to be logged in or not!
Surely the validlogin () function should be called before the header is included then?
not possible as I also mentioned, not all pages require a login and the pages are built dynamicly
0

Does the footer.inc.php and SOME-FILE-HERE.php write to the response stream immediately? Because if so, this won't work as you will have already written something before you sent the headers.

3 Comments

exactly, now you understand the question =)
ah, well, that becomes more challenging :) There is a function, and I cannot, for the life of me remember it, that forces the processor to cache the response until you give the command to send it all, I'll try to look that up for you because that might solve your problem...
jasondavis, the answer from Gumbo is correct although make sure you call ob_end_flush() at the end of the script to send the buffered response stream. Basically, ob_start(); //all of your code including any includes ob_end_fush();
0

You need to buffer the ouput so that the HTTP header is not send on the first output. You can either buffer any ouput implicitly by enabling ouput_buffering or explicitly by calling ob_start. But the latter has to be called before the first output, so ideally in the first line of the script that’s initially called.

Comments

0

As already mentioned by the others use ob_start() or the output_buffer-setting to buffer the output. Apart from that it's from my point of view not a good practice to output content in the middle of functional code but this is a another topic.

You can find more information at Google or in this Article about Output Buffering in PHP.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.