PHP code example of kodhe / agent

1. Go to this page and download the library: Download kodhe/agent library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

kodhe / agent example snippets




// Load the library
$this->load->library('agent');

// Browser detection
echo $this->agent->browser();      // e.g., "Chrome"
echo $this->agent->version();       // e.g., "120.0.0.0"
echo $this->agent->platform();      // e.g., "Windows 10"

// Device detection
echo $this->agent->mobile();        // e.g., "iPhone"
echo $this->agent->is_mobile();     // true/false
echo $this->agent->is_robot();      // true/false

// Referrer detection
echo $this->agent->referrer();
echo $this->agent->is_referral();

// Language and charset
echo $this->agent->accept_lang('en');
echo $this->agent->accept_charset('utf-8');

use Kodhe\Agent\Agent;

$agent = new Agent();

// Browser detection
echo $agent->browser();
echo $agent->version();

// Device detection  
echo $agent->isMobile();
echo $agent->isDesktop();
echo $agent->isRobot();

// Advanced usage with drivers
$browserDriver = $agent->getBrowserDriver();
echo $browserDriver->getBrowser();
echo $browserDriver->getVersion();

$agent = new Agent();
$agent->parse('Mozilla/5.0 (Custom Agent String)');

echo $agent->browser();
echo $agent->platform();


$config['agent'] = [
    'cache_detection' => true,
    'cache_ttl' => 3600,
    'detect_robots' => true,
    'detect_mobile' => true,
];

$agent = new Agent();
$agent->loadConfig();

$agent->getBrowserDriver()  // BrowserDriver instance
$agent->getDeviceDriver()   // DeviceDriver instance
$agent->getOsDriver()       // OsDriver instance
$agent->getRobotDriver()    // RobotDriver instance
$agent->getParser()         // UserAgentParser instance

use Kodhe\Agent\Collections\BrowserCollection;

$collection = new BrowserCollection();
$collection->add('MyBrowser', 'My Custom Browser');

$agent = new Agent();
$agent->getBrowserDriver()->setCollection($collection);

use Kodhe\Agent\Contracts\AgentDriverInterface;
use Kodhe\Agent\Parsers\UserAgentParser;

class CustomDriver implements AgentDriverInterface {
    private $parser;
    
    public function __construct(UserAgentParser $parser) {
        $this->parser = $parser;
        $this->detect();
    }
    
    public function detect(): void {
        // Custom detection logic
    }
    
    public function getValue(): string {
        return 'custom_value';
    }
    
    public function isMatch(?string $key = null): bool {
        return true;
    }
}