Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6469391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T05:59:06+00:00 2026-05-25T05:59:06+00:00

Situation I have a relatively short php code I found and tweaked that includes

  • 0

Situation

I have a relatively short php code I found and tweaked that includes a random html file from my ‘randomizer’ folder into my page.

Here is the code

<?php
  error_reporting(0);
  function random_file($string){
    return ((file_exists($string))&&(preg_match('#(\.html)$#i',$string)))  ? true : false ;
  }

  define('OUTPUT_TYPE','text');

  define('RANDOM_FILES_FOLDER','randomizer/');

  $my_array = Array();
  $my_dir = RANDOM_FILES_FOLDER ;

  if ($dir = @opendir("$my_dir")) {

    while (($file = readdir($dir)) !== false) {
      if ($file != "." && $file != ".."  && !is_dir($my_dir.$file))
      {
        switch(OUTPUT_TYPE):

          case'text':
            if(random_file($my_dir.$file)){
              $my_array[] = $file;
            }
          break;

          default:
          break;

        endswitch;

      }
    }
    closedir($dir);
  }

  if(count($my_array)>0){

    $random_number = rand(0, count($my_array)-1);
    $random_file = $my_array[$random_number];

    switch(OUTPUT_TYPE):

      case'text':
        include($my_dir.$random_file);
      break;

      default:
      break;

    endswitch;

  }
?>

Question

It does what it is supposed to do (perhaps someone can trim/optimize that code for me) but I have only a few files to randomize and I don’t want the same file to appear twice when I refresh or open the page a day after.

I think cookies may be the answer, but not sure how to do anything with them.

Can anyone write a piece code to add to mine to do that or provide a code that has all those attributes? keep in mind it must include files at random from a folder, I don’t want the code from those files on my actual page code for CMS purposes

Keep in mind I am a PHP and Javascript beginner with VERY basic knowledge, so please dumb it down for me.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T05:59:07+00:00Added an answer on May 25, 2026 at 5:59 am

    Very rough:

    session_start();
    $dir = 'randomizer/';
    
    if (empty($_SESSION['files'])) {
        $_SESSION['files'] = array_filter(scandir($dir), function ($file) use ($dir) {
            return is_file($dir . $file) && preg_match('#(\.html)$#i', $file);
        });
        shuffle($_SESSION['files']);
    }
    
    include $dir . array_shift($_SESSION['files']);
    

    Keep a list of all candidate files in the session, use them one by one. That way all files will be displayed once in random order before the cycle starts again. Only not recommended if the list is very long.

    It’s worth noting that the array_filter callback syntax requires PHP 5.3.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a php application that has about 50-55 code files. The file that
I came across a situation where I have a pretty big file that I
Situation: I have a simple XML document that contains image information. I need to
I have the following situation: I have a certain function that runs a loop
I have situation, where running a query that filters by an indexed column in
I have situation in which I read a record from a database. And if
I have a situation where I could really benefit from having system like memcached,
I just found an interesting situation. Suppose you have some SwingWorker (I've made this
I have a situation with a relatively large git repository located in a virtual
I'm a .Net developer but I have a situation where I can't guarantee that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.