Store Click on cookie

Asked

Viewed 312 times

1

I have searched here for the site and found nothing that could really help me. I believe some of you can help me.

The following is, I created a link that takes the ID of 'x' post through GET and every click on that link is performed an UPGRADE in the BD where it takes the previous number of clicks and counts +1, but if I click numerous times on that link will count numerous clicks.

I would like to know how I do to store the click of that particular user for 24 hours?

the code I use to upgrade clicks is below

<?php 
 $ID = filter_input(INPUT_GET, 'ID');
       if(empty($ID) || !is_numeric($ID)){

 echo '<script type="text/javascript">location="../";</script>';   
}else{     
       $seleciona = $pdo->prepare("SELECT * FROM postagens WHERE ID = ? LIMIT 1");
       $seleciona->bindValue(1, $ID, PDO::PARAM_INT);
   $seleciona->execute();

       $dados = $seleciona->fetchObject();     
       $n_status = $dados->CLIQUES + 1;

       if($dados){         
           $stmte = $pdo->prepare("UPDATE postagens SET CLIQUES = :1 WHERE ID = :2");
           $stmte->bindParam(":1", $n_status , PDO::PARAM_INT);
           $stmte->bindParam(":2", $ID , PDO::PARAM_STR);
           $executa = $stmte->execute();

           if($executa){
               echo '<script type="text/javascript">location="http://'.$dados->LINK.'";</script>';
           }else{
               echo 'Erro ao inserir os dados';
           }
       }
   }   
?> 
  • 1

    You could add an extra column to the table that stores the click count. This column stores the last hour when there was a request GET. Then at the time of UPDATE you put an extra condition on your WHERE. Only increment the counter if the current time is longer than the last click time for at least 24 hours.

  • That would help in part. But if I do this and determine for example, 1hr without change it would hurt me since in 1hr if other users click on the link there will be no change in the counter. That’s why I want to create the cache. It would be +/- that same line of reasoning you but I want that whenever a user click on that specific link create a cache on that person’s pc, then I make a condition where if the cache exists will not count the click, so if a thousand people click there will be a thousand clicks. but if a thousand people click 2 times there will still be a thousand clicks

  • I thought there was an accountant per user. Guilherme’s response is good.

2 answers

2


Create another table to store clicks, and put the USER ID, a TIMESTAMP for the time it was clicked, and the ID of such post.

    CREATE TABLE Cliques (userid int, data timestamp, postid int);

To get the number of clicks, use:

    SELECT COUNT(*) FROM Cliques WHERE postid = 0 AND DATE_ADD(data, INTERVAL 24 HOUR) > NOW() 

To remove the old clicks, just run this query:

    DELETE FROM Cliques WHERE DATE_ADD(data, INTERVAL 24 HOUR) < NOW()

I recommend you read about some Mysql functions about date and time.

EDIT: You edited mentioning that you needed to do this using cookies. It’s not possible. You can’t count how many people have that cookie in your browser.

EDIT 2: I would really use the database for this, if there is no user system, you can also change the userid to a vacuum where you can save the user IP.

  • GOOD... Take the IP is a good alternative... VALEW

0

I liked Guilherme’s reply. I will propose an alternative:

  • Make a user session last 24 hours, without renewal by activity;
  • When the user makes the request that can be accounted for, check if there is a variable in the session indicating that this request has already been made. If the request has been made before, do not count the click;
  • If the variable mentioned above is not found, count the click, and then create this variable.

The good thing about that is that no changes to the database are necessary.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.