Limiting Entries Per User

Got something cool to share with AppGini users? Feel free to post it here!
Post Reply
rpierce
Veteran Member
Posts: 97
Joined: 2018-11-26 13:55
Location: Washington State

Limiting Entries Per User

Post by rpierce » 2021-07-16 23:13

I have created a survey. Is there a way I could prevent a person from taking the survey multiple times? It is set up for anonymous persons to take the survey, so I'm thinking I need a way to limit the IP address to only one entry. Thank you as always....

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1126
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: Limiting Entries Per User

Post by jsetzer » 2021-07-17 06:05

The IP address will likely change every day, unless you can control the network and give fixed, static IP addresses to all possible participants' devices. This means users can take the survey again next day.

Additionally, IP addresses just identify a device (for a certain time) and not the user.

Also, IP addresses can be faked.

So, IP addresses cannot be used as bulletproof unique identifiers for identifying the user in most of the cases.
Kind regards,
<js />

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1126
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: Limiting Entries Per User

Post by jsetzer » 2021-07-17 06:15

Many applications use javascript to save a generated unique identifier as cookie or in localStorage. This id can be used to identify the browser for that user profile.

When saving a survey, store unique identifier as cookie or in localStorage.

When entering the site, read and check if that identifier has already been stored as cookie or in localStorage. If not, this may be a new user. If yes, that user will have been here before.

Caution: this is not bulletproof, too:

- users can delete the cookie or localStorage item
- users can use incognito browser
- users can use a different browser
- users can deny saving cookies
- this will just identify the user profile/browser combination
Kind regards,
<js />

rpierce
Veteran Member
Posts: 97
Joined: 2018-11-26 13:55
Location: Washington State

Re: Limiting Entries Per User

Post by rpierce » 2021-07-17 16:01

Ok, thank you for the guidance. This survey is limited to people in my company so I don't think that too much cheating will occur, but there are always the childish who will try to skew results.

Do you have any pointers on where I would look to find out how to design a cookie type of remedy?

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1126
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: Limiting Entries Per User

Post by jsetzer » 2021-07-18 13:49

LocalStorage:

https://www.w3schools.com/jsref/prop_wi ... torage.asp

You may also google for javascript fingerprint libraries which try to collect different browser and environment settings t for building a likely unique identification string ("fingerprint"). But I have never used such in real life projects.
Kind regards,
<js />

rpierce
Veteran Member
Posts: 97
Joined: 2018-11-26 13:55
Location: Washington State

Re: Limiting Entries Per User

Post by rpierce » 2021-07-18 22:21

Once I figure out the code, which Hooks file would I include it in? My assumption is hooks/tablename - function tablename_header - case 'detailview'.

Am I even close??

rpierce
Veteran Member
Posts: 97
Joined: 2018-11-26 13:55
Location: Washington State

Re: Limiting Entries Per User

Post by rpierce » 2021-07-18 22:32

I'm referring to the localstorage method.

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1126
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: Limiting Entries Per User

Post by jsetzer » 2021-07-19 06:22

rpierce wrote:
2021-07-18 22:21
Once I figure out the code, which Hooks file would I include it in? My assumption is hooks/tablename - function tablename_header - case 'detailview'.
* Please note: for this article I did not create a "surveys" table but used an existing "employees" table. So, you will see "employees" here and there. Replace it by your specific table name.

Well, I guess you only want to deny insert if that user already has inserted once and not if she/he has entered the "add new" page without saving. So, we should not change detail view in any case but only after insert.

If it was my project, I'd start like this*:

Step 1: Create a "thank you page"

(1.1) You may create a custom page, for example survey_inserted.php:

file: survey_inserted.php

Code: Select all

<?php

include("language.php");
include("lib.php");
include("header.php");

?>
<div class="alert alert-success">Thank you for taking the survey!</div>
<script>
    window.localStorage.setItem(' ', ' ');
    setTimeout(function() {
        window.location.href = "index.php";
    }, 5000);
</script>
<?
include("footer.php");
This code shows a success message, and saves a flag in localStorage. After 5 seconds user will be redirected to homepage (index.php).

Step 2: Change redirect after insert

Find the "Redirect after insert" value of your "surveys" table*:

AppGini_0nmBBYs2nh.png
AppGini_0nmBBYs2nh.png (8.94 KiB) Viewed 123 times

(2.1) Change that value like this:

Code: Select all

survey_inserted.php?SelectedID=#ID#
(2.2) regenerate (+upload) the generated files.

From now on, users will see the new custom "thank you" page after insert and will be redirected to homepage after 5 seconds.


Step 3: Deny filling another survey

Now we have a flag in localstorage which indicates that we have inserted a survey before. If user tries to enter surveys in "add new" mode, we can read the flag and deny editing.

(3.1) create hooks/surveys-dv.js (or edit, if already exists):

file: hooks/surveys-dv.js

Code: Select all

redirectReturningUserTo("employees_view.php");

function redirectReturningUserTo(url) {
    if (!jQuery("input[name='SelectedID']").val().length && window.localStorage.getItem(" ") != undefined) {
        jQuery(".container > *").css("filter", "blur(5px) grayscale(1)");
        setTimeout(function () { window.location.href = url; }, 100);
    }
}
---

Test

(1) Insert a new survey (or enter the custom page manually):

chrome_GPtB48Z7l0.png
chrome_GPtB48Z7l0.png (3.75 KiB) Viewed 123 times

You will see the "thank you message".

RGYgYdbAJr.png
RGYgYdbAJr.png (9.92 KiB) Viewed 123 times

You will not see the localStorage setting unless you check application tab in developer tools of your browser.

(2) Try to create a new survey*:

W1k4gDOYYb.gif
W1k4gDOYYb.gif (250.18 KiB) Viewed 123 times

Detail view in "Add new" mode will be blurred immediately and user will be redirected to Table View.

--

Additional notes and limitations
  • The code shown here is a starting point for your own implementation
  • Extension: You may also hide/remove the "add new" button in table view: create (or edit) hooks/surveys-tv.js, check existence of flag using .getItem function, then hide or remove element
    Something like this (not tested) inside document.ready function of table-view-hook:

    Code: Select all

    if (window.localStorage.getItem(" ") != undefined) jQuery("#addNew").hide();
  • According to your specs, this solution will work for one (single) survey-table. Just in case you will have more than one survey in the future (a table per survey), you will have to change the key when using localStorage.getItem and localStorage.setItem functions according to the specific table name.
  • As mentioned in my previous post(s), this is not bulletproof and can be hacked easily if a user has little knowledge about javascript and browser settings. But according to your specification this should be fine with your secure intranet environment.
Kind regards,
<js />

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1126
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: Limiting Entries Per User

Post by jsetzer » 2021-07-19 06:37

Sorry, I cannot edit my previous post any longer, but I'd like to add another idea:
  • Extension (2):
    If another insert is denied, instead of redirecting to table view, you can also...
    (1) Create a new custom page telling the user that she/he cannot fill-in another survey
    (2) Change the redirection (first line of step 3.1) to that new custom page
Kind regards,
<js />

rpierce
Veteran Member
Posts: 97
Joined: 2018-11-26 13:55
Location: Washington State

Re: Limiting Entries Per User

Post by rpierce » 2021-07-19 16:56

Thank you for all your hard work....I'll let you know how it works for me.

rpierce
Veteran Member
Posts: 97
Joined: 2018-11-26 13:55
Location: Washington State

Re: Limiting Entries Per User

Post by rpierce » 2021-07-19 18:36

Holy Cow...that all worked great right out of the box. Thank you for such a clear and detailed resolution. I'm sure that others will be able to use this in their development.

You must have a giant brain!! :lol:

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1126
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: Limiting Entries Per User

Post by jsetzer » 2021-07-19 20:01

Thank you for your feedback! :D

Great, I'm happy it works for you out of the box!
Kind regards,
<js />

Post Reply