我很难找出这个问题的好标题,所以我希望这很清楚。我目前正在使用我的某个网站上的TwitterOauth模块发布推文。虽然这有效,但我需要对提交的推文数量设置限制; 只需一小时。防止网站双重提交
注意:我没有选择使用数据库。这对于这个问题至关重要。
我已经并入这个如下,在处理实际张贴到Twitter API PHP文件:
# Save the timestamp, make sure lastSentTweet exists and is writeable
function saveTimestamp(){
$myFile = "./lastSentTweet.inc";
$fh = fopen($myFile, 'w');
$stringData = '<?php function getLastTweetTimestamp() { return '.time().';}';
fwrite($fh, $stringData);
fclose($fh);
}
# Include the lastSentTweet time
include('./lastSentTweet.inc');
# Define the delay
define('TWEET_DELAY', 3600);
# Check for the last tweet
if (time() > getLastTweetTimestamp() + TWEET_DELAY) {
// Posting to Twitter API here
} else {
die("No.");
}
(初始)的lastSentTweet.inc
文件的内容(CHMOD 777):
<?php function getLastTweetTimestamp() { return 1344362207;}
问题是,虽然这工作;它允许意外双重提交;如果多个用户(以及此脚本运行的站点当前非常繁忙)触发此脚本,则会发生2次提交(或更多,但尚未发生),以便Twitter通过,而不仅仅是1.我的第一个想法是打开,写入和关闭文件(尽管很小)的延迟,但我可能是错的。
有没有人有一个想法是什么允许意外双重提交(以及如何解决这个问题)?
使用db而不是文件。 – 2012-08-07 20:34:48
对不起,忘了提及:我不能(加上那个问题)。否则我当然会;) – 2012-08-07 20:43:13
不能想象为什么你不能使用数据库。 PHP有一个内置的 – 2012-08-07 20:46:38