Quantcast
Channel: Active questions tagged html - Stack Overflow
Viewing all articles
Browse latest Browse all 72388

How to prevent anchor tag spam?

$
0
0

I have been wracking my head around this problem for a while now and simply can't figure out how to prevent it. I have changed the code to help you understand my problem.

I have an anchor tag that when pressed takes the user to a script that inserts a row into the database. However, when this button is spammed it inserts the same number multiple times into the database.

Inside script.php

<?php session_start(); require_once 'database.class.php'; 
$current_number = $database->fetchNum(); //THIS IS A FUNCTION TO FETCH THE CURRENT NUMBER IN THE DATABASE
if(isset($_SESSION['scriptspam'])) {

     if($_SESSION['scriptspam'] <= time() - 2) {

         $_SESSION['scriptspam'] = time();
         $carry_on = 1;

     } else {

         $carry_on = 0;
         header("Location: ../gs2.php?e=999");
         exit();

     } 
} else {

     $_SESSION['scriptspam'] = time();
     $carry_on = 1;
}
if($carry_on) {
     $database->insertNum($current_number + 1);
     header("Location: ../gs2.php?e=999");
}
?>

Theoretically everytime the user presses this link a new row should be included into the database for example:

#######################
ID               NUMBER
1                100
2                101
3                102

However, what I find is when someone spams the link the same number is included into the database like this:

#######################
ID               NUMBER
1                100
2                100
3                100
4                100
5                100

This carrys on depending how much the link is spammed. I added the "scriptspam" $_SESSION variable to try and prevent it from inserting into the database if 1.5 seconds hasn't passed to prevent spamming but this doesn't seem to work.

Does anyone know a better solution or should my code work but doesnt?


Viewing all articles
Browse latest Browse all 72388

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>