Limit Form Use with Cookies
There may be occasions you want to limit to one the number of times per day (or week or month) a visitor can use a form. You may be taking a poll or restricting posts to a forum or blog or other media. This little cookie code is just the tool you need.
You can limit how many times a site visitor can use a form with the One-Event Cookie JavaScript code.
Download the complete code below the example.
One-Use Form
Put the One-Event Cookie JavaScript either in the HEAD area of the web page or immediately above the form that will make use of it.
In the One-Event Cookie JavaScript, specify the number of days users must wait before they may use the form again. Put the number of days between the apostrophes near the end of the
OneEventLifetime = '1';
line. In the example form, the cookie lasts for 1 day, a 24-hour period.
No other changes need be made to the One-Event Cookie JavaScript.
In the example form, JavaScript is used to determine whether or not a cookie already exists by calling the One-Event Cookie JavaScript. Then:
- If a cookie already exists, a disabled "You have used this form once today" button is printed.
- If no cookie exists, a "Submit Today's Entry" button is printed.
If the user's browser is not JavaScript-enabled, no button is printed.
Example:
Will Bontrager
©2005 Bontrager Connection, LLC
Please note:
Articles on this website are presented "as is". However -
If you have a question about a CGI script, HTML, CSS, PHP, or JavaScript
Ask one of our Experts and you'll have your answer!
Click here for details.