Jump to content

Get random file per day?


Godfrey
 Share

Recommended Posts

I'm trying to display a random file per day. Strangely, this works on my development server but not my live server. On my live server, I get random files but it's on every page refresh. So I'm thinking something's wrong with mktime(), and maybe in combination with getRandom().

This is the code I'm using:

srand(mktime(0,0,0));
        $sectionpages = $pages->find("template=section");
        $randSection = $sectionpages->getRandom(); // get a random "section" page, each has many files
        $randFile = $randSection->files->getRandom(); // get a random file from that random "section" page.
 

Would this be a problem with my live server, like with unix/linux timestamps? Or would this be a compatibility problem with getRandom, if that makes sense? 

I also tried using some code from here: http://processwire.com/talk/topic/2081-when-random-is-too-random/
But I couldn't quite translate the code from working with Pages to files.

I'm running WAMP as my dev server, and my shared hosting uses LAMP :\ So I'm guessing there might be a problem there?

How would you guys approach this? 

As always, many many many thanks for any help!!

 

Link to comment
Share on other sites

That function is now in core for use with WireArrays. This isn't only for pages.

Just as an example with a stack of images:

$fs = new WireArray();
$pa = $pages->find("template=image, image.count>0");
foreach($pa as $p){
  $fs->add($p->image);
}

$f = $fs->findRandomTimed(1);
echo $f->first
  • Like 3
Link to comment
Share on other sites

Hmm, I keep getting the error: "Call to a member function findRandomTimed(1) on a non-object" ... or "Unable to complete this request due to an error. Error has been logged." 

Why would something like this or similar not work?

        $sectionpages = $pages->find("template=section"); // returns a PageArray
        $randSection = $sectionpages->findRandomTimed(1); // findRandomTimed(1) returns 1 page. PageArray is a child class of WireArray right? 
        $randFile = $randSection->files->findRandomTimed(1); // supposedly, the page of $randSection should have a field holding multiple files 
 



 

Link to comment
Share on other sites

  • 1 year later...

Hmm, I have this code working (changes order or listings daily) on my dev server but it fails on live server:

$listings = wire('pages')->get('/listings/')->find('listingActive=1')->findRandomTimed(5);

I'm using PW2.5. The result on live is a different order on every call, on dev the order changes daily as it is supposed to.

I suspect the apache Suhosin extension on the live server is screwing this up as I believe it overrides PHPs rand function and changes seeding in some way. Anyone else seen this issue or have any ideas on making this work in conjunction with Suhosin.

Link to comment
Share on other sites

  On 10/14/2014 at 8:39 PM, muzzer said:

I suspect the apache Suhosin extension on the live server is screwing this up as I believe it overrides PHPs rand function and changes seeding in some way. Anyone else seen this issue or have any ideas on making this work in conjunction with Suhosin.

[php.ini]

suhosin.srand.ignore = Off
suhosin.mt_srand.ignore = Off

OR

[.htaccess]

php_flag suhosin.mt_srand.ignore Off
php_flag suhosin.srand.ignore Off

(this might not necessarily work for you)

  • Like 1
Link to comment
Share on other sites

Thx @sforsman

Yeh, shared host for this site so no php.ini access. Have tried setting this in .user.ini which also has no effect (although other settings - gd etc - can be set here successfully). Also tried in .htaccess, no luck.

I'm wondering how widespread the use of suhosin is - fairly widespread I would guess. In which case perhaps the core function findRandomTimed() should use it's own randomising logic rather than php rand()?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...