【问题标题】:Submit Button Limit For a User为用户提交按钮限制
【发布时间】:2018-02-07 05:50:06
【问题描述】:

如何使用 php cookie 为访客设置点击提交按钮限制。 例如:如果用户从 php 邮件表单中发送了 3 封邮件。发送 3 封邮件后,会显示 Your limit is over。我该怎么做?

<form action="post.php" mathord="post"><input type="email" placeholder="Your Email" name"email"><input type="text" placeholder="Your Text"  name="text"><input type="submit" value="sent" name="sub"></form>

发送邮件后。如果用户尝试发送另一封电子邮件。它会说你已经给我们发了邮件

【问题讨论】:

  • 到目前为止你有什么尝试?发布您的代码。
  • 请在您提出问题时尝试添加示例代码,否则除非问题不需要代码,否则没有人会完全理解您的要求
  • 我添加一些代码先生
  • 防止这种情况被规避的唯一方法是强制用户登录,然后在与该用户相关的数据库或文件中增加一个计数器。

标签: php cookies submit


【解决方案1】:

这是一个完整的工作表单示例,其中一个 IP 只能发布 3 次。它由一个 JSON“数据库”和一个数组管理。

我希望这会有所帮助。

代码

<?php

if (isset($_POST['sub'])) { // Checking if there's a submit

    $dir = __DIR__ . "/submitted.json"; // File directory

    if (!file_exists($dir)) { // If the file doesn't exist
        file_put_contents($dir, "[]"); // Creates a file
        $jsonDatabase = array();
    } else
        $jsonDatabase = json_decode(file_get_contents($dir), true); // If it exists, it gets the content

    $userIP = $_SERVER['REMOTE_ADDR']; // Save the user IP in a variable

    if (array_key_exists($userIP, $jsonDatabase)) { // If the user IP exists in the array

        if ($jsonDatabase[$userIP] <= 3) { // If the IP has less than three submits
            $jsonDatabase[$userIP] += 1; // Adds a submit to the IP
            $canSubmit = true;
        }


    } else { // If the IP doesn't exist in the array
        $jsonDatabase[$userIP] = 1; // Save the IP in the array with the value one
        $canSubmit = true;
    }

    file_put_contents($dir, json_encode($jsonDatabase)); // Save the array back to the "database"

    if ($canSubmit) { // If the IP can submit
        // Do your stuff with the data you've got.
    } else { // If not
        echo "You've reached the limit.";
    }
}

?>

<html>
<head>
    <title>StackOverflow example</title>
</head>
<body>
<form action="index.php" method="post">
    <input type="email" placeholder="Your Email" name="email"/><br>
    <input type="text" placeholder="Your Text" name="text"/><br/>
    <input type="submit" value="Send" name="sub"/>
</form>
</body>
</html>

问候。

【讨论】:

  • @AbraCadaver 哦,我明白了.. 我现在要制作另一个系统。抱歉,来晚了..
  • @AbraCadaver 你喜欢这个版本吗?你这个有用吗?问候。
  • @RussellRex 它是在此 -> file_put_contents($dir, json_encode($jsonDatabase)); 成功完成时创建的。问候
  • 我听不懂,先生。你能给我完整的清晰代码吗?试了两天还是解决不了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-07-05
  • 2015-08-19
  • 1970-01-01
  • 2011-02-17
  • 2011-05-06
  • 1970-01-01
  • 2015-02-14
相关资源
最近更新 更多