How to Get Favicon From Website in PHP

By | April 11, 2013

Get Fevicon of Any Website

Hello friends today i am going to tell you that how to get favicon from website. In this tutorial we will use the get_header function to fetch the favicon icon of website. Note that we are using the pre defined name of favicon.ico to fetch the icon. If the website is having their favicon as other name than it will not show anything. In next tutorial i will let you know how to fetch dynamically favicon icon from any website if the icon name is not fevicon or website owner is using some other name for icon.


Demo:

Fetch Fevicon Icon Code:

Here i am not telling you that how to make form and how to get the url from user. I am putting the php code directly to show how the process works and how you can use it. You can use this code in your project to show your customers/clients website fevicon icon.

<?php
$userPath=$_POST[“url”];
$path=”http://www.”.$userPath.”/favicon.ico”;
$header= get_headers($path);
if(preg_match(“|200|”, $header[0]))
{
echo ‘<img src=”‘.$path.'”>’;
}
else
{
echo “<span>Not found</span>”;
}
?>

We use the $_post[“url”] to get the user input than we will add the fevicon.ico. Than we put whole url in $path so that we will get whole qualified path. After getting the path we will search that favicon.ico is available or not over the website if it is available than get_header will print 200 else not.

So we will find the header response If  there is 200 as response than it will fetch the favicon.ico else it will print “not found”. You can use this code anywhere in your project to fetch the favicon from any website. In our next tutorial we will let you know how you will get/fetch the  favicon icon if its name is not favicon.ico so in short we will find it dynamically.


Leave a Reply

Your email address will not be published. Required fields are marked *