www.webdeveloper.com
Results 1 to 6 of 6

Thread: Favicon Check : Help!

  1. #1
    Join Date
    Jun 2008
    Location
    Europe
    Posts
    1,096

    Favicon Check : Help!

    I am making a simple script to check if a site has a favicon, and if so, to display it.

    Here is the test script with a form:
    http://www.metataggenerator.org/anal...viconcheck.php

    As you can see, it is a work in progress...

    There are three functions that I would like to do:
    1.) Test to see if there is a favicon in the root directory, and if so, display it.
    2.) Report if there is a favicon tag, and if so...
    3.) Extract and display the favicon meta tag if it exists.

    The first function is not working... I have tried a number of ways and cannot get it to properly report if there is no favicon in the root directory.

    Here is the code:
    PHP Code:
    <form action="faviconcheck.php" method="post">
    <input type="text" name="url" value="http://www.metataggenerator.org" size="50" />
    &larr; Enter URL into this field and click &quot;Favicon Check&quot; (use http://)<br />
    <input type="submit" value="Favicon Check" />
    </form><br />
    <p>&nbsp;</p>
    <?php


    echo '<b>1.) First Function: Test to see if favicon exists</b><br />';  

    function 
    fileExists($path) {
     if(@
    file_get_contents($path,0,NULL,0,1)){return 1;}else{ return 0;}
     } 
    $url $_POST['url']; 
    $path "$url/favicon.ico";

    if (
    fileExists($path) == 1) {
        echo 
    "<img src=\"$path\"> &larr; Favicon is in the root directory";
    } else {
        echo 
    'There is no favicon in the root directory';
    }
    echo 
    '<hr />';
    echo 
    '<b>2.) Second Function: Extract the Favicon tag and display if exists</b><br />'

    $page file_get_contents("$url");

    $explodefirst explode('icon"',$page);
    $headerisgone $explodefirst[1];
    $explodesecond explode('>',$headerisgone);
    $faviconlink $explodesecond[0];

    $explodefirst explode('href="',$faviconlink);
    $headerisgone $explodefirst[1];
    $explodesecond explode('"',$headerisgone);
    $faviconlink $explodesecond[0];

    echo 
    "<img src=\"$faviconlink\"> &larr; Favicon.<br />";
    echo 
    $faviconlink;



    echo 
    '<hr />';
    echo 
    '<b>3.) Third Function: Report if there is a favicon tag.</b>';
    echo 
    '<br />';
    $file file_get_contents("$url");
    if(!
    strpos($file'<link rel="shortcut icon"')) {
           echo 
    '<font color="red">There is no favicon/shortcut tag.</font><br />';
           } else {
    echo 
    'There is a favicon/shortcut tag.<br />';
    }
    echo 
    '<hr />';


    ?>
    Last edited by donatello; 05-28-2011 at 06:48 AM. Reason: Ooops... forgot to post the PHP code... :) :P

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,175
    This was fun:
    PHP Code:
    <?php
    class FavIcon {
       
    /**
        * @var string URL to search for icon
        */
       
    private $pageUrl;
       
    /**
        * @var array Results of parse_url()
        */
       
    private $urlParts;
       
    /**
        * @var string URL to favicon
        */
       
    private $iconUrl;
       
    /**
        * @var string How the favicon was located: 'site root' | 'link tag'
        */
       
    private $iconSource;
       
    /**
        * Constructor
        * @return void
        * @param string $pageUrl
        */
       
    public function __construct($pageUrl)
       {
          
    $parts parse_url($pageUrl);
          if(
    $parts == false) {
             throw new 
    Exception("Unable to parse URL");
          }
          
    $this->pageUrl $pageUrl;
          
    $this->urlParts $parts;
          if(!
    $this->getRootIcon()) {
             
    $this->getLinkIcon();
          }
       }
       
    /**
        * Getter for class vars
        * @return mixed
        * @param string $var
        */
       
    public function __get($var)
       {
          if(
    property_exists($this$var)) {
             return 
    $this->$var;
          }
          else {
             throw new 
    Exception("Invalid class var '$var'");
          }
       }
       
    /**
        * Look for favicon.ico file in site root
        * @return bool
        */
       
    private function getRootIcon()
       {
          
    $url $this->urlParts['scheme'] . '://' $this->urlParts['host'] .
                 
    '/favicon.ico';
          
    $curl curl_init($url);
          
    curl_setopt($curlCURLOPT_NOBODYtrue);
          
    curl_setopt($curlCURLOPT_HEADERtrue);
          
    curl_setopt($curlCURLOPT_RETURNTRANSFERtrue);
          
    $result curl_exec($curl);
          
    curl_close($curl);
          if(
    stripos($result'200 OK')) {
             
    $this->iconUrl $url;
             
    $this->iconSource 'site root';
             return 
    true;
          }
          return 
    false;
       }
       
    /**
        * Look for favicon in a link tag
        * @return bool
        */
       
    private function getLinkIcon()
       {
          
    $curl curl_init($this->pageUrl);
          
    curl_setopt($curlCURLOPT_HEADERfalse);
          
    curl_setopt($curlCURLOPT_RETURNTRANSFERtrue);
          
    $result curl_exec($curl);
          
    curl_close($curl);
          if(!empty(
    $result)) {
             
    $dom = new DOMDocument();
             if(@
    $dom->loadHTML($result)) {
                
    $links $dom->getElementsByTagName('link');
                foreach(
    $links as $link) {
                   if(
    $link->hasAttribute('rel') and $link->hasAttribute('href')) {
                      
    $rel strtolower($link->getAttribute('rel'));
                      if(
    $rel == 'shortcut icon' or $rel == 'icon') {
                         
    $this->iconUrl $link->getAttribute('href');
                         
    $parts parse_url($this->iconUrl);
                         if(empty(
    $parts['host'])) {
                            
    $this->iconUrl $this->urlParts['scheme'] . '://' .
                            
    $this->urlParts['host'] . $parts['path'];
                         }
                         
    $this->iconSource 'link tag';
                         return 
    true;
                      }
                   }
                }
             }
          }
          return 
    false;
       }
    }

    // TEST:
    $test = new FavIcon('http://www.ebookworm.us/');
    if(
    strlen($test->iconUrl)) {
       echo 
    "<p><img src='$test->iconUrl' alt=''> ({$test->iconSource})</p>";
    }
    else {
       echo 
    "<p>No Favicon found.</p>";
    }
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Jun 2008
    Location
    Europe
    Posts
    1,096
    Wow.
    Awesome work.

    I play with scripts as a hobby, but have a long way to go to catch up with some of the folks around here...

    There is one place where this script misses the favicon, my previous script identified the URL, although it was a lot messier code... (like most of my code!)

    For WordPress Sites that have the Favicon in the theme, like this one:
    http://www.TruthNeverDies.com

    The old script actually extracted the Favicon URL...
    (OLD SCRIPT: http://www.metataggenerator.org/anal...oncheckOLD.php )

    Any ideas?

    Thanks again...

    BTW: Your script is up with a form to enter URLs:
    http://www.metataggenerator.org/anal...viconcheck.php

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,175
    Quote Originally Posted by donatello View Post
    Wow.
    ...
    There is one place where this script misses the favicon, my previous script identified the URL, although it was a lot messier code... (like most of my code!)

    For WordPress Sites that have the Favicon in the theme, like this one:
    http://www.TruthNeverDies.com

    The old script actually extracted the Favicon URL...
    (OLD SCRIPT: http://www.metataggenerator.org/anal...oncheckOLD.php )

    Any ideas?
    ...
    It finds it fine for me at the site root.
    PHP Code:
    // TEST:
    $test = new FavIcon('http://www.truthneverdies.com/');
    if(
    strlen($test->iconUrl)) {
       echo 
    "<p><img src='$test->iconUrl' alt=''> ({$test->iconSource})</p>";
    }
    else {
       echo 
    "<p>No Favicon found.</p>";

    Source of output:
    Code:
    <p><img src='http://www.truthneverdies.com/favicon.ico' alt=''> (site root)</p>
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  5. #5
    Join Date
    Jun 2008
    Location
    Europe
    Posts
    1,096
    Ooops...

    looks like there are two favicons in that theme... the one that appears in the theme subdirectory, and the author also placed one in the root.

    I wonder how it works for sites that have it only in the theme...???

    Thanks again...

    I'm going to incorporate this into my newest toy...

    http://www.metataggenerator.org/meta-tag-analyzer/


  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,175
    My understanding is that browsers will only find it if it's a favicon.ico file in the site root, or else if it's specified in an appropriate link tag. Anything else, and the browser is not going to know about it (at least in terms of it being a favicon).
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles