Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You're getting race conditions. You will need to implement <a href="http://php.net/manual/en/function.flock.php" rel="nofollow">locking</a> on your file while you're making changes, but you need to enclose <em>both</em> the read (the <code>include</code> 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.</p> <p>This would be fairly ineffective. You have other options which might be available in your PHP installation, here are some:</p> <ol> <li>You <em>can</em> use a database even if you don't have a database server: <a href="http://bg2.php.net/manual/en/book.sqlite.php" rel="nofollow">SQLite</a></li> <li>You can store your timestamp in APC and use <a href="http://bg2.php.net/manual/en/function.apc-cas.php" rel="nofollow"><code>apc_cas()</code></a> to detect if your last stored timestamp is still current when you update it.</li> </ol> <p><strong>Update</strong></p> <p>Your locking workflow needs to be something like this:</p> <ol> <li>Acquire the lock on your stored timestamp. If you're working with files, you need to have the file open for reading <em>and</em> writing, and have called <code>flock()</code> on it. <code>flock()</code> 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.</li> <li>Read the stored timestamp from the already locked file.</li> <li>Check if the required time has passed since the stored timestamp. <ul> <li><em>Only</em> if it has passed, send the tweet and save the current timestamp to the file; otherwise you don't touch the stored timestamp.</li> </ul></li> <li>Release the lock (just closing the file is enough).</li> </ol> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload