0

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!

xan
  • 65
  • 1
  • 6
  • The code you posted contains a white space. Look at `setcookie('AboutVisit1', date("D M j G:i:s T Y"), $Month);` and then look what you do - you close the php with `?>`, then you have a white space, then you open with ` – N.B. Aug 09 '16 at 21:17

4 Answers4

1

The code you posted contains a white space. Look:

setcookie('AboutVisit1', date("D M j G:i:s T Y"), $Month);
?>

 <?php

See the space between closing and opening?

N.B.
  • 13,688
  • 3
  • 45
  • 55
1

The problem is that you are echoing messages before you are sending the last cookie: setcookie('visitCount1'); is being sent to the browser after you already ran echo "It's your first time on the server!"; (or echo "Welcome back! <br> You last visited on ". $last . "<br>";). Make sure not to use echo functions before sending the last cookie.

Edit: as others pointed out there also is a whitespace in your code.

JohannesB
  • 1,995
  • 21
  • 35
1

Given you are brand new, here are a few tips

<?php

// if you are going to change the timezone, best to always make this first, so it effects everything in the script
date_default_timezone_set('EST');

// 3600 seconds = 1 hour not Month
// give your variables proper names it will help if you get into that habit from day one
$one_hour = 3600;
$expires = $one_hour + time();

// check if visit cookie exists and increment it, otherwise, this must be visit 1
$count = isset($_COOKIE['visitCount1']) ? ++$_COOKIE['visitCount1'] : 1;

// set cookies at the start, before outputting anything
// i would also recommend setting the path, as that can catch you out if you have a deep directory structure, / will mean the cookie works for the whole site
setcookie('AboutVisit1', date("D M j G:i:s T Y"), $expires, '/');
setcookie('visitCount1', $count, $expires, '/');

if(isset($_COOKIE['AboutVisit1']))
{
    $last_visit = $_COOKIE['AboutVisit1'];
    // you don't need to use . to concatenate variables if you use double "
    echo "Welcome back! <br> You last visited on $last_visit <br>";
}

if(isset($_COOKIE['visitCount1'])){
    // you dont' need () when you do echo
    // you don't need to use . to concatenate variables if you use double quotes "
    echo "You have viewed this page $count times.";
} else {
    echo "It's your first time on the server!";
}

// you don't need trailing ?>
bumperbox
  • 10,166
  • 6
  • 43
  • 66
  • Ok, if I want to visit the site again, it does not show my count going up and last visits, how do I make this happen through my browser or netbeans? – xan Aug 09 '16 at 23:52
  • Check your cookies in the browser and see what is being set, that code is tested and works here ok – bumperbox Aug 10 '16 at 03:59
0

the setcookie() function should be called before you output anything else. So there is some parts in your code that is outputting something before that setcookiehas been called.

Hardy
  • 5,590
  • 2
  • 18
  • 27