Results 1 to 4 of 4
  1. #1

    random page script - php

    hey guys

    what i'm after is like a banner rotator that changes the banner either with every reload or per ip, but instead of rotating a banner would it be able to rotate the actual html page?


    this is for a little one page site, i just want to add variety, change the images, colors etc


  2. #2
    Join Date
    Nov 2001
    (not fancy, but will work)
    if you want to keep the url the same you could use a script like:
    PHP Code:
    = array(
    $pick array_rand($pages1);
    you would just need page1.html to be the same directory as the script and just point people to the script's URL.
    to add pages just follow the array example (making sure to leave the comma off the last one)

    Disclaimer: I haven't tried it with including files, just echoing the page name.

    hope this helps.

  3. #3
    thanks for your reply,
    i'm off out now for a few beers but will give it a go first thing in the morning , yeah it's just basic html pages, no includes or anything


    i'll let you know how it goes

  4. #4
    Hi, you could try the following script. I have used it many times and found it to be good.
    Just copy the following code into a text doc and rename it whatever.php when called it will display random pages from a given directory.
    All you need to edit is the directory and the page extension.

    Hope it helps.

    //***************EDIT FROM HERE*********************
    //Any directory name should end by the trailing "/"
    $directoryName="pages/";//replace the "./" by the name
    // of the directory where your pages are placed, note that the trailing "/" is mandatory
    // Note that this should be a relative path to the directory where this file is located
    $pageExtension=".html";// in this case, we are randomly displaying .html files (note the ".")
    // note that all pages you want to randomly display should have the same extension
    // In the directory specified above by the variable "$directoryName", you can put just anything...
    // from images to movies, other subdirectpries....etc.... just anything.... but,
    // make sure that the files you want to display randomly are the only one in that directory
    // having the extension (in this example ".html") .... so you put there only ".html" files that
    // you want to display randomly..... and can add other non .html files as well
    // You may change the $pageExtension variale to ".php" , ".htm" or what ever ... but then, all that
    // type of file will be randomly displayed!
    ********************* STOP EDITING HERE ***********************************

    function displayAaPHPrandomPage($dirname, $extension){
    $dirhandle = opendir($dirname) or die("ERROR opening ".$dirname);
    while (false !== ($file = readdir($dirhandle))) {
    global $SCRIPT_NAME ;

    if ( [email protected]_dir($file) && (substr_count($file,$extension) !=0) && ( substr_count($SCRIPT_NAME,$file)==0)) {
    $filelist[] = $file;


    $pageNumber = rand(0, sizeof($filelist) - 1);


    return ;


    <?=displayAaPHPrandomPage($directoryName, $pageExtension)?>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts