2012-04-07 98 views
0

Possible Duplicate:
Headers already sent by 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]: Cannot send session cache limiter - headers already sent

有人可以發佈一個很好的解決方案來檢查會話是否開始,如果沒有重定向到登錄頁面,否則留在頁面上? Txanks

+2

在打開'<?php'之前刪除空格。它被視爲發送到瀏覽器的文字輸出,之後PHP不能設置任何額外的標題(如會話cookie) – 2012-04-07 00:46:58

+0

如果在顯示的代碼中的<?php'之前沒有空格(根據您的代碼)必須在你的'config.inc.php'中要麼是空格,要麼在該文件中做一些其他的輸出。 – 2012-04-08 06:55:02

+0

或auto_prepend_file – Leigh 2012-06-15 13:08:06

回答

5

這是你的問題:

<?php 
^^^^ 

頭已經被髮送之前,你不能輸出任何內容;包括空白。

+0

就在這裏沒有空白區域?還有其他想法嗎? – 2012-04-07 08:31:17

+0

不,我只是在頁面上格式化就是這樣 – 2012-04-07 12:50:22

1

每當你看到headers already sent,這意味着有些東西會在PHP發送頭文件之前輸出。

就你而言,它是PHP標籤之前的其他來源,正如Oli Charlesworth所說的。

相關問題