Advertisement
aaaaaa123456789

Random link hopper

Jun 13th, 2012
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.37 KB | None | 0 0
  1. <?php
  2.  
  3. function add_element($list, $frequency, $URL) {
  4.   $freq = (int) $frequency;
  5.   if ($freq <= 0) return $list;
  6.   $list[($list['count'] ++)] = array(
  7.     'freq' => $freq,
  8.     'URL' => $URL
  9.   );
  10.   $list['sum'] += $freq;
  11.   return $list;
  12. }
  13.  
  14. function draw_random($list) {
  15.   $randomnumber = mt_rand(0, $list['sum'] - 1);
  16.   for ($pos = 0; ; $pos ++)
  17.     if ($randomnumber < $list[$pos]['freq'])
  18.       return $list[$pos]['URL'];
  19.     else
  20.       $randomnumber -= $list[$pos]['freq'];
  21. }
  22.  
  23. $elements = array('count' => 0, 'sum' => 0);
  24.  
  25. /*
  26. ADD ELEMENTS HERE
  27. Just copy the line and modify the last two values.
  28. The number is the frequency, the third thing is the site.
  29. So, these lines
  30. $elements = add_element($elements, 1, 'http://google.com');
  31. $elements = add_element($elements, 4, 'http://jiggmin.com');
  32.  
  33. would add google.com with a frequency of 1 (out of the total, which is 1 + 4 = 5)
  34. and jiggmin.com with a frequency of 4.
  35.  
  36. If you're unsure about the frequency, just put 1 for every site.
  37. */
  38.  
  39. $elements = add_element($elements, 1, 'http://google.com');
  40. $elements = add_element($elements, 4, 'http://jiggmin.com');
  41.  
  42.  
  43.  
  44. if ($elements['sum'] == 0) die ('No links');
  45. $finallink = draw_random($elements);
  46.  
  47. $result = <<<HTMLcode_end
  48. <html>
  49.   <head>
  50.     <meta http-equiv="Refresh" content="0; URL=$finallink">
  51.   </head>
  52. </html>
  53. HTMLcode_end;
  54.  
  55. echo $result;
  56.  
  57. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement