Jump to content

Disable multiple clicking on a link


proxximo

Recommended Posts

Hey Guys,

 

I am creating a php text based game and during testing I have found a HUGE exploitable problem. During purchasing or selling items by clicking on a link (which then uses a MySQL query to updated the database), if you click the link multiple times before the next page loads you can sell one item multiple time, thus making quite a bit of in game currency really quickly.

 

I have found a quick patch using javascript but it is not a solution I really want to stick with seeing as it uses an alert box...

var clicked = false;
function processClick() {
   if (clicked) return;
   clicked = true;
   alert("Your item has been sold");
}

Basically I call the above function which stops them from clicking multiple times by popping an alert box.

 

My question is... is there a better way to disable multiple clicks in this way, as I generally only like to use alert boxes for debugging and really slows down he pace of the game. Not to mention it looks terrible.

 

Any help would be great. All of my google searches have left me with no clear alternative.

 

Thanks!

Proxximo

Link to comment
Share on other sites

This solution still allows the issue to persist. I tried in this form as well as creating a function and calling it onClick. Its driving me absolutely nuts! But thank you very much for a quick response _EmilsM!

Link to comment
Share on other sites

If you're doing a game, you may find other uses for $.data() as well.

jsFiddle: http://jsfiddle.net/Re7jQ/

 

Javascript / jQuery

$(document).ready(function()
{
    $("selectLink").click(function(e)
    {
        e.preventDefault();
        
        if ($(this).data('disabled'))
            return false;
        
        $(this).data('disabled', true);
        window.open($(this).attr("href"), "_blank");
    });
});

Edit: I didn't realize this is your safety to selling items.  While it will stop the average user, Javascript is not going to prevent people from exploiting it.  You need to check server-side to assure they are not selling an item they don't have.

Edited by Xaotique
Link to comment
Share on other sites

A somewhat more generic solution to disabling links after a single click would be:

http://jsfiddle.net/Xeghq/

$(document).on('click', '.one-click', function(e){
    if ($(this).data('clicked')){
        e.preventDefault();
    }
    else {
        $(this).addClass('clicked').data('clicked', true);
    }
});
That would cause any element with class="one-click" to only allow a single click. Once clicked all further clicks would be disabled, and the element would get class="clicked" added to it which you could use to change the visual style if desired. Note that this would work for things such as form submits also.

 

As has been mentioned however, a JS solution like this will only stop someone who is lazy/unskilled. Server-side validations are required to fully prevent such a thing.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.