0

可能重复:
PHP 已发送的标头

在这里我有一个奇怪的问题来保护我的页面,检查会话是否启动,如果没有重定向到登录,

    <?php 
require_once ('includes/config.inc.php'); 

// Start output buffering:
ob_start();

// Initialize a session:
session_start();

// Check for a $page_title value:
if (!isset($page_title)) {
    $page_title = 'User Registration';
}

// If no first_name session variable exists, redirect the user:
if (!isset($_SESSION['first_name'])) {

    $url = BASE_URL . 'index.php'; 
    ob_end_clean(); // Delete the buffer.
    header("Location: $url");
    exit(); // Quit the script.

}
?>

我收到了这个错误:在第 8 行:

session_start() [function.session-start]:无法发送会话缓存限制器 - 标头已发送

有人可以发布一个好的解决方案来检查会话是否已启动,如果没有重定向到登录页面,则留在页面上?Txanks

4

2 回答 2

5

这是你的问题:

    <?php 
^^^^

在标头发送之前,您不能输出任何内容;包括空格。

于 2012-04-07T00:46:15.037 回答
1

无论何时,您都会看到headers already sent,这意味着在 PHP 发送标头之前正在输出某些内容。

在您的情况下,它是 Oli Charlesworth 所说的 PHP 标记之前的附加源。

于 2012-04-07T00:48:54.177 回答