Godfrey Posted September 2, 2013 Share Posted September 2, 2013 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 More sharing options...
Soma Posted September 2, 2013 Share Posted September 2, 2013 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 3 Link to comment Share on other sites More sharing options...
Godfrey Posted September 4, 2013 Author Share Posted September 4, 2013 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 More sharing options...
Soma Posted September 4, 2013 Share Posted September 4, 2013 findRandomTimed() returns an array not a single page. Link to comment Share on other sites More sharing options...
muzzer Posted October 14, 2014 Share Posted October 14, 2014 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 More sharing options...
sforsman Posted October 16, 2014 Share Posted October 16, 2014 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) 1 Link to comment Share on other sites More sharing options...
muzzer Posted October 19, 2014 Share Posted October 19, 2014 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now