...

View Full Version : PHP Random Include Issue



jbenoit
08-01-2012, 04:02 PM
I grabbed and slightly modified this PHP Include Random code somewhere, but haven't been able to make it work for me the way it should (or should I say, the way I want it to). I'm pretty weak at PHP. The code randomly includes 1 of 7 files in a global area across the website I'm building, so needs to have an absolute path (in this case localhost as I'm developing locally).

The problem: This code will correctly display 1 of the 7 files randomly that are in the folder "includes/quotes/", but only like every 2 or 3 refreshes. The other times it just displays nothing, not even "no files found". If I remove
$_SERVER['DOCUMENT_ROOT'] . "/includes/quotes/"; from the $path definition and replace it simply with "include/quotes/, it works fine, but only at the root level (no sub level pages display the include). Also, the array at the top with quote1.php, etc. seems to not matter... The random files are simply chosen from the path I specify.

Ideas on making this code play nice with me? I'm sure it's an easy fix/tweak, I'm just not that savvy:


<?php

error_reporting (E_ALL | E_STRICT);

$files = array('quote1.php', 'quote2.php', 'quote3.php', 'quote4.php', 'quote5.php', 'quote6.php', 'quote7.php');

$path = $_SERVER['DOCUMENT_ROOT'] . "/includes/quotes/";

if( $handle = opendir( $path ) ) {
while (false !== ($file = readdir($handle))) {
if( $file != "." && $file != ".." && !is_dir( $path.$file ) ) {
$files[] = $path.$file;
}
}
// You need to populate the $files variable before you count it;)
$file = $files[ rand( 0, count( $files ) ) ];
// Now we can include it!
include_once ("$file");
}

else {
print "no files found";
}

?>

DrDOS
08-01-2012, 04:48 PM
Your code to generate the random number needs to be directly below the $files array, it needs that information to make the random number, and the include part of the code needs the information that it makes to do it's job.

I must say this seems to be complicated code to do such a simple thing. If you include a .txt file it is simply written verbatim wherever you put it in the page. But very much can be done to make it simpler. The quotes could also be variables, or array elements themselves, and other things could be simplified too.

jbenoit
08-01-2012, 05:06 PM
I'd previously used this script which works as well & is definitely simpler and I'd probably go back to, but was equally having issues, specifically with absolute paths. Again, this code works fine for top level pages. I guess ultimately if I knew how to incorporate an absolute URL as my $path, that would solve the problem. Is there an easy way to do that? Simply adding the URL doesn't work.


<?php
// Load an array with filenames
$myIncludeFiles = array('quote1.php', 'quote2.php', 'quote3.php', 'quote4.php', 'quote5.php', 'quote6.php', 'quote7.php');
// Get the size of the array
$arraySize = count($myIncludeFiles);
// Pick a random number. Array indexes run 0 -> N-1
$randNo = rand(0,$arraySize-1);
//path to files
$path = "includes/quotes/";
// Include the file
include_once($path.$myIncludeFiles[$randNo]);
?>

DrDOS
08-01-2012, 05:24 PM
To designate this folder ( on a nix OS ) ./ , one folder up ../ , repeat as needed. absolute path "http://".$_SERVER[HTTP_HOST]."/"; Look at the page source to see that it's correct.

jbenoit
08-01-2012, 06:52 PM
So, I added this into the code but I'm probably missing something (still shows blank). My absolute path is "http://localhost/includes/quotes/". Can you show me how exactly I would implement that using this code block? I feel like I need to define the actual http address (in this case "localhost") somewhere. Just not sure the exact syntax.


<?php
// Load an array with filenames
$myIncludeFiles = array('quote1.php', 'quote2.php', 'quote3.php', 'quote4.php', 'quote5.php', 'quote6.php', 'quote7.php');
// Get the size of the array
$arraySize = count($myIncludeFiles);
// Pick a random number. Array indexes run 0 -> N-1
$randNo = rand(0,$arraySize-1);
//path to files
$path = "http://".$_SERVER[HTTP_HOST]."/includes/quotes/";
// Include the file
include_once($path.$myIncludeFiles[$randNo]);
?>

Arcticwarrio
08-01-2012, 07:29 PM
define you server in a config or function file somewhere:



define(SITE, "http://localhost");
//or
define(SITE, "http://yourwebsite.com");


then you can change just 1 line when you go live:


//use as
$path = SITE."/includes/quotes/";

DrDOS
08-01-2012, 08:30 PM
You need to include the file in the whole path, just like you do when you move or copy a file.

jbenoit
08-01-2012, 09:29 PM
So, this is what I've got right now:


<?php
// Load an array with filenames
$myIncludeFiles = array('quote1.php', 'quote2.php', 'quote3.php', 'quote4.php', 'quote5.php', 'quote6.php', 'quote7.php');
// Get the size of the array
$arraySize = count($myIncludeFiles);
// Pick a random number. Array indexes run 0 -> N-1
$randNo = rand(0,$arraySize-1);
//SITE definition
define(SITE, "http://localhost");
//path to files
$path = SITE."/includes/quotes/";
// Include the file
include_once($path.$myIncludeFiles[$randNo]);
?>

Am I heading in the right direction? It all seems logical, but at least using my localhost (MAMP), I'm getting no include with this. Anything else I'm missing?

Arcticwarrio
08-01-2012, 11:34 PM
in the define you need quotes for the name

define("SITE", "http://localhost");

jbenoit
08-02-2012, 04:46 AM
So I tried that on the testing server & then live to see what errors I was getting & it seemed like in general it's a PHP security setting that doesn't like urls. So I tried one more thing I'd tried before in a slightly different way and I had SUCCESS (finally) both locally (MAMP) and live.

Here it is:

<?php
// Load an array with filenames
$myIncludeFiles = array('quote1.php', 'quote2.php', 'quote3.php', 'quote4.php', 'quote5.php', 'quote6.php', 'quote7.php');
// Get the size of the array
$arraySize = count($myIncludeFiles);
// Pick a random number. Array indexes run 0 -> N-1
$randNo = rand(0,$arraySize-1);
//path to files
$path = "/includes/quotes/";
// Include the file
include_once($_SERVER['DOCUMENT_ROOT'].$path.$myIncludeFiles[$randNo]);
?>

Thanks for your input!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum