I am trying to learn php (brand new) and I am trying to achieve the exact same thing found in this post which is using cookies for a counter and last visits on a website:
PHP cookie visit counter not working
but unlike the user in that post I am running into this infamous error:
"It's your first time on the server! Warning: Cannot modify header information - headers already sent by (output started at in (...) on line 17
This is line 17: setcookie('visitCount1');
Now I realized this is common and searched SO and found this post:
How to fix "Headers already sent" error in PHP
I thoroughly read and went through the possible reasons for why this might be occurring, including white spaces and inputting the noted php lines mentioned by my browser prior to any html code and also removing the end tag "?>" AND I as well tried putting ob_start() in the beginning of my code but still the same error results.
Here is the code that I am trying to run (taken from the above post):
<?php
$Month = 3600 + time();
date_default_timezone_set('EST');
setcookie('AboutVisit1', date("D M j G:i:s T Y"), $Month);
?>
<?php
if(isset($_COOKIE['AboutVisit1']))
{
$last = $_COOKIE['AboutVisit1'];
echo "Welcome back! <br> You last visited on ". $last . "<br>";
}
if(isset($_COOKIE['visitCount1'])){
$cookie = ++$_COOKIE['visitCount1'];
echo ("You have viewed this page" . $cookie . "times.");
}
else
{
echo "It's your first time on the server!";
setcookie('visitCount1');
}
?>
I am using netbeans 8.1 with wamp server and chrome as my browser. What else can be done to solve this?
Is it actually possible to see the cookies and track record of sessions if I am just testing this on my browser or through netbeans?
Do I have to include an html header and body or can I just put it in the php body?
On php fiddle it works (somewhat) and I get this (always the same time):
Welcome back! You last visited on Tue Aug 9 15:43:00 EST 2016 It's your first time on the server!