-2

Found this PHP script on GitHub https://github.com/ao/favicons and seems work well. Easy to incorporate into other scripts that might need a favicon.

NOTE: Seems respected user here advises not to use this code... See comment below...

The issue I am having is cachine is not working correctly. If you look at top of index.php, you will see:

<?php
error_reporting(0);
// Change the location where images are stored/retrieved
//$_CACHE_PATH = "../favicon_cache";    // one directory up
$_CACHE_PATH = "cache";                 // current directory
if (!isset($_GET['url'])) die();

if (substr( $_GET['url'], 0, 4 ) !== "http") {
    $_GET['url'] = "http://".$_GET['url'];
}
$parse = parse_url($_GET['url']);
$domain = $parse['host'];
if (isset($_GET['refresh'])) {
    @unlink('../'+$_CACHE_PATH+'/'.$domain);
}
if (isset($_GET['debug'])) {
    require 'FaviconDownloader.php';
    $_favicon = new FaviconDownloader($_GET['url']);
    $_favicon->debug();
    die();
}
if (file_exists($_CACHE_PATH+'/'.$domain)) {
    //show cached copy first!
    header('Content-Type: image/png');
    echo file_get_contents($_CACHE_PATH+'/'.$domain);
    die();
}
require 'FaviconDownloader.php';
$favicon = new FaviconDownloader($_GET['url']);
if($favicon->icoExists){
    if (!file_exists($_CACHE_PATH+'/'.$domain)) {
        file_put_contents($_CACHE_PATH+'/'.$domain, $favicon->icoData);
    }
    header('Content-Type: image/png');
    echo file_get_contents($_CACHE_PATH+'/'.$domain);
} else {
    header('Content-Type: image/png');
    echo file_get_contents('default.png');
}
?>

Entire index.php: https://github.com/ao/favicons/blob/master/index.php

No matter what I change the cache folder to, all favicons are still written to the root folder. Have tried a few tweaks with the code, but nothing seems to work. Removing the error_reporting(0); at top of index.php doesn't show any extra errors (only shows: Resource interpreted as Document but transferred with MIME type image/png in dev console).

My cache folder is writable and properly assigned to owner,so pretty sure non-issue there.

Started to ask question / open issue at GitHub but see another person having same issue with no answer from author. In hopes someone here with better PHP skills than myself can point me in right direction.

Woody
  • 1,449
  • 3
  • 19
  • 27
  • Even the most basic tutorial would tell you that `+` is not a concatenation operator in PHP. Whoever wrote this, you don't want to use their code. For anything. – miken32 Apr 09 '19 at 15:59
  • Ok, thanks for voting my question down. Simply needed advice. – Woody Apr 09 '19 at 16:03
  • Hard to understand why people are down voting the question. Not my code, only asked advice on how to fix. – Woody Apr 10 '19 at 02:58
  • I'd assume it's because it's a really obvious syntax error, questions about which are off topic, which is why I voted to close. It's probably also why the only answer you got was from a low-rep user, half an hour after I gave you the answer in comments. I will reiterate my advice about not trusting any code this person has written, if that's the sort of mistake they're making. And don't ever assume that you know who's voting your question up or down. You don't. – miken32 Apr 10 '19 at 19:24

1 Answers1

1

You should use dot operator (.) for concating strings, for example

echo file_get_contents($_CACHE_PATH+'/'.$domain);

should be changed to

echo file_get_contents($_CACHE_PATH.'/'.$domain);
ReZa
  • 347
  • 2
  • 17
  • Thanks @ReZa. I had already tried to replace all of the + with . but no luck. Favicon stops displaying and strange, no type of error is displayed or logged. – Woody Apr 09 '19 at 16:29
  • You have disabled error reporting by error_reporting(0); so you can see no errors‌ @Woody – ReZa Apr 09 '19 at 16:32
  • Thanks again @ReZa. I actually have the error reporting commented out on my test server. The author of script at GitHub has it active, I do not. But seems I needed to place ClouldFlare in dev mode to see my changes. Seems to work with your advice:) – Woody Apr 09 '19 at 16:35