如何使用php cookie为访问者设置单击“提交”按钮限制。 示例:如果用户从php邮件表单发送了3封邮件。发送3封邮件后,它将显示您的限制已结束。我怎么能这样做?
<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>
发送邮件后。如果用户尝试发送另一封电子邮件。它会说你已经发了邮件给我们
答案 0 :(得分:1)
这是一个工作表单的完整示例,其中IP只能发布三次。它由JSON&#34;数据库&#34;管理。和一个数组。
我希望这会有所帮助。
<强>代码强>
<?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>
问候。