-1

I have a button where logged in users can click and download some material. I want this button to be clicked only once in a lifetime and then either disappear or be disabled. So once the user clicks the button they download their stuff and then the button is no longer active. What is the best way to do this?

<button type="submit" class="button">Get Certificate</button>
Spy
  • 149
  • 10
  • 2
    Quite a few ways to do this. Have you searched for this on the WWW? – Funk Forty Niner Oct 05 '19 at 13:58
  • If you want this for every user just once.Then you need to save the state in the db. I don't understand why you need this – saif iqbal Oct 05 '19 at 13:59
  • @FunkFortyNiner I have indeed searched the World Wide Web for this. All I got was "how to prevent 2 submissions on forms" – Spy Oct 05 '19 at 13:59
  • There is something called a "nonce". You could also do as @saifiqbal mentioned, using a database which is something I also had in mind. – Funk Forty Niner Oct 05 '19 at 14:01
  • Should help u [https://stackoverflow.com/questions/2323948/disabling-the-button-after-once-click](https://stackoverflow.com/questions/2323948/disabling-the-button-after-once-click) – Interface Oct 05 '19 at 14:02
  • Here's a Q&A on "nonces" here on Stack: https://stackoverflow.com/questions/4145531/how-to-create-and-use-nonces - which is server side and not client-side. Don't use client-side methods. – Funk Forty Niner Oct 05 '19 at 14:03
  • All good advice. Thanks – Spy Oct 05 '19 at 14:04
  • Also think about how to handle someone that clicked the download but did not successfully download the file. – Danny Oct 05 '19 at 15:00

1 Answers1

-1

Well am assuming what you meant by once in a lifetime is, even after the user logout and login again, they still won't be able to re-download the file. In that case as mentioned by @saifiqbal, you have to save the state on a database. Create a table named user_certificate for example, add a user_id reference column and a Boolean type column named downloaded. You can then populate this table when user successfully download their certificate by setting downloaded to true for the user. Then on your download page check if downloaded=true to disable the button.

<button type="submit" class="button" <?php if ($downloaded==true) echo "disabled; ?> >Get Certificate</button>

The variable $downloaded is fetched from the database. You can choose to name the database table, columns and the variable anything you want, just giving you an idea. Pardon me for any typo, I typed this from my cellphone.

Oludotun
  • 107
  • 1
  • 3