0

Hey I'm trying to make a counter of how many times someone is visiting my webpage and when it was the last time he visited. The last time visited is working fine. I'm having a problem to display how many times he has been on the page however. There's a bad display and it seems like I may be missing and incrementation somewhere but I can't seem to figure it out:

<?php
$Month = 3600 + time();

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

 <?php
if(isset($_COOKIE['AboutVisit']))
{
$last = $_COOKIE['AboutVisit'];
echo "Welcome back! <br> You last visited on ". $last . "<br>";
 $cookie = ++$_COOKIE['AboutVisit'];


 echo ("You have viewed this page" . $cookie . "times.");
}
else
{
echo "It's your first time on the server!";
}
?>

EDIT: NEW CODE

<?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');
}
?>
Mr Lister
  • 45,515
  • 15
  • 108
  • 150
R.DM
  • 105
  • 1
  • 11
  • 2
    `$_COOKIE['AboutVisit']` has a date in it (_or so it would seem_) so `++$_COOKIE['AboutVisit']` is not likely to work. You need a seperate cookie for the visit counter – RiggsFolly Apr 11 '16 at 08:36
  • That comment was really helpful. the display now doesnt show up as a date. I set a cookie in my last else statement with only a name and tried incrementing it in the corresponding statement $cookie = ++$_COOKIE['AboutVisit']; (changed the name obviously) – R.DM Apr 11 '16 at 08:43
  • but the incrementing doesnt work. it stays at the same number.. – R.DM Apr 11 '16 at 08:48
  • 1
    Add your new code under the orifinal question as an UPDATE – RiggsFolly Apr 11 '16 at 08:50
  • Okay done. So i tried with new cookies, and for the visit counter, it seems to always be returning false that i have set the cookie as it always goes to say "It's your first time on the server!" – R.DM Apr 11 '16 at 08:54

3 Answers3

1

You forgot to call setcookie(). Take a look at http://www.w3schools.com/php/func_http_setcookie.asp

eol
  • 23,236
  • 5
  • 46
  • 64
  • I did set one up in a previous php just before it, though it didnt appear in my question for some reason. – R.DM Apr 11 '16 at 08:38
  • @R.DM Not all code in your question was properly indented, so the editor didn't show it. I've fixed the indentation. – Mr Lister Apr 18 '16 at 19:47
1

You will need 2 cookies, one for the date and one for the counter.

Also remember that cookies must be sent before any other output is sent to the browser, or they will be lost (and an error generated), so it would be better to store your messages in variables and output them once you have completed ALL cookie processing.

It would also be simpler to save the time() in the date cookie and format its output only for viewing on the page.

<?php
if(isset($_COOKIE['LastVisitDate']))
{
    $msg1 = 'Welcome back! <br> You last visited on ' . date('d/m/Y H:i:s', $_COOKIE['LastVisitDate'])  . '<br>';
} else {
    $msg1 = "It's your first time on the server!";
}
setcookie('LastVisitDate', time(), time()+3600);   // reset to now

if ( isset($_COOKIE['VisitCount']) ) {
    $msg2 = "You have viewed this page {$_COOKIE['VisitCount']} times.";
    setcookie('VisitCount', (int)$_COOKIE['VisitCount']+1, time()+3600 );
} else {
    setcookie('VisitCount',1, time()+3600 );
    $msg2 = 'Thanks for visiting, I hope you enjoy your first visit';
}

echo $msg1;
echo $msg2;
?>

Also note that cookies can be blocked, by the browser, so this is not a completely reliable method of tracking users.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • Thank you for your answer. really grateful. I also like your way of thinking for the date. However, I tried out your code, and it seems to have the same mistake.. it won't increment the number of views... it always stays at 1 and I really have no idea why. I tried pre-inc, but that didnt change anything.. – R.DM Apr 11 '16 at 09:10
  • Ok let me have another look – RiggsFolly Apr 11 '16 at 09:11
  • Thank you once again. and yes I have been told that cookies can be blocked, but I'm not so worried about that at the moment :p – R.DM Apr 11 '16 at 09:15
  • Ok, the `$_COOKIE['VisitCount']++` did not work for some reason, so i changed it to `(int)$_COOKIE['VisitCount']+1` that seems to work quite happily. Or in fact `++$_COOKIE['VisitCount']` will work, obvious when I think about it, – RiggsFolly Apr 11 '16 at 09:15
  • Great! thanks a lot. Do you have any idea why it did not work? – R.DM Apr 11 '16 at 09:19
  • 1
    `$x++` is add 1 after using variable `++$x` is add one then use value – RiggsFolly Apr 11 '16 at 09:20
0

Try This

 <?php
if(isset($_COOKIE['AboutVisit']))
{
$last = $_COOKIE['AboutVisit'];
echo "Welcome back! <br> You last visited on ". $last . "<br>";
$value= $_COOKIE['AboutVisit']+1; //or whatever you wnt
setcookie("AboutVisit", $value);


 echo ("You have viewed this page" . $cookie . "times.");
}
else
{
echo "It's your first time on the server!";
}
?>
Faizal Saleem
  • 19
  • 1
  • 10