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

Go To StackoverFlow.com

1

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]) ;
?>
2012-04-05 14:55
by GibsonFX
You are back with your script ... i think a total redesign would be the best ... am working on i - Baba 2012-04-05 15:05


2

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.

2012-04-05 15:04
by Bee
You sir are a legend, why wasn't I offered this simple solution sooner? Saves so much headache lo - GibsonFX 2012-04-05 15:16
Nice but your script would still fail once in a while ... your array is not starting form 0 rand(0,count($pages)-1); would generate array key 0 which does not exist and would make your script fai - Baba 2012-04-05 15:19
Thank you for the tip Baba, I did wonder if that was what was causing the blank pages every so often : - GibsonFX 2012-04-05 15:23
Nicely done, +1 - Alix Axel 2012-04-05 15:25
You are welcome Luke - Bee 2012-04-05 16:26


1

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
2012-04-05 15:06
by Mike
Indeed, the code works just fine, you should be getting some PHP erro - gitaarik 2012-04-05 15:07
Thank you very much : - GibsonFX 2012-04-05 15:31


1

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

:)

2012-04-05 15:11
by Baba
Thank you very much : - GibsonFX 2012-04-05 15:21
You are always welcom - Baba 2012-04-05 15:25
Ads