Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question PHP Random Include Issue

    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:

    Code:
    <?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";
    		  }       
    		    
    		?>

  • #2
    Senior Coder
    Join Date
    Sep 2010
    Posts
    2,451
    Thanks
    17
    Thanked 275 Times in 275 Posts
    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.

  • #3
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 Code:
    <?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]);
        
    ?>

  • #4
    Senior Coder
    Join Date
    Sep 2010
    Posts
    2,451
    Thanks
    17
    Thanked 275 Times in 275 Posts
    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.

  • #5
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 Code:
    <?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]);
    ?>

  • #6
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    738
    Thanks
    20
    Thanked 85 Times in 85 Posts
    define you server in a config or function file somewhere:

    PHP Code:
    define(SITE"http://localhost");
    //or
    define(SITE"http://yourwebsite.com"); 
    then you can change just 1 line when you go live:

    PHP Code:
    //use as
    $path SITE."/includes/quotes/"

  • #7
    Senior Coder
    Join Date
    Sep 2010
    Posts
    2,451
    Thanks
    17
    Thanked 275 Times in 275 Posts
    You need to include the file in the whole path, just like you do when you move or copy a file.

  • #8
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    So, this is what I've got right now:

    PHP Code:
    <?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?

  • #9
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    738
    Thanks
    20
    Thanked 85 Times in 85 Posts
    in the define you need quotes for the name

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

  • #10
    New to the CF scene
    Join Date
    Aug 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile

    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 Code:
    <?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!


  •  

    Tags for this Thread

    Posting Permissions

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