开发者

php limit mailto() calls based on IP

Say I make a simple html contact form and I POST some data (email from input box) to a php script. In this php script mailto() function is used to send the user an email, using 开发者_Go百科the email addy submitted in the $POST variable.

I want my php script to check whether the person who executed the script has done it recently.

(For example, Tim uses the form, it sends him an email. 5 minutes later he accidentally submits the form again, my script stops the mailto() function from executing because tim already used the form once today(20 minutes whatever))


A solution would be to log (into a table in a MySQL or SQLite database, typically) each time a mail is sent :

  • current timestamp
  • user who sent the mail (his IP address, for instance)

Then, when your form is submitted, you'll have to check into that database's table if there is a recent row for the current user -- and if there is none, send the mail.


After submitting the message with mailto() the first time, set a cookie to expire in 20 minutes (or whatever). Check the cookie when submitting again, and if it is there and valid, prevent the send.

EDIT: See comment about user deleting their own cookies. This doesn't prevent it.

$expire_minutes = 20;

if (!isset($_COOKIE("alreadysent"))
{
  // First time:
  mailto();
  // Set the cookie
  setcookie("alreadysent", TRUE, time() + (60 * $expire_minutes)); // 20min expiry
}
else
{
  echo "sorry you already sent it.";
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜