GibsonFX
GibsonFX

Reputation: 1060

PHP Script on timer, can't get file to cache

I've been stuck on this for a couple of days now, and I'm really having trouble getting this script to function correctly.

I have a very basic starting script, which outputs a random page of text/html/php every time the page refreshes.

<?php
$pages = array(1 => 'text1-1.php', 2 => 'text1-2.php', 3 => 'text1-3.php', 4 => 'text1-  4.php');
$key = array_rand ( $pages );
include($pages[$key]) ;
?>

My goal is to have a script that only changes the output every 1 or 2 days (or what ever time is specify), so no matter how many times you refresh the page, the output won't change until the timer expires.

I have tried the following pieced together from tips people have given me, but no matter what I try the script always outputs something different, every time the page is refreshed.

I think the problem is the file isn't caching, but I don't understand why.

If there are any other problems you can see, I would be grateful for some pointers. :)

Thank you for any help you can offer. :)

<?php
$pages = array(1 => 'text1-1.php', 2 => 'text1-2.php', 3 => 'text1-3.php', 4 => 'text1-   4.php');

$cachefile = "cache/timer.xml";
$time = $key = null;
$time_expire = 24*60*60;

if(is_file($cachefile)) {
    list($time, $key) = explode(' ', file_get_contents($cachefile));
}

if(!$time || time() - $time > $time_expire) {
    $key = rand(0,count($pages)-1);
    file_put_contents($cachefile, time().' '.$key);
}
include($pages[$key]) ;
?>

Upvotes: 1

Views: 240

Answers (3)

Bee
Bee

Reputation: 2512

How about this method to generate your random number:

srand(floor(time()/60/60/24/2));
$key = rand(0,count($pages)-1);

It fixes the seed for two days (technically for 48 hours, not necessarily matching two full days) so the first call to rand() always returns the first number based on that seed.

Upvotes: 2

Baba
Baba

Reputation: 95161

Replace

if(!$time || time() - $time > $time_expire) {

With

if (! $time || (time () - $time) > $time_expire) {

Also

mt_rand is better than rand you might want to change that too

Edit 1

Since your array is not starting form 0 you should also

replace

$key = rand(0,count($pages)-1);

With

$key = mt_rand( 1, count ( $pages ));

Or

make your array

$pages = array (
        0 => 'text1-1.php',
        1 => 'text1-2.php',
        2 => 'text1-3.php',
        3 => 'text1-4.php' 
);

Tested your script now .. it works perfectly fine ... let me know if you need anything else

Thanks

:)

Upvotes: 1

Mike
Mike

Reputation: 847

Have you checked to make sure the file is actually created? Does the directory "cache" exist? Can you web server process write to it? Note that file_put_contents will issue a WARNING only if it cannot create the file; no error will be produced and the script will appear to run without a problem if you have your server set to not show warnings.

I absolutely agree the file is not being written; your code works fine for me. Without cache/:

Warning: file_put_contents(cache/timer.xml): failed to open stream: No such file or directory in ...

With cache/ and write permissions:

$ php test.php
text1-1.php
$ php test.php
text1-1.php
$ php test.php
text1-1.php
$ php test.php
text1-1.php
$ php test.php
text1-1.php
$ php test.php
text1-1.php
$ php test.php
text1-1.php
$ php test.php
text1-1.php
$ php test.php
text1-1.php
$ php test.php
text1-1.php

Upvotes: 1

Related Questions