Click counter

hi this is a click counter work well but i need it to only click once per session user or ip or something
cause anytime i click d button the counter increases i dont like it like that

 //update button click

if (isset($_POST['action']) && $_POST['action'] == 'update')
        {
     
        $query = "UPDATE " . $DBPrefix . "auccounter set download_counter = download_counter + 1 WHERE auction_id = :auction_id";
        $params = array();
        $params[] = array(':auction_id', $id, 'int');
        $params[] = array(':download_counter', $id, 'int');

        $db->query($query, $params);    
                    
        header('location: item.php?id=' . $id);
        exit(); 

    }

thanks

Can you explain?

means if there anyway round for the counter to not keep increasing wen a user keep clicking the button

session
userip

just given example

Session is probably more realistic, as many people can share an IP and you probably want all those clicks counted.

Assuming you already have a session in place it would go something like this

if (isset($_POST['action']) && $_POST['action'] == 'update')
{
    if (!isset($_SESSION['downloaded_item_'.$id])) {
        $query = "UPDATE " . $DBPrefix . "auccounter set download_counter = download_counter + 1 WHERE auction_id = :auction_id";
        $params = array();
        $params[] = array(':auction_id', $id, 'int');
        $params[] = array(':download_counter', $id, 'int');

        $db->query($query, $params);
    }

    $_SESSION['downloaded_item_'.$id] = true;
                    
    header('location: item.php?id=' . $id);
    exit();
}

The first time they download it $_SESSION['downloaded_item_'.$id] won’t be set, so the query will run and then $_SESSION['downloaded_item_'.$id] will be set so the query won’t run on any subsequent calls limiting the counter to once per session (and per id).

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.