Is there a way to limit number of submissions for a php form?

醉酒当歌 提交于 2020-01-17 07:46:10

问题


eg. A registration form can only be used x amount of times?


回答1:


Quick and Dirty Solution For PHP.

Everytime they visit a page, it increments the counter. Too many times means the page die()s. You can choose to put this code in a spot where it will only be executed when someone submits the form.

It resets the count every so often.

Known Bugs: Clearing Cookies breaks it, having cookies off breaks it.

<?php

session_start();

if(!isset($_SESSION['count']))
{
   $_SESSION['count'] = 1;
   $_SESSION['first'] = time();
}
else
{
   // Increase the Count
   $_SESSION['count']++;
   // Reset every so often
   if($_SESSION['first'] < (time() - 500))
   {
      $_SESSION['count'] = 1;
      $_SESSION['first'] = time();
   }
   // Die if they have viewed the page too many times
   if($_SESSION['count'] > 100)
   {
      die("You have submitted to many times");
   }
}

This is given you want a per user solution. If it is a whole site thing, just comment and I'll delete.




回答2:


Yes. Have the form processing page record to a database how many times it's been used, and have it disable the form when x uses are recorded.




回答3:


By used x amount of times I presume you actually mean submitted x amount of times. You could have a session counter that increments each time the form is submitted and does not pass validation, and prevents further submission attempts when the limit is reached.




回答4:


If you're submitting the form via ajax this is a pretty good resource.




回答5:


Unfortunately, there isn't a solid method here. The best way would be to store a cookie, but obviously they can clear cookies. You could try making another unique string/id of some sort by combining (md5) their IP address with their user-agent (browser,etc), then storing that in the database, but that will probably deny some people who shouldn't be denied.



来源:https://stackoverflow.com/questions/1281463/is-there-a-way-to-limit-number-of-submissions-for-a-php-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!