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]) ;
?>
1
  • You are back with your script ... i think a total redesign would be the best ... am working on it Commented Apr 5, 2012 at 15:05

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

You sir are a legend, why wasn't I offered this simple solution sooner? Saves so much headache lol
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 fail
Thank you for the tip Baba, I did wonder if that was what was causing the blank pages every so often :)
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

1 Comment

Indeed, the code works just fine, you should be getting some PHP error
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

:)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.