How do I update a column once per page viewed?

0

I'm trying to create a hit counter, this code updates my column once per session, but what I want is to update my column whenever the user views a new page.

if(!isset($_SESSION))session_start();

if(empty($_SESSION['counter'])){    
$_SESSION['counter'] = 1;
$mread = $conn->prepare("UPDATE table SET counter = counter + 1 WHERE id = :id");
$mread->bindParam(':id', $id, PDO::PARAM_INT);
$mread->execute();
};

If I only use the query UPDATE it will refresh my column whenever I reload the page, and that's not what I want. I want to update my column whenever the user views a new page.

How do I do this?

    
asked by anonymous 05.10.2018 / 03:07

1 answer

1

Put in a variable the name of each page, eg:

page1.php $nomePagina="pagina1";

page2.php $nomePagina="pagina2";

code of page1.php

if(!isset($_SESSION))session_start();

    $nomePagina="pagina1";

    if(empty($_SESSION[$nomePagina])){

      ..........
      ..........


      $_SESSION[$nomePagina] = 1;

      //UPDATE

   };
    
05.10.2018 / 04:20