soumen-dey/laravel-user-agent

An advanced user agent detector for Laravel 5.5 and above.

v1.0 2018-07-08 15:14 UTC

This package is not auto-updated.

Last update: 2024-04-17 01:53:55 UTC


README

An advanced user agent detector for Laravel 5.5 and above.

A powerful package allowing you to detect the user's Browser, Platform and more. This package provides a very simple API and requires no configurations at all.

Installation

Via Composer

$ composer require soumen-dey/laravel-user-agent

Note: This package is tested and is fully compatible with Laravel 5.5 and above, not sure about support below Laravel 5.5.

If you are on Laravel 5.4 or below, you need to manually register the ServiceProvider and the Alias. In your config/app.php file just do:

'providers' => [
    // ...
    Soumen\Agent\AgentServiceProvider::class,
];

'aliases' => [
	//...
    'Agent' => Soumen\Agent\Facades\Agent::class,
];

For Laravel 5.5 and above, you don't need to do anything, just install the package and you are good to go!

Usage

Setup

If you have an alias set up for the Agent class, you don't need to import anything. However, if the alias is not set, you can do:

use Soumen\Agent\Agent;

// or

use Soumen\Agent\Facades\Agent

You are now ready to use the package.

API

The Agent Facade

You can access the visitor's details such as the browser using:

Agent::browser(); // returns all the details related to the visitor's browser

Likewise you can also fetch other details:

Agent::ip(); // the ip address of the visitor
Agent::device(); // details related to the visitor's device, eg, Phone, Tablet, etc.
Agent::header(); // details related to the request's header
Agent::platform(); // details related to the platform of the visitor, eg. Windows, etc.
Agent::userAgent(); // returns the HTTP_USER_AGENT string

You can also fetch server related info:

Agent::server(); // returns details related to the server

Available Methods

Methods
Agent::ip() Returns the visitor's ip address.
Agent::all() Returns all the details about the visitor.
Agent::get() Same as the all() method.
Agent::header() Get information related to the request's header.
Agent::server() Get information related to the applications's server.
Agent::device() Get information related to the visitor's device, eg. Apple, Samsung, Mobile, Tablet, etc.
Agent::browser() Get information related to the visitor's browser.
Agent::platform() Get information related to the visitor's platform, eg. Windows, Mac, etc.
Agent::userAgent() Returns the HTTP_USER_AGENT string.

You can also retrieve properties:

Agent::browser('name'); // Firefox 61
Agent::browser()->name; // Firefox 61
Agent::browser()->getName(); // Firefox 61

Individual services handles the task of fetching visitor information, for more information read the Services section.

To get individual service:

$browser = Agent::get('browser'); // returns the browser service
$browser = Agent::browser(); // same as above

Services

The Agnet facade is a global wrapper for individual services that handles the actual task of fetching information about the visitor. You can call the Services directly if you want to be more specific.

Get details about the visitor's browser for example:

use Soumen\Agent\Services\Browser;

Browser::get(); // returns all details related to the visitor's browser
Browser::getDetails(); // same as get() method

The API for every service is similar except for some minor changes, so you can do:

use Soumen\Agnet\Services\Device;
use Soumen\Agent\Services\Platform;

$device = Device::get(); // get details about the visitor's device
$platform = Platform::get(); // get details related to the visitor's platform

Available Services

Service Namespace Usage
Browser Soumen\Agent\Services\Browser Browser::get(),
Browser::getDetails()
Returns all the information related to the visitor's browser
Platform Soumen\Agent\Services\Platform Platform::get(),
Platform::getDetails()
Returns all the information related to the visitor's platform, eg. Windows, Mac, etc.
Device Soumen\Agent\Services\Device Device::get(),
Device::getDetails()
Returns all the information related to the visitor's device, eg, Mobile or Tablet, Name, etc.
UserAgent Soumen\Agent\Services\UserAgnet UserAgent::get(),
UserAgent::getDetails()
Returns the visitor's HTTP_USER_AGENT string
Header Soumen\Agent\Services\Header Header::get(),
Header::getDetails()
Returns all information related to the request's header
Server Soumen\Agent\Services\Server Server::get(),
Server::getDetails()
Returns information about the application's server.

Fetching Properties

To get individual browser property such as name do one of the following:

$browser = Agent::browser('name');
$browser = Agent::browser()->name;
$browser = Agent::browser()->name();
$browser = Agent::browser()->getName();

Using the Service:

use Soumen\Agent\Services\Browser;

// get the name of the browser
$browser = Browser::get('name');
$browser = Browser::get()->name;
$browser = Browser::get()->name();
$browser = Browser::get()->getName();

// or

$browser = Browser::getDetails('name');
$browser = Browser::getDetails()->name;
...
// You get the idea

This applies to all the available properties of the browser. Refer to the section for each service for more information.

Properties as parameter

You can use the property name as the parameter, so you can do:

Agent::browser('name'); // Firefox 61
Agent::platform('name'); // Windows 10

Instead of:

Agent::browser()->name;
Agent::platform()->name;

Using individual service:

use Soumen\Agent\Services\Browser;
use Soumen\Agent\Services\Platform;

Browser::get('name'); // Firefox 61
Platform::get('name'); // Windows 10

// or

Browser::getDetails('name'); // Firefox 61
Platform::getDetails('name'); // Windows 10

Note: The parameter should be the exact same string as the property name.

For a list of available properties, refer to the Properties section of the respective Service

Browser

Using the Facade:

$browser = Agent::browser();

Using the Service:

use Soumen\Agent\Services\Browser;

$browser = Browser::get();

// or

$browser = Browser::getDetails();
Available Properties:
Properties Methods
$browser->name $browser->name(),
$browser->getName()
Returns the name of the browser.
$browser->version $browser->version(),
$browser->getVersion()
Returns the browser's version number.
$browser->versionMajor $browser->versionMajor(),
$browser->getVersionMajor()
Returns the browser's semantic major version number.
$browser->versionMinor $browser->versionMinor(),
$browser->getVersionMinor()
Returns the browser's semantic minor version number.
$browser->versionPatch $browser->versionPatch(),
$browser->getVersionPatch()
Returns the browser's semantic patch version.
$browser->engine $browser->engine(),
$browser->getEngine()
Returns the browser's rendering engine.
$browser->family $browser->family(),
$browser->getFamily(),
$browser->getVendor()
Returns the browser's vendor. Eg. Chrome, Firefox, etc.

Platform

Using the Facade:

$platform = Agent::platform();

Using the Service:

use Soumen\Agent\Services\Platform;

$platform = Platform::get();

// or

$platform = Platform::getDetails();
Available Properties:
Properties Methods
$platform->name $platform->name(),
$platform->getName()
Returns the name of the Operating System.
$platform->family $platform->family(),
$platform->getFamily(),
$platform->getVendor()
Returns the Operating System's vendor. Eg. Windows, Mac, Linux, etc.
$platform->version $platform->version(),
$platform->getVersion()
Returns the Operating System's human friendly version eg. XP, Vista, etc.
$platform->versionMajor $platform->versionMajor(),
$platform->getVersionMajor()
Returns the Operating System's semantic major version number.
$platform->versionMinor $platform->versionMinor(),
$platform->getVersionMinor()
Returns the Operating System's semantic minor version number.
$platform->versionPatch $platform->versionPatch(),
$platform->getVersionPatch()
Returns the Operating System's semantic patch version.

Device

Using the Facade:

$device = Agent::device();

Using the Service:

use Soumen\Agent\Services\Device;

$device = Device::get();

// or

$device = Device::getDetails();
Available Properties:
Properties Methods
$device->family $device->family(),
$device->getFamily(),
$device->getVendor()
Returns the device's vendor like Samsung, Apple, Huawei.
$device->model $device->model(),
$device->getModel()
Returns the device's brand name like iPad, iPhone, Nexus.
$device->mobileGrade $device->mobileGrade(),
$device->getMobileGrade()
Returns the device's mobile grade in scale of A,B,C for performance.
$device->isMobile $device->isMobile()
$device->getIsMobile()
Determines if the device is a mobile device, returns boolean.
$device->isTablet $device->isTablet()
$device->getIsTablet()
Determines if the device is a tablet device, returns boolean.
$device->isDesktop $device->isDesktop()
$device->getIsDesktop()
Determines if the device is a desktop computer, returns boolean.
$device->isBot $device->isBot()
$device->getIsBot()
Determines if the device is a crawler / bot, returns boolean
$device->getType() Returns the type of device eg. mobile, tablet, etc.

User Agent

Using the Facade:

$userAgent = Agent::userAgent();

Using the Service:

use Soumen\Agent\Services\UserAgent;

$userAgent = UserAgent::get();

No specific properties for this service.

Header

Using the Facade:

$header = Agent::header();

Using the Service:

use Soumen\Agent\Service\Header;

$header = Header::get();

// or

$header = Header::getDetails();

Available Properties

Properties Methods Parameters
$header->host,
$header->host,
$header->host(),
$header->host(),
$header->getHost()
host
$header->userAgent,
$header->user_agent,
$header->userAgent(),
$header->user_agent(),
$header->getUserAgent()
user-agent
$header->accept,
$header->accept,
$header->accept(),
$header->accept(),
$header->getAccept()
accept
$header->acceptLanguage,
$header->accept_language,
$header->acceptLanguage(),
$header->accept_language(),
$header->getAcceptLanguage()
accept-language
$header->acceptEncoding,
$header->accept_encoding,
$header->acceptEncoding(),
$header->accept_encoding(),
$header->getAcceptEncoding()
accept-encoding
$header->cookie,
$header->cookie,
$header->cookie(),
$header->cookie(),
$header->getCookie()
cookie
$header->connection,
$header->connection,
$header->connection(),
$header->connection(),
$header->getConnection()
connection
$header->upgradeInsecureRequests,
$header->upgrade_insecure_requests,
$header->upgradeInsecureRequests(),
$header->upgrade_insecure_requests(),
$header->getUpgradeInsecureRequests()
upgrade-insecure-requests
$header->cacheControl,
$header->cache_control,
$header->cacheControl(),
$header->cache_control(),
$header->getCacheControl()
cache-control

You can use the property names as the string in the parameter column:

Header::get('user-agent');
Agent::header('user-agent');

Server

Using the Facade:

$server = Agent::server();

Using the Service:

use Soumen\Agent\Services\Server;

$server = Server::get();

// or

$server = Server::getDetails();

Available Properties

Properties Methods
$server->DOCUMENT_ROOT,
$server->documentRoot
$server->DOCUMENT_ROOT(),
$server->documentRoot(),
$server->getDocumentRoot()
Returns the value of PHP's $_SERVER['DOCUMENT_ROOT'] superglobal.
$server->REMOTE_ADDR,
$server->remoteAddr
$server->REMOTE_ADDR(),
$server->remoteAddr(),
$server->getRemoteAddr()
Returns the value of PHP's $_SERVER['REMOTE_ADDR'] superglobal.
$server->REMOTE_PORT,
$server->remotePort
$server->REMOTE_PORT(),
$server->remotePort(),
$server->getRemotePort()
Returns the value of PHP's $_SERVER['REMOTE_PORT'] superglobal.
$server->SERVER_SOFTWARE,
$server->serverSoftware
$server->SERVER_SOFTWARE(),
$server->serverSoftware(),
$server->getServerSoftware()
Returns the value of PHP's $_SERVER['SERVER_SOFTWARE'] superglobal.
$server->SERVER_PROTOCOL,
$server->serverProtocol
$server->SERVER_PROTOCOL(),
$server->serverProtocol(),
$server->getServerProtocol()
Returns the value of PHP's $_SERVER['SERVER_PROTOCOL'] superglobal.
$server->SERVER_NAME,
$server->serverName
$server->SERVER_NAME(),
$server->serverName(),
$server->getServerName()
Returns the value of PHP's $_SERVER['SERVER_NAME'] superglobal.
$server->SERVER_PORT,
$server->serverPort
$server->SERVER_PORT(),
$server->serverPort(),
$server->getServerPort()
Returns the value of PHP's $_SERVER['SERVER_PORT'] superglobal.
$server->REQUEST_URI,
$server->requestUri
$server->REQUEST_URI(),
$server->requestUri(),
$server->getRequestUri()
Returns the value of PHP's $_SERVER['REQUEST_URI'] superglobal.
$server->REQUEST_METHOD,
$server->requestMethod
$server->REQUEST_METHOD(),
$server->requestMethod(),
$server->getRequestMethod()
Returns the value of PHP's $_SERVER['REQUEST_METHOD'] superglobal.
$server->SCRIPT_NAME,
$server->scriptName
$server->SCRIPT_NAME(),
$server->scriptName(),
$server->getScriptName()
Returns the value of PHP's $_SERVER['SCRIPT_NAME'] superglobal.
$server->SCRIPT_FILENAME,
$server->scriptFilename
$server->SCRIPT_FILENAME(),
$server->scriptFilename(),
$server->getScriptFilename()
Returns the value of PHP's $_SERVER['SCRIPT_FILENAME'] superglobal.
$server->PATH_INFO,
$server->pathInfo
$server->PATH_INFO(),
$server->pathInfo(),
$server->getPathInfo()
Returns the value of PHP's $_SERVER['PATH_INFO'] superglobal.
$server->PHP_SELF,
$server->phpSelf
$server->PHP_SELF(),
$server->phpSelf(),
$server->getPhpSelf()
Returns the value of PHP's $_SERVER['PHP_SELF'] superglobal.
$server->HTTP_HOST,
$server->httpHost
$server->HTTP_HOST(),
$server->httpHost(),
$server->getHttpHost()
Returns the value of PHP's $_SERVER['HTTP_HOST'] superglobal.
$server->HTTP_USER_AGENT,
$server->httpUserAgent
$server->HTTP_USER_AGENT(),
$server->httpUserAgent(),
$server->getHttpUserAgent()
Returns the value of PHP's $_SERVER['HTTP_USER_AGENT'] superglobal.
$server->HTTP_ACCEPT,
$server->httpAccept
$server->HTTP_ACCEPT(),
$server->httpAccept(),
$server->getHttpAccept()
Returns the value of PHP's $_SERVER['HTTP_ACCEPT'] superglobal.
$server->HTTP_ACCEPT_LANGUAGE,
$server->httpAcceptLanguage
$server->HTTP_ACCEPT_LANGUAGE(),
$server->httpAcceptLanguage(),
$server->getHttpAcceptLanguage()
Returns the value of PHP's $_SERVER['HTTP_ACCEPT_LANGUAGE'] superglobal.
$server->HTTP_ACCEPT_ENCODING,
$server->httpAcceptEncoding
$server->HTTP_ACCEPT_ENCODING(),
$server->httpAcceptEncoding(),
$server->getHttpAcceptEncoding()
Returns the value of PHP's $_SERVER['HTTP_ACCEPT_ENCODING'] superglobal.
$server->HTTP_COOKIE,
$server->httpCookie
$server->HTTP_COOKIE(),
$server->httpCookie(),
$server->getHttpCookie()
Returns the value of PHP's $_SERVER['HTTP_COOKIE'] superglobal.
$server->HTTP_CONNECTION,
$server->httpConnection
$server->HTTP_CONNECTION(),
$server->httpConnection(),
$server->getHttpConnection()
Returns the value of PHP's $_SERVER['HTTP_CONNECTION'] superglobal.
$server->HTTP_UPGRADE_INSECURE_REQUESTS,
$server->httpUpgradeInsecureRequests
$server->HTTP_UPGRADE_INSECURE_REQUESTS(),
$server->httpUpgradeInsecureRequests(),
$server->getHttpUpgradeInsecureRequests()
Returns the value of PHP's $_SERVER['HTTP_UPGRADE_INSECURE_REQUESTS'] superglobal.
$server->HTTP_CACHE_CONTROL,
$server->httpCacheControl
$server->HTTP_CACHE_CONTROL(),
$server->httpCacheControl(),
$server->getHttpCacheControl()
Returns the value of PHP's $_SERVER['HTTP_CACHE_CONTROL'] superglobal.
$server->REQUEST_TIME_FLOAT,
$server->requestTimeFloat
$server->REQUEST_TIME_FLOAT(),
$server->requestTimeFloat(),
$server->getRequestTimeFloat()
Returns the value of PHP's $_SERVER['REQUEST_TIME_FLOAT'] superglobal.
$server->REQUEST_TIME,
$server->requestTime
$server->REQUEST_TIME(),
$server->requestTime(),
$server->getRequestTime()
Returns the value of PHP's $_SERVER['REQUEST_TIME'] superglobal.

Change log

Please see the changelog for more information on what has changed recently.

Contributing

Please see contributing.md for details and a todolist.

Security

If you discover any security related issues, please email me at soumendeyemail@gmail.com.

Credits

License

This package is released under the MIT License (MIT). Please see the license file for more information.