Stamina regeneration without scheduled updating

This example shows how you can throttle user actions (e.g. login attempts, posting in a forum, viewing a CPU-intensive page).

Click the button below. Click it fast.

The button

connect('localhost', 11211) or die ("Could not connect"); $results = $memcache->get($identifier); // If no results were found, make a fresh entry if (empty($results)) { $results = array( 'stamina' => MAXIMUM, 'timestamp' => time() ); } // If results were found, update them else { $results['stamina'] += (time() - $results['timestamp']) * (REGENERATE / 3600); $results['stamina'] = min(MAXIMUM, $results['stamina']); $results['timestamp'] = time(); } // Decrement the stamina and confirm $results['stamina'] = $results['stamina'] - 1; if ($results['stamina'] >= 0) { $memcache->set($identifier, $results, 3600*MAXIMUM/REGENERATE); echo "

Successful.

"; echo "

".max(0, floor($results['stamina']))." points remaining.

"; } else { echo "

What's the rush?

"; echo "

Your points regenerate at ".(REGENERATE/60)." per minute up to a maximum of ".MAXIMUM." points.

"; } } ?>