Search code examples
phphtmlfavicon

Random Favicon On Web Page


I am trying to show a different favicon on each refresh of the browser from a selection of 3 images. Here is the code which i currently have:

<head>
<!--Other Stuff-->
<?
$faviconNumber = rand(1,3);
if ($faviconNumber == 1) {
  echo '<link rel="shortcut icon" type="image/ico" href="../img/favicon1.ico"/>';
}
else if ($faviconNumber == 2) {
  echo '<link rel="shortcut icon" type="image/png" href="../img/favicon2.png"/>';
}
else {
  echo '<link rel="shortcut icon" type="image/png" href="../img/favicon3.png"/>';
}
?>
</head>

There are 3 different favicons (2 png, 1 ico). However when this is run it will only ever show the second favicon.


Solution

  • You can change your icon names like favicon_1.ico, favicon_2.ico and favicon_3.

    After this, please change your code like below.

    <head>
    <link rel="shortcut icon" type="image/ico" href="../img/favicon_<?php echo rand(1,3); ?>.ico"/>
    </head>