0

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

?>
hakre
  • 193,403
  • 52
  • 435
  • 836
Soren
  • 797
  • 5
  • 15
  • 32

3 Answers3

1

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

David Houde
  • 4,835
  • 1
  • 20
  • 29
  • 1
    Didn't know about the closing tag problem, thanks. An interesting discussion about this topic i found [here](http://stackoverflow.com/questions/4410704/php-closing-tag). Personally i will stick to the closing tags, but i will look out for trailing blanks as well as for preceeding blanks. – martinstoeckli Oct 10 '11 at 20:36
0

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.

Surreal Dreams
  • 26,055
  • 3
  • 46
  • 61
0

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.

martinstoeckli
  • 23,430
  • 6
  • 56
  • 87