This is my first attempt at setting cookies and am having a terrible time.
Here is the code:
<?php
if (isset($_COOKIE['trans']))
{
$transNum=$_COOKIE['trans'];
}
else
{
$countme = ("countme.txt");
$hits = file($countme);
$hits[0] ++;
$fp = fopen($countme,"w");
fputs($fp , "$hits[0]");
fclose($fp);
$transNum = Date('Ymd')."-".$hits[0];
setcookie('trans',$transNum,time()+432000);
}
?>
<html>
<head></head>
<body><?= $transNum; ?></body>
</html>
I am trying to create a number, $transNum, and store it in a cookie that will expire in 7 days. I am using a basic counter, which is code I found as an example and is colored red, so that every time a user views the page and does not have the cookie set, it will add to the counter and set the cookie for that user.
Originally, I was getting the error:
Cannot modify header information - headers already sent by....on line 19.
that was the "setcookie()" line. The counter was working fine and giving me the $transNum perfectly.
After trying several different things to try to fix the header issue, I managed to create a new error.
unexpected T_variable on line 15.
Reading through other threads, and past experience, this usually comes up with a simple ; or bracket missing. I have gone over and over and over the code and can't figure out what I did to get the new error, let alone what I can do to rid myself of the first error. Maybe some fresh eyes can see what I cannot. Can someone please help me figure out what I need to do?