简单随机生成器脚本不起作用

时间:2012-04-04 15:05:02

标签: php

我有这个脚本,但它无法正常工作,不确定如何修复它,请给我任何提示?所有我走出另一端的是字母“t”

谢谢:)

<?php
$cachefile = './current_t_id';
$time = $id = null; // assume we have no cached quote
$change_every = 3600; // seconds
$pages = array(1 => 'text1-1.php', 2 => 'text1-2.php');

foreach($pages as $pagekey => $page){
    if($pagekey == $siteId){
        include($page);
    }
}

// if cached quote exists, load it
if(is_file($cachefile)) {
    list($time, $id) = explode(' ', file_get_contents($cachefile));
}

// if no cached quote or duration expired, change it
if(!$time || time() - $time > $change_every) {
    srand ((double) microtime() * 100000);
    $id = rand(0,count($page)-1);
    file_put_contents($cachefile, time().' '.$id); // update cache
}

// echo the quote, be it cached or freshly picked 
echo ($page[$id]);
?>

1 个答案:

答案 0 :(得分:1)

好的,我将给你3个不同的例子

变量

$quotes = array (
        "hello",
        "baba",
        "luke" 
);


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

一个。使用随机引号

// Using Ramdom Quptes
$key = array_rand ( $quotes );
echo $quotes [$key]; 
//Or
include($pages[$key]) ;

B中。使用罗宾

// Using Robin
$cacheFile = "robin.cache";
$robin = null;
$quotesID = intval ( file_get_contents ( $cacheFile ) );
$totalQuotes = count ( $quotes );

$key = ($quotesID < ($totalQuotes - 1)) ? $quotesID ++ : 1;
file_put_contents ( $cacheFile, $quotesID );

echo $quotes [$key];
//Or
include($pages[$key]) ;

使用计时器

// Using Timer

$cacheFile = "timer.cache";
$expiration = 3600;

$robin = null;
list($quotesID, $time) = explode(' ', file_get_contents($cacheFile));
$totalQuotes = count ( $quotes );

if($time < (time() - $expiration))
{
    $key = mt_rand(0,count($pages)-1);
    file_put_contents($cacheFile, time().' '.$id);
}
echo $quotes [$key];
//Or
include($pages[$key]) ;

我希望您的问题现在可以解决

由于 :)

相关问题