1

I want to set a favicon icon on this page, but I cannot do it by echo because the PHP is going to echo out JSON format. How do I do it?

if I add this line It is mixing up with my main output

echo '<link rel="icon" href="/favicon.ico" type="image/x-icon">';

the output I'm getting

<link rel="icon" href="/favicon.ico" type="image/x-icon">hello world

my PHP code

<?php

header('Content-Type: application/json');

class Main {
    function mFun() {
        echo "hello world";
    }
}

$main = new Main();
echo '<link rel="icon" href="/favicon.ico" type="image/x-icon">';
$main -> mFun();

?>

UPDATE

why anyone not noticing header('Content-Type: application/json');

  • it can be done, just make sure you put favicon.ico in the same directory as ur php and remove the slash / – Alan Deep Apr 29 '20 at 15:08
  • Add a favicon to the root of your API request url. You don't need to add `link` include – Jack Apr 29 '20 at 15:08
  • By doing that I'm getting `` with my json –  Apr 29 '20 at 15:09
  • @JackNicholson can you elaborate with code? –  Apr 29 '20 at 15:10
  • 1
    @Arghadip Sorry for the late reply, What I meant is that if you are trying to return JSON data with a favicon then the favicon will be taken from the root of your site automatically by some browsers. (Latest chrome does currently do this). You don't need to return the favicon in your page at all with any code. Simply have `/favicon.ico` in your root. I hope this helps you :) IF you are trying to change a favicon then you need to wait for cache to expire. It will happen in time, you won't be able to clear the cache for it – Jack May 01 '20 at 06:53
  • Not all browsers will pull the favicon. It will depend on your browser. Hopefully this helps - I don't know why this was closed with a useless 'duplicate' thread. Had nothing to do with JSON requests... – Jack May 01 '20 at 06:55

0 Answers0