Store Click on cookie

1

I already searched the site here and did not find anything that could really help me. I think some of you can help me.

This is the following, I created a link that gets the 'x' post ID via GET and with each click on that link an UPGRADE is performed in the DB where it takes the previous number of clicks and counts +1, but if I clicking countless times on that link will count countless clicks.

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

The code that I use to upgrade my 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';
           }
       }
   }   
?> 
    
asked by anonymous 29.05.2014 / 19:44

2 answers

2

Create another table to store clicks, and place the USER ID, a TIMESTAMP for the time it was clicked, and the ID of that 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 date and time functions .

EDIT : You have edited by mentioning that you needed to do this using cookies. It's not possible. You can not 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 vachar where you can save the user's IP. p>     

29.05.2014 / 19:56
0

I liked Guilherme's answer. I will propose an alternative:

  • Make a user session run for 24 hours, with no renewal per activity;
  • When the user makes the request that can be counted, check if there is a variable in the session indicating that this request has already been made. If the request has already 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 is that no changes to the database are required.

    
29.05.2014 / 20:17