- 论坛徽章:
- 0
|
Cannot modify header information - headers already sent by 17
<?php
session_start();
header('Content-Type: text/html; charset=utf-8');
$historycount = 20; //保存历史记录数量
function browse_history($id)
{
global $historycount;
//判断参数$id是否为空或为数字
if(empty($id) || !is_numeric($id)) return;
//判断用户历史记录是否存在
if(!isset($_COOKIE['history']))
{
$history = serialize(array($id));
setcookie("history",$history);
return;
}
$history = @unserialize(array($_COOKIE['history']));
if($history)
{
//如果history记录多于系统允许的值,则删除最后一个
if(count($_COOKIE['history']) >= $historycount)
{
$tmp = array_pop($history);
}
array_unshift($history,$id);
setcookie("history",serialize($history)); //把最新的插在最前面
}
else
{
setcookie("history","",time()-60);
if (is_array($_COOKIE['history']))
{
foreach ($_COOKIE['history'] as $name => $value)
{
setcookie("history[$name]","", time()-60);
}
}
$history = serialize(array($id));
setcookie("history",$history);
}
} // function end
/*----------------------下面是测试--------------------------*/
print_r($_COOKIE['history']);
echo '<br /><hr />';
if(isset($_GET['id']))
{
browse_history($_GET['id']);
echo '编号为:'.$_GET['id'].'的浏览记录已经存入cookie<br />';
}
for($i=0;$i<100;$i++)
{
echo "<a href=\"history.php?id=$i\">product $i</a><br>\r\n";
}
?> |
|
|