Как я могу перенаправить в PHP без ошибок заголовка?

Как я могу перенаправить в PHP с этой настройкой ниже, не получая ошибок вывода заголовка, я понимаю, что ничто не может быть напечатано в браузере, прежде чем заголовок установлен, я ищу решение, а не объяснение, почему это происходит, пожалуйста.

<?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();
    }
}
?>

Я хотел бы использовать функцию заголовка php, а не перенаправление мета или javascript

Также поддержание списка страниц, которые требуют входа или нет, здесь не вариант, если это возможно

Ответы на вопрос(7)

Ваш ответ на вопрос