-1

Probably a dumb question but I just started learning PHP and now I have to do an exercise in which, on the one hand, the session variables have to count how often you have visited the page before closing the browser. And on the other hand, the cookies should count how often you have visited a website in total. So also if you have closed your web browser, the cookie should continue counting. This is my problem though. If I close my web browser and start it again, the cookie starts all over again with counting. How to solve this?

PHP File

<?php
  session_start();

  if (!isset($_SESSION['count'])) {
    $_SESSION['count'] = 1;
  } else {
    $_SESSION['count']++;
  }

  echo "You have visited this page " . $_SESSION['count'] . " times before you closed your browser";

  $count = $_SESSION['count'];

  setcookie("count", "$count", time() + 3600);

  if (!isset($_COOKIE['count'])) {
      $_COOKIE['count'] = ($_COOKIE['count'] + $_SESSION['count']);  
  } else {
      $_COOKIE['count']++;
  }
  echo "<br> In total you have visited this page " . $_COOKIE['count'] . " times";
?>
Niels
  • 9
  • 2
  • 8
  • Visited a website before closing, or total number of page requests on the website ever? So should the cookie counter be an aggregate of the session counter, or the total number of session counters that occurred for that user? – Flosculus May 24 '17 at 14:04

3 Answers3

1

You are overwritting the cookie value with the session value.

Instead check if the cookie is already set, and if so, just add to it:

  session_start();

  if (!isset($_SESSION['count'])) {
    $_SESSION['count'] = 1;
  } else {
    $_SESSION['count']++;
  }

  $count = $_SESSION['count'];

  if (!isset($_COOKIE['count'])) {
      setcookie("count", $count, time() + 3600);
      $_COOKIE['count'] = $count; //setcookie does not update the superglobal $_COOKIE

  } else {
      setcookie("count", $count + $_COOKIE['count'], time() + 3600);
      $_COOKIE['count'] += $count; //see above
  }
  //also headers (eg for setting cookies) can only be sent BEFORE the response body, so no echos
  echo "You have visited this page " . $_SESSION['count'] . " times before you closed your browser";
  echo "<br> In total you have visited this page " . $_COOKIE['count'] . " times";
Steve
  • 20,703
  • 5
  • 41
  • 67
0

I think you must set cookie with path

setcookie("count", "$count", time() + 3600, "/");
Louay Hamada
  • 781
  • 1
  • 14
  • 22
  • 1
    From [the manual](http://php.net/setcookie): *The default value is the current directory that the cookie is being set in.* Default should be fine for current use case. – Álvaro González May 24 '17 at 14:19
0

Most major browsers bundle developer tools that include a cookie inspector. In Firefox it's called Storage Inspector. It can be found in program menus but default short-cut is Shift+F9:

Storage Inspector

Your cookie should show up there. Also, the Network Monitor (Ctrl+Shift+Q) allows to diagnose whether the cookie is being received and sent back (since you are setting cookies from server-side, the transport mechanism used is HTTP headers).

Network Monitor

You should also check PHP manual as often as needed. If your IDE won't generate links from code, there's a little trick you can use: append the function name to http://php.net/, e.g.: http://php.net/setcookie.

Last but not least, you have a condition to run when a given array keys does not exist, yet you use the variable anyway:

if (!isset($_COOKIE['count'])) {
      $_COOKIE['count'] = ($_COOKIE['count'] + $_SESSION['count']);  
                           ^^^^^^^^^^^^^^^^^

If PHP did not warn you, it's because your PHP set-up is configured to hide it from you. You need to fix it ASAP because coding without the aid of error messages is hard. As quick start, you can set the error_reporting and display_errors directives in your computer's system-wide php.ini file (details here). Errors thumb rule: show in development, log in production.

These tools should suffice to trouble-shoot your issue.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360