a7medo778
a7medo778

Reputation: 75

a button that can only be clicked once, EVER

as the title said i would like to create a button that says "available" and once any one clicks it it turns to "unavailable" for all future users any ideas ?

note : its for a school project, nothing fancy or even secured, just trying to prove a point

sorry for being vague for some, all i am trying to do is to create an article in my joomla website with a button that only the fastest user can access and click, thats it i am not worried with security or even cheating attempts joomla articles can have php js codes among others added to them

Upvotes: 3

Views: 1236

Answers (6)

Matthew Vines
Matthew Vines

Reputation: 27581

Given the nature of your problem, you may be better off blocking the users from the other end. Meaning, since you want a link to be visited only once, have the server only serve that page once, then deny access. The button will stay clickable for everyone, but only one user will ever actually receive the page requested, and only once.

do this the same way others have suggested hiding the button, set a value in a file, or a db, or really anywhere persistent on the server, and check that when the page loads, aborting, or redirecting to a denial page if present.

Upvotes: 0

Flukey
Flukey

Reputation: 6555

Simplest way without using a database, here's the basic logic using a simple text (or you could use YAML, ini, xml etc.) file with either 1 (clicked) or 0 (not clicked) in it:

$filePath = './clicked.txt';

if((int) file_get_contents($filePath)){
    // put code here to display the button and mark it as disabled
    echo "Can't click";
} else {
    // code to display button
    // code to process button request and marked clicked as true in the file
    file_put_contents( $filePath, '1');
    echo "changed file";
}

You'll need to do some validation checks i.e. the file is readable, you can update the file etc. but that's pretty much all you'd need to do if you don't want to use a db.

Also, be sure to put that file below the web directory so a user can't browse to it.

EDIT: But if you're using Joomla, just use the db to do it. This way is ugly.

Upvotes: 1

jwheron
jwheron

Reputation: 2572

Do you really want this button to only be clicked once, ever? If so, then you need to store its status in a database of some sort, and update the database when someone clicks it.

It would probably also be in your best interest to not disable it by JavaScript, as someone could just turn off JavaScript and circumvent your decision.

As an aside, this requirement doesn't make a lot of sense to me. This is going to be -- at best -- hard to verify. You could create a submit button that redirects to another page, but a user could then just hit his/her browser's back button and click the button all over again. There isn't much you can do to prevent that.

Since you clarified: Your best bet is still to store the "clicked" value in a database somewhere (a file will work just fine).

When you load the page, use PHP to check the file for a "clicked" value. If it's there, you can disable the button directly in the HTML.

I'd suggest adding a second level of checks in PHP (or whatever server-side language you're using). When someone clicks the button, you can just go back out to your database/file and check for the "clicked" value. If you find that it's already been clicked, you can do nothing (or show the user a message letting them know that it's already been clicked). Otherwise, you can update the database/file accordingly.

Upvotes: 0

Amadan
Amadan

Reputation: 198566

Quite complicated, really. Generate button by JavaScript to prevent people without JavaScript from clicking it uncontrolled. Keep a comet server so that when one user clicks it, the change is instantly propagated to all other connected clients, and the button is removed. Afterwards, do not authorise the generation of the button, for any new users.

Upvotes: 2

PseudoNinja
PseudoNinja

Reputation: 2856

Well you have a couple pieces going here.

First off you need a method to store the value (has this been clicked or not). My standard practice is to create a user_properties table (uid, property, value) in the DB to store the value to.

To manage it within the application I load the properties to a User (Profile) Object and store it to Session for reference. When the button is rendered you can do a quick check to see if the value exists in the Properties array of the User profile, if it does, disable; else leave it alone.

When the button is clicked the property in the User profile is stored and the updated to the database. Personally I check to see if something has changed in the session profile on load, if it has update database.

Upvotes: 0

TimWolla
TimWolla

Reputation: 32731

You could save a value into a database or create a text-file if the button was pressed. If the database-entry or the file exists no further pressing is allowed. Be sure to disable the button as well as not executing the server-side action when the button was pressed.

Upvotes: 0

Related Questions