All pastes #912833 Raw Edit

Anonymous

public php v1 · immutable
·published 2008-02-21 17:46 UTC
rendered paste body
$list_epoch = 1;$list = array(  'blah',  'yadda',  'etc');if(($_SERVER['HTTP_REFERER'] == 'http://georgebushisyournewbicycle.com/') && !($_COOKIE['annoy_spacehobo'])) {  # SpaceHobo stares at you disapprovingly.  # Do not offer him cookies again, and instead serve a random entry.  $phrasen = array_rand($list);  $phrase = $list[$phrasen];} else {  # PHP session cookie junk  session_name('annoy_spacehobo');  session_start();  # Just in case you need to invalidate everyone's list cache.  # This also serves as array initialization for new visitors.  if($_SESSION['list_epoch'] < $list_epoch) {    $_SESSION['list_epoch'] = $list_epoch;    $_SESSION['list_used'] = array();  }  # If the user has gone through all phrases, reset.  if(count($_SESSION['list_used']) >= count($list)) {    $_SESSION['list_used'] = array();  }  # Weed out entries the user has already seen.  # NOTE: Figure out something more efficient if list ever gets  # over a couple thousand.  $list_candidates = array();  foreach($list as $a) {    if(!in_array($a, $_SESSION['list_used'])) {      $list_candidates[] = $a;    }  }  if(count($list_candidates) > 0) {    $phrasen = array_rand($list_candidates);    $phrase = $list_candidates[$phrasen];    $_SESSION['list_used'][] = $phrase;  } else {    # The only reason $list_candidates should have zero entries is if    # $list had duplicate entries.  Just in case, reset and serve up    # something random.    $_SESSION['list_used'] = array();    $phrasen = array_rand($list);    $phrase = $list[$phrasen];  }}