Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP setcookie() with pre-server side processing?
    primarykey
    data
    text
    <p>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?</p> <p>Here is my code that I include on the page before anything is written to the browser:</p> <pre><code>&lt;?php $Browser = $_SERVER["HTTP_USER_AGENT"]; $TheTable = "PageVisits"; if (strripos($Browser,"mozilla") &lt; 0|| strripos($Browser,"search") &gt; 0 || strripos($Browser,"bot") &gt; 0 || strripos($Browser,"scoutjet") &gt; 0 || strripos($Browser,"ask jeeves/teoma") &gt; 0 || strripos($Browser,"slurp") &gt; 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); . ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload