Yii 2.0: Analyse the server's USER-AGENT against browsecap file to determine it's capability.

no followers

Does your application need to know user's details on browser details, os and device type? We can use PHP's misc. function to achieve that provided you have an update copy of browsercap.ini

<?php
 
/**
 * ABrowser class analyses the server's USER-AGENT against browsecap file to detrmine it's capability.
 * 
 * @author Deepak Pradhan
 * @version 0.1
 */
 
namespace artha\extensions\browser;
 
use artha\components\AComponent;
 
 
/**
 * get_browser— Tells what the user's browser is capable of.
 * Requires 'full_php_browscap.ini] from [http://browscap.org/]
 */
class ABrowser extends AComponent {
 
    public function init()
    {
        parent::init();
    }
 
   /**
    * Tells what the user's browser is capable of.
    * Usage:
    *   // decalre a object
    *      $browser = new ABrowser;
    *   // Set the agent
    *      $agent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36';
    *   // invoke myTest()
    *      $browser->myTest($uagent);
 
    * @param string $uAgent usually server's user-agent
    * @param mixed $rtnArray 
    * @return mixed array or object result
    */
    public function getBrowser($uAgent, $rtnArray)
    {
        return get_browser($uAgent, $rtnArray);
    }
 
    /**
     * Just a basic test
     * @param string $uAgent
     * @param type $rtnArray
     */
    public function myTest($uAgent, $rtnArray = true)
    {
        echo '<pre>';
 
        // return array data - Set $rtnArray = true
        $result = $browser = $this->getBrowser($uAgent, $rtnArray = true);
        print_r($result);
 
        // Setting $rtnArray = false will return stdClass object
        $result = $browser = $this->getBrowser($uAgent, $rtnArray = false);
        print_r($result);
 
        echo '</pre>';
    }
 
}

Be the first person to leave a comment

Please to leave your comment.

Write new article