我很難找出這個問題的好標題,所以我希望這很清楚。我目前正在使用我的某個網站上的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