有没有一种方法可以限制php表单的提交数量? [英] Is there a way to limit number of submissions for a php form?

查看:114
本文介绍了有没有一种方法可以限制php表单的提交数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如一张注册表只能使用x次?

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

推荐答案

PHP的快速而又肮脏的解决方案.

Quick and Dirty Solution For PHP.

每次他们访问页面时,它都会增加计数器的数量.太多次意味着页面die().您可以选择将此代码放置在仅当有人提交表单时才执行的位置.

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.

它经常重置计数.

已知的错误:清除Cookie会破坏它,关闭cookie会破坏它.

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.

这篇关于有没有一种方法可以限制php表单的提交数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆