How can I redirect in PHP without header errors?

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

<?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 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 use the php header function of the user rather than meta or javascript redirection

Also supported is a list of pages requiring a login or not, if possible, this is not an option

+5
source share
7 answers

Can't you do it:

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

Or, put the included files in each of the SOME-FILE-HERE files, if possible, so you get:

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

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

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

ob_start() , . .

+10

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

+2

script header(), . , . ob_start() . - ANSI Unicode !

( , ) script, exit() ( ) .

0

footer.inc.php SOME-FILE-HERE.php ? , , , - .

0

, HTTP- . , ouput_buffering, , ob_start. , script, .

0

, ob_start() output_buffer - . , , , .

Google PHP.

0

All Articles