每7次刷新更改背景颜色

时间:2014-08-26 11:45:51

标签: php session

我需要每7次刷新更改bgcolor,但我需要保持背景颜色相同,直到第7次刷新。我需要在同一个文件中保存当前颜色和刷新计数器。

这是我的代码:

<?php
$a = "count.txt";

$verify = file_get_contents($a);
$visit = $varify;
$newvisit = $visit +1;

$counter = fopen ($a, "w");
fwrite($counter,$newvisit);
fclose($counter);

if($visit %7 == 0){
    $color = "rgb(" . rand(0,255) . "," . rand(0,255) . "," . rand(0,255) . ")";
    $ocolor = fopen($a, "w" );
    fwrite($ocolor, $newvisit.";".$color);
    fclose($ocolor);
}
echo $visit;
?>

<body style="background-color: <?php echo $culoare; ?>">

</body>

2 个答案:

答案 0 :(得分:1)

<?php
    session_start();
    if(!isset($_SESSION['initial'])){
        $_SESSION['initial']="1";
    } else {
        $_SESSION['initial']+=1;
    }
    $visit=$_SESSION['initial'];
    $visit %7;

    if($visit %7 == 0){
      $_SESSION['code'] = "rgb(" . rand(0,255) . "," . rand(0,255) . "," . rand(0,255) . ")";
    } else if(isset($_SESSION['code'])) {
      $_SESSION['code']=$_SESSION['code'];
    } else {
      $_SESSION['code']="rgb(0,0,0)";
    }
?>
    <body style="background-color: <?php echo $_SESSION['code']; ?>">

    </body>

答案 1 :(得分:1)

<?php
if(is_readable('color.txt')) {
   $data = explode('|', file_get_contents('color.txt'));
} else {
   $data = [0,0];
}
$count = $data[0];
$color = $data[1];
if($count %7 == 0) {
    $color = "rgb(" . rand(0,255) . "," . rand(0,255) . "," . rand(0,255) . ")";
}
file_put_contents('color.txt', ++$count.'|'.$color);
?>


<body style="background-color: <?php echo $color; ?>">

</body>