Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8357171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:23:58+00:00 2026-06-09T10:23:58+00:00

I was having a hard time figuring out a good title for this question,

  • 0

I was having a hard time figuring out a good title for this question, so I hope this is clear. I am currently using the TwitterOauth module on one of my sites to post a tweet. While this works, I need to set a limit to the amount of tweets submitted; just one each hour.

Note: I do not have the option to use a database. This is paramount for the question.

I have incorporated this as follows, in the PHP file that handles the actual posting to the Twitter API:

# 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.");
}

(initial) contents of the lastSentTweet.inc file (chmod 777):

<?php function getLastTweetTimestamp() { return 1344362207;}

The problem is that while this works; it allows for accidental double submits; if multiple users (and the site this script runs on is currently extremely busy) trigger this script, it happens that 2 submits (or more, though this has not occurred yet) to Twitter slip through, instead of just the 1. My first thought is the (although minute) delay in opening, writing and closing the file, but I could be wrong.

Does anyone have an idea what allows for the accidental double submits (and how to fix this)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T10:23:59+00:00Added an answer on June 9, 2026 at 10:23 am

    You’re getting race conditions. You will need to implement locking on your file while you’re making changes, but you need to enclose both the read (the include statement) and the update inside the lock; what is critical is to ensure nobody else (e.g. another HTTP request) is using the file, while you read its current value and then update it with the new timestamp.

    This would be fairly ineffective. You have other options which might be available in your PHP installation, here are some:

    1. You can use a database even if you don’t have a database server: SQLite
    2. You can store your timestamp in APC and use apc_cas() to detect if your last stored timestamp is still current when you update it.

    Update

    Your locking workflow needs to be something like this:

    1. Acquire the lock on your stored timestamp. If you’re working with files, you need to have the file open for reading and writing, and have called flock() on it. flock() will hang if another process has the file locked, and will return only after it has acquired the lock, at which point other processes attempting to lock the file will hang.
    2. Read the stored timestamp from the already locked file.
    3. Check if the required time has passed since the stored timestamp.
    • Only if it has passed, send the tweet and save the current timestamp to the file; otherwise you don’t touch the stored timestamp.
    1. Release the lock (just closing the file is enough).

    This would ensure that no other process would update the timestamp after you have read and tested it but before you have stored the new timestamp.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having a hard time figuring out good reasons for the dependency property.
Having a hard time figuring out why this alert code is being called once
Having a hard time figuring out the best way to do this... I have
I'm having some hard time figuring out what's wrong in this simple code: This
I'm having a hard time figuring out why this is not working. I have
I'm having a hard time figuring out how best to do this. I have
Having a hard time figuring out the best way to go about this. What
Im having a really hard time figuring out how to specify a good search
I'm having a hard time figuring out how to do this if statement. I
I'm having a hard time figuring out how to translate this simple SQL statement

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.