【发布时间】:2010-11-18 08:05:50
【问题描述】:
如何使用下面的设置在 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 的 header 函数,而不是 meta 或 javascript 重定向
如果可能的话,维护一个需要登录或不需要登录的页面列表不是一个选项
【问题讨论】: