I Need Random Link Script

Started on the old forum in June 2009 · view the original in the archive · old links redirect here

2 replies · 645 views · started 17 years ago · 1 min read

3 participants
wapkerala Junior · 13 posts #1

hello guys can any one give me a Random Link Scripts

blackhowk Senior · 411 posts #3

This function returns a random file from a given folder. It also allows extension filtering.

function RandomFile($folder='', $extensions='.*'){ 
  
    // fix path: 
    $folder = trim($folder); 
    $folder = ($folder == '') ? './' : $folder; 
  
    // check folder: 
    if (!is_dir($folder)){ die('invalid folder given!'); } 
  
    // create files array 
    $files = array(); 
  
    // open directory 
    if ($dir = @opendir($folder)){ 
  
        // go trough all files: 
        while($file = readdir($dir)){ 
  
            if (!preg_match('/^.+$/', $file) and  
                preg_match('/.('.$extensions.')$/', $file)){ 
  
                // feed the array: 
                $files[] = $file;                 
            }             
        }         
        // close directory 
        closedir($dir);     
    } 
    else { 
        die('Could not open the folder "'.$folder.'"'); 
    } 
  
    if (count($files) == 0){ 
        die('No files where found :-('); 
    } 
  
    // seed random function: 
    mt_srand((double)microtime()*1000000); 
  
    // get an random index: 
    $rand = mt_rand(0, count($files)-1); 
  
    // check again: 
    if (!isset($files[$rand])){ 
        die('Array index was not found!'); 
    } 
  
    // return the random file: 
    return $folder . $files[$rand]; 
  
}

Get a random row from a table. Useful for just about anything where you need a random entry. Put in anything you want, but just leave "ORDER BY RAND()" and "LIMIT n" where they are. (LIMIT can be set to any number you want though.)

SELECT FROM sometable WHERE 1 ORDER BY RAND() LIMIT 1; *

This will show a random link when someone comes to your page.

//set the urls $urls = array("http://google.com" ,"http://hotmail.com" ,"http://hawkee.com" );

//set the text links $text = array("Google" ,"Hotmail" ,"Hawkee"); srand(time());

//set the number in (rand()%3); for however many links there are $random = (rand()%3); echo ("<a href = "$urls[$random]">$text[$random]</a>");

Or see attach:

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close