Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 237
  • Last Modified:

Randomly rotate 5 different PHP header files.

I need to rotate 5 different PHP header files that are include files for a header of a website.
0
ct8270
Asked:
ct8270
  • 4
  • 3
1 Solution
 
ldbkuttyCommented:
you want to include any of the 5 header file randomly ?
0
 
ldbkuttyCommented:
<?
$folderPath = '/path/to/header_files';
if ($handle = opendir($folderPath)) {
   /* This is the correct way to loop over the directory. */
   while (false !== ($file = readdir($handle))) {
       $fileArray[] = $file;
   }
   closedir($handle);
}

shuffle($fileArray);

include "$fileArray[0]";

?>
0
 
markdocCommented:
Idbkutty's answer is best for any number of header files in a directory whose names are unknown to you, the programmer.  But a faster way would be:

<?php

$includespath = "the_includes_path/";
$arrHeaders = array(
    "header1.inc",
    "header2.inc",
    "header3.inc",
    "header4.inc",
    "header5.inc");

$elements = count($arrHeaders);
include($includespath.$arrHeaders[rand(0, $elements - 1)]);

?>

That is, if you know the include files names' at hand.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ldbkuttyCommented:
yes, thats more simple :) But there is shuffle() function that can be used rather than using count() and rand() functions.

<?php

$includespath = "the_includes_path/";
$arrHeaders = array(
    "header1.inc",
    "header2.inc",
    "header3.inc",
    "header4.inc",
    "header5.inc");

shuffle($arrHeaders);
include($includespath . $arrHeaders[0]);

?>
0
 
markdocCommented:
Idbkutty, I used the rand() function because shuffle(), as its name suggests, should use *more* processing time than rand(), because rand() only gives you a random integer and not mess around with an array's elements. Although good for a small number of elements like 5, shuffle() would be much, much slower when dealing with larger arrays in general. ;)

Anyway, a *dangerous* optimization to my former post would be:

<?php

$includespath = "the_includes_path/";
$arrHeaders = array(
    "header1.inc",
    "header2.inc",
    "header3.inc",
    "header4.inc",
    "header5.inc");

$elements = 5;   // Since you've already hard-coded the filenames why not hardcode this too?
include($includespath.$arrHeaders[rand(0, $elements - 1)]);

?>
0
 
ldbkuttyCommented:
yes, you are a keen programmer :)
0
 
markdocCommented:
Thanx Idbkutty, but your 54 K+ points makes you the more prolific one. ;)
0
 
ct8270Author Commented:
Worked perfectly! Thanks for all your help.
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now