2011-08-11 84 views
-3

Possible Duplicate:
Headers already sent in phpPHP錯誤「無法發送會話緩存限制器 - 已經發送了頭......」

我收到以下錯誤:

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/content/t/a/t/brians/html/about.php:9) in /home/content/t/a/t/brians/html/includes/sidebar.php on line 2

在在about.php我有以下的代碼來檢索該文件的sidebar.php:

<?php include('includes/sidebar.php'); ?> 

sidebar.php的,我有以下代碼:

<?php 
session_start(); 

$data=array("user1"=>array("url"=>"file1.php","password"=>"pass1"), 
"user2"=>array("url"=>"file2.php","password"=>"pass2")); 

if(isset($_POST['username']) && isset($_POST['password'])) { 
if($data[$_POST['username']]['password'] == $_POST['password']) { 
$_SESSION['username'] = $_POST['username'] . " " . $_POST['password']; 
header('Location: ' . $data[$_POST['username']]['url']); 
} else { 
login('The username or password is incorrect. <br />'); 
} 
} else { 
login(); 
} 
?> 

<?php 

function login($response='This is the default message, if there is none provided') { 

?> 

<img src="../images/clientlogin_header.png" alt="Client Login"></img> 
<p> 
<div id="login-area"> 
      <form action="" method="post"> 
       <label for="username">username</label> 
       <input type="text" name="username" /> 

       <label for="password">password</label> 
       <input type="password" name="password" /><br /> 

       <input type="submit" name="Login" value="Login" class="submit-button" /> 
      </form> 
      <div style="clear: both;"></div> 
</div> 
</p> 
<?php } ?> 

我不能完全解決這個錯誤。任何人都可以給我建議嗎?

+4

請搜索棧溢出該錯誤信息 - 它被問了已經超過5000次了:) http://stackoverflow.com/search?q=+Cannot+send+session+cache+limiter+-+headers+already+sent –

回答

1

會話通過包含會話標識符的cookie與訪問者連接到您的站點。

在HTTP協議中,Cookie通過HTTP頭髮送,HTTP頭必須出現在包含頁面HTML的HTTP響應主體之前。

爲了開始一個會話(session_start()),您不能發送任何輸出,因爲這樣Cookie頭就不能發送 - 它不會在輸出之前根據需要發送。

因此,在編寫session_start()之前,請確保代碼中沒有輸出,包括來自其他文件的HTML,空格或空白行。您可能需要移動該行代碼,以便在about.php的輸出之前執行該代碼。

2
<?php include('includes/sidebar.php'); ?> 

應該有任何輸出之前在about.php

或者您可以使用ob_start功能使用緩衝和fon't想想

相關問題