【问题标题】:Random image background change PHP CSS随机图片背景更改 PHP CSS
【发布时间】:2015-04-24 13:19:11
【问题描述】:

我已经设置了一个背景更改脚本,它在我的其他一个网站上运行良好,当我尝试在这个新网站 (http://bit.ly/1Hwbn94) 上执行此操作时,它只显示第一个背景图像并且确实不是每 20 秒随机更改一次(点击另一个页面后)。

我是不是把 PHP 代码弄乱了?到目前为止我找不到错误。也许我必须在 header.php 中添加一些内容?

random_img.php 中的 PHP 代码

<?php
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate"); // HTTP/1.1
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache"); // HTTP/1.0
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header('Content-Type: image/jpeg');
$bilder = glob( "../showcase/images/*.jpg" );
$seed = floor(time()/20); 
srand($seed); 
$random_image = $bilder[rand(0, count($result)-1)];
header('Location:'.$random_image.'');
?>

CSS 代码

body { 
width: 100%;
height: 100%;
margin: 0;
padding: 0;
background: url("../random_img.php") no-repeat top left fixed; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover; background-attachment: fixed;
}

【问题讨论】:

  • path 对吗?
  • 好吧,我想是的。如果路径不正确,则根本没有背景图片,但正在加载第一个。

标签: php css image random


【解决方案1】:

在这一行$result 未定义

$random_image = $bilder[rand(0, count($result)-1)];

正确

$random_image = $bilder[rand(0, count($bilder)-1)];

【讨论】:

  • 哈哈,是的,是害虫。尤其是在阅读数千遍内容时。 :D
【解决方案2】:

问题可能是 CSS 的缓存。

试试你的 CSS

 background: url("../random_img.php?nocache=1") no-repeat top left fixed;

问候...

【讨论】:

    【解决方案3】:

    尝试这样做,后台可能会发生 php 错误,如果您将标题设置为 image/jpeg,您将无法看到。删除所有自定义标头并在同一目录中的 php 文件中尝试以下代码,看看是否有任何错误。

    $bilder = glob( "../showcase/images/*.jpg" );
    $seed = floor(time()/20); 
    srand($seed); 
    $random_image = $bilder[rand(0, count($bilder)-1)];
    echo $random_image;
    

    如果一切设置正确,您的图像位于正确的目录中,您应该会看到所选图像的字符串。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-05-14
      • 2012-07-12
      • 1970-01-01
      • 1970-01-01
      • 2014-01-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多