可能重复:
PHP 已发送的标头
我解决了这个问题,但由于我不明白是什么原因造成的,我不能确定它是否真的解决了。
在您登录之前,我的 PHP 站点会在主页上显示最新的活动。我最近修改了逻辑以包含更多类型的活动。看起来它有效,但我在登录时收到以下错误:
Warning: Cannot modify header information - headers already sent by
(output started at header.php:75)
in index.php on line 26
我认为此错误消息具有误导性,因为我修复它的方法是在 MySQL 查询中将“LIMIT 10”更改为“LIMIT 9”,以使活动显示在主页上。
public function getLatestActivity()
{
$sql = "SELECT 'Debate' AS Type, d.Debate_ID AS ID, CONCAT('debate.php?debate_id=', d.Debate_ID) AS URL, d.Title, d.Add_Date
FROM debates d
UNION SELECT 'Discussion' AS Type, d.Discussion_ID AS ID, CONCAT('discussion.php?discussion_id=', d.Discussion_ID) AS URL, d.Title, d.Add_Date
FROM discussions d
UNION SELECT 'Petition' AS Type, p.Petition_ID AS ID, CONCAT('petition.php?petition_id=', p.Petition_ID) AS URL, p.Petition_Title AS Title, p.Add_Date
FROM petitions p
ORDER BY Add_Date DESC
LIMIT 9";
try
{
$stmt = $this->_db->prepare($sql);
$stmt->execute();
$activity = array();
while ($row = $stmt->fetch())
{
$activity[] = '<span style="font-size: x-large"><strong>Latest activity</strong></span><br /><span style="font-size: large">' . $row['Type'] . ': <span style="color: #900"><a href="' . $row['URL'] . '" style="color: #900">' . $row['Title'] . '</a></span></span>';
}
$stmt->closeCursor();
return $activity;
}
catch(PDOException $e)
{
return FALSE;
}
}
这就是我对该函数返回的数据所做的事情。它遍历数组并每 4 秒显示一个新项目。
<?php $latest_activity = $pdb->getLatestActivity(); ?>
<script type="text/javascript">
var activity = <?php echo json_encode($latest_activity); ?>;
var index = -1;
$(function()
{
getLatestActivity();
});
function getLatestActivity()
{
index = (index + 1) % activity.length;
var div = document.getElementById('divLatestActivity');
if (index < activity.length)
{
div.innerHTML = activity[index];
}
setTimeout("getLatestActivity()", 4000);
}
</script>
为什么将“LIMIT 10”更改为“LIMIT 9”可以解决“无法修改标头信息”问题?