php - 每次刷新时随机出现图标

标签 php favicon

我正在尝试制作一个在每次刷新时更改图标的脚本。

<?php
 $dir = "img/icons/";
 $files = array();
 if ($handle = opendir($dir)) {
   while (false !== ($file = readdir($handle))) {
    $files[] = $file;
   }
 }
 $icon = $files[rand(2,count($files)-1)];
 echo "<LINK REL=\"shortcut icon\" HREF=\'img/icons/'.$icon."\">";
?>

此脚本从/img/icons 文件夹中获取每个图标,并随机选择一个。问题是,当我将其粘贴到 HTML 文件中时,页面顶部会出现奇怪的字符“; ?>。有人可以修复我的脚本吗?

最佳答案

<?php
 $dir = "img/icons/";
 $files = array();
 if ($handle = opendir($dir)) {
   while (false !== ($file = readdir($handle))) {
    $files[] = $file;
   }
 }
 $icon = $files[rand(2,count($files)-1)];
 echo '<LINK REL="shortcut icon" HREF="img/icons/'.$icon.'">';
?>

<强> Here 你可以看看在 HTML 中使用 PHP。您的服务器也应该为此进行正确配置。我总是推荐 .php 来处理 PHP 脚本。

希望这有帮助。

关于php - 每次刷新时随机出现图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10665570/

相关文章:

html - 如果 favicon 没有命名为 favicon.ico 怎么办?

php - 登录 url session 重写

javascript - ajax 不适用于 url 中包含 id 的页面?

php - MySQL 更新枚举

ruby-on-rails - 未提供 Rails 3.1 favicon.ico

seo - Favicon/图标未反射(reflect)在谷歌搜索结果中

image - 为什么我的 favicon.ico 文件比其各部分的总和大得多?

PHP:解析 postgre 的默认字符串时间戳格式

javascript - 服务器发送的事件轮询导致长时间延迟

favicon - 如何为 mkdocs 网站添加 favicon?