Do an action 1 out of every N page requests


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. function probabilityMatch($inNumberOfEvents) {
  2. $max = min($inNumberOfEvents, mt_getrandmax());
  3. if ($max > 0) {
  4. return (mt_rand(0, $max) == 1);
  5. }
  6. }
  7.  
  8. // Example usage
  9. // if (probabilityMatch(10)) {
  10. // clean the cache 1 out of every 10 page requests
  11. // cleanCache();
  12. // }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.