PHP header("Location: $url");


PHP header("Location: $url");

可能的重复项:
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]:无法发送会话缓存限制器 - 标头已发送

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

这是你的问题:

    <?php 
^^^^

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

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

在您的情况下,正如 Oli Charlesworth 所说,它是 PHP 标签之前的其他来源。