PHP setcookie() 与预服务器端处理?

发布于 2024-12-09 03:07:58 字数 1696 浏览 1 评论 0原文

我有一个简单的页面日志代码。基本上,它会检查是否有 VisitorID cookie,如果没有,它会查询我的数据库,获取下一个可用数字,然后我想将其设置为 VisitorID cookie。 问题是当我尝试运行它时,我得到“无法修改标头信息 - 标头已由... bla.. bla.. bla 发送”。 在 PHP 中,如果我还不知道要将 cookie 设置为什么,我该如何设置 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);                    .

?>

I have a simple pagelog code. Basicly, it checks to see if there is a VisitorID cookie, if not it queries my database, gets the next available number then I want to set that as the VisitorID cookie.
The problem is when I try to run it, I get that "Cannot modify header information - headers already sent by ... bla.. bla.. bla".
In PHP how can I set a cookie if I dont know what I want to set it to yet?

Here is my code that I include on the page before anything is written to the browser:

<?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);                    .

?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

自由范儿 2024-12-16 03:07:58

除非错误或先前的脚本发送输出,否则上述代码应该有效。另外,排除结束 php 标记 ?> 是一个很好的做法。从文件末尾开始,以消除标签后出现空格的可能性。这不会影响您的 PHP 脚本。

祝你好运

The above code should work unless there is output being sent by an error, or previous script. Also, it's good practise to exclude the closing php tag ?> from the end of your file to eliminate the possibility of whitespace after the tag. This will not effect your PHP script.

good luck

隐诗 2024-12-16 03:07:58

显然,在调用 setcookie() 函数之前会有一些输出。我想到了一些可能性:

  1. 调用 php 文件可能有一个 UTF8 BOM 标头。这些是文件开头的 2 个字节,声明该文件是 UTF8。这些字节通常不会显示在 PHP 编辑器中。
  2. 确保调用 php 文件的第一个字符是 php 块 的开始,甚至不应该有空格。
  3. 您的代码在某处发出错误消息。您可以检查一下,通过注释掉 setcookie() 函数,您应该能够看到此错误消息。

我会在调用 php 文件中查找问题,而不是在您显示的代码中查找问题。

Obviously there is some output before you call the setcookie() function. There are some possibilities that come to my mind:

  1. The calling php file may have an UTF8 BOM header. These are 2 bytes at the begin of the file, declaring that the file is UTF8. Often these bytes are not shown in a PHP editor.
  2. Make sure the very first character of the calling php file is the begin of a php block <?, there should not even be a blank.
  3. Your code gives out an error message somewhere. You can check that, with commenting out the setcookie() function, you should be able to see this error message then.

I would look for the problem in the calling php file, not in the code you have shown.

§普罗旺斯的薰衣草 2024-12-16 03:07:58

您可以打开输出缓冲。这将阻止该消息并允许您的代码运行。

使用 ob_start() 开始缓冲并 ob_flush() 将缓冲区发送到输出。

我在这里担心的是,您的代码不应该触发此错误 - 当您检查和设置此 cookie 时,您不会向页面输出任何内容,是吗?我不认为设置 cookie 会触发这样的错误。

You can turn on output buffering. That will prevent the message and allow your code to run.

Use ob_start() to begin buffering and ob_flush() to send the buffer to output.

My concern here is that your code shouldn't be triggering this error - you're not outputting anything to the page when you check and set this cookie, are you? I wasn't under the impression that setting cookies would trigger an error like this.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文