【发布时间】:2012-12-11 23:43:12
【问题描述】:
我有一个简单的页面日志代码。基本上,它会检查是否有 VisitorID cookie,如果没有,它会查询我的数据库,获取下一个可用号码,然后我想将其设置为 VisitorID cookie。 问题是当我尝试运行它时,我得到“无法修改标头信息 - 标头已由 ... bla .. bla.. bla 发送”。 在 PHP 中,如果我不知道要设置什么,我该如何设置 cookie?
这是我在将任何内容写入浏览器之前包含在页面中的代码:
<?php
$Browser = $_SERVER["HTTP_USER_AGENT"];
$TheTable = "PageVisits";
if (strripos($Browser,"mozilla") < 0||
strripos($Browser,"search") > 0 ||
strripos($Browser,"bot") > 0 ||
strripos($Browser,"scoutjet") > 0 ||
strripos($Browser,"ask jeeves/teoma") > 0 ||
strripos($Browser,"slurp") > 0 )
{
$TheTable = "BotVisits";
}
$IPAddress = $_SERVER["REMOTE_ADDR"];
$AcceptedTypes = $_SERVER["HTTP_ACCEPT"];
$Referer = $_SERVER["HTTP_REFERER"];
$VisitorID = $_COOKIE["VisitorID"];
//Get VisitorID
if (strlen($VisitorID) == 0)
{
$SqlStr = "SELECT IF(IsNull(MAX(VisitorID)), 1, MAX(VisitorID) + 1) AS NewVistorID " .
"FROM " . $TheTable . " ";
$con = mysql_connect("DBServer","DBUserName","DBPassword");
mysql_select_db("ratpackc_ratpack", $con);
$result = mysql_query($SqlStr);
$VisitorID = mysql_result($result, 0);
mysql_close($con);
}
//Update page log
$SqlStr = "INSERT INTO " . $TheTable . " " .
"(VisitorID, IPAddress, ThePage, Referer, Browser, AcceptedTypes) " .
"VALUES (" . $VisitorID . ",'" . $IPAddress . "','" . $ThisPage . "','" . $Referer . "','" . $Browser . "','" . $AcceptedTypes . "')" ;
$con = mysql_connect("DBServer","DBUserName","DBPassword");
mysql_select_db("ratpackc_ratpack", $con);
mysql_query($SqlStr);
mysql_close($con);
$CookieExpire = time()+31536000;
setcookie("VisitorID", $VisitorID, $CookieExpire); .
?>
【问题讨论】: