Repository navigation
V 1.2.0
Agent Detection | Discovery and Analysis of Connection Agent Information
The Agent Detection library offers a wide range of methods that allow you to discover and analyze connection agent information in your application.
⚙️ This library is compatible with Laravel versions 8.0 and above ⚙️
Table of Contents
- Installation
- Introduction
- Available Methods
- Get Agent
- Validate if it's a Mobile Connection
- Validate if it's a Desktop Connection
- Validate if it's an iPhone
- Validate if it's a Macintosh
- Validate if it's Linux
- Validate if it's Android
- Validate if it's Windows
- Validate if it's a Windows Phone
- Validate if it's an iPod
- Validate if it's an iPad
- Validate if it's an iMac
- Validate if it's a Tablet
- Validate if it's a Crawler (Robot)
- Validate if Agent Matches an Expression
- Validate if Agent Connects from a Specific IP or Port
- Get the Client Connection Operating System
- Get Browser Data
- Get Robot Data
- Creator
- License
Installation
To install the library, you can easily do so through Composer.
composer require rmunate/agent-detectionIntroduction
This simple library allows you to obtain specific details about the connection agent, which will enable you to enhance the user experience based on connection data. For example, if you detect that it's a mobile connection, you can invite the user to download the mobile application if your system has one. Similarly, you can offer a more personalized experience for Android or iPhone users, differentiate options, menus, and other elements between desktop and mobile device users.
This library also leverages the functionality of the package https://github.com/JayBizzle/Crawler-Detect, allowing you to determine if it's a bot, crawler, or spider. In summary, you have endless possibilities at your disposal.
Available Methods
Below are the available methods in the library. Make sure you have the latest version installed in your system to use any of the following methods or directives.
Get Agent
Agent::get() returns the current connection agent detected by the server.
use Rmunate\AgentDetection\Agent;
Agent::get();
//"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36"Validate if it's a Mobile Connection
You can perform this validation from controllers or directly from the view using the provided Blade directive.
use Rmunate\AgentDetection\Agent;
// Detecting the Current Agent
if (Agent::detect()->isMobile()) {
# It's a Mobile Connection...
}
// Assigning a Custom Agent
if (Agent::set("Mozilla/5.0...")->isMobile()) {
# It's a Mobile Connection...
} Blade Directive: This directive validates the in-use connection agent and, based on this, acts as a condition.
@ifAgentIsMobile
<h1>It's a Mobile Connection</h1>
@else
<h1>It's Not a Mobile Connection</h1>
@endIfAgentIsMobileValidate if it's a Desktop Connection
You can perform this validation from controllers or directly from the view using the provided Blade directive.
use Rmunate\AgentDetection\Agent;
// Detecting the Current Agent
if (Agent::detect()->isDesktop()) {
# It's a Desktop Connection...
}
// Assigning a Custom Agent
if (Agent::set("Mozilla/5.0...")->isDesktop()) {
# It's a Desktop Connection...
} Blade Directive: This directive validates the in-use connection agent and, based on this, acts as a condition.
@ifAgentIsDesktop
<h1>It's a Desktop Connection</h1>
@else
<h1>It's Not a Desktop Connection</h1>
@endIfAgentIsDesktopValidate if it's an iPhone
You can perform this validation from controllers or directly from the view using the provided Blade directive.
use Rmunate\AgentDetection\Agent;
// Detecting the Current Agent
if (Agent::detect()->isIPhone()) {
# It's an iPhone...
}
// Assigning a Custom Agent
if (Agent::set("Mozilla/5.0...")->isIPhone()) {
# It's an iPhone...
} Blade Directive: This directive validates the in-use connection agent and, based on this, acts as a condition.
@ifAgentIsIPhone
<h1>It's an iPhone</h1>
@else
<h1>It's Not an iPhone</h1>
@endIfAgentIsIPhoneValidate if it's a Macintosh
You can perform this validation from controllers or directly from the view using the provided Blade directive.
use Rmunate\AgentDetection\Agent;
// Detecting the Current Agent
if (Agent::detect()->isMacintosh()) {
# It's a Macintosh...
}
// Assigning a Custom Agent
if (Agent::set("Mozilla/5.0...")->isMacintosh()) {
# It's a Macintosh...
} Blade Directive: This directive validates
the in-use connection agent and, based on this, acts as a condition.
@ifAgentIsMacintosh
<h1>It's a Macintosh</h1>
@else
<h1>It's Not a Macintosh</h1>
@endIfAgentIsMacintoshValidate if it's Linux
You can perform this validation from controllers or directly from the view using the provided Blade directive.
use Rmunate\AgentDetection\Agent;
// Detecting the Current Agent
if (Agent::detect()->isLinux()) {
# It's Linux...
}
// Assigning a Custom Agent
if (Agent::set("Mozilla/5.0...")->isLinux()) {
# It's Linux...
} Blade Directive: This directive validates the in-use connection agent and, based on this, acts as a condition.
@ifAgentIsLinux
<h1>It's Linux</h1>
@else
<h1>It's Not Linux</h1>
@endIfAgentIsLinuxValidate if it's Android
You can perform this validation from controllers or directly from the view using the provided Blade directive.
use Rmunate\AgentDetection\Agent;
// Detecting the Current Agent
if (Agent::detect()->isAndroid()) {
# It's Android...
}
// Assigning a Custom Agent
if (Agent::set("Mozilla/5.0...")->isAndroid()) {
# It's Android...
} Blade Directive: This directive validates the in-use connection agent and, based on this, acts as a condition.
@ifAgentIsAndroid
<h1>It's Android</h1>
@else
<h1>It's Not Android</h1>
@endIfAgentIsAndroidValidate if it's Windows
You can perform this validation from controllers or directly from the view using the provided Blade directive.
use Rmunate\AgentDetection\Agent;
// Detecting the Current Agent
if (Agent::detect()->isWindows()) {
# It's Windows...
}
// Assigning a Custom Agent
if (Agent::set("Mozilla/5.0...")->isWindows()) {
# It's Windows...
} Blade Directive: This directive validates the in-use connection agent and, based on this, acts as a condition.
@ifAgentIsWindows
<h1>It's Windows</h1>
@else
<h1>It's Not Windows</h1>
@endIfAgentIsWindowsValidate if it's a Windows Phone
You can perform this validation from controllers or directly from the view using the provided Blade directive.
use Rmunate\AgentDetection\Agent;
// Detecting the Current Agent
if (Agent::detect()->isWindowsPhone()) {
# It's a Windows Phone...
}
// Assigning a Custom Agent
if (Agent::set("Mozilla/5.0...")->isWindowsPhone()) {
# It's a Windows Phone...
} Blade Directive: This directive validates the in-use connection agent and, based on this, acts as a condition.
@ifAgentIsWindowsPhone
<h1>It's a Windows Phone</h1>
@else
<h1>It's Not a Windows Phone</h1>
@endIfAgentIsWindowsPhoneValidate if it's an iPod
You can perform this validation from controllers or directly from the view using the provided Blade directive.
use Rmunate\AgentDetection\Agent;
// Detecting the Current Agent
if (Agent::detect()->isIpod()) {
# It's an iPod...
}
// Assigning a Custom Agent
if (Agent::set("Mozilla/5.0...")->isIpod()) {
# It's an iPod...
} Blade Directive: This directive validates the in-use connection agent and, based on this, acts as a condition.
@ifAgentIsIpod
<h1>It's an iPod</h1>
@else
<h1>It's Not an iPod</h1>
@endIfAgentIsIpodValidate if it's an iPad
You can perform this validation from controllers or directly from the view using the provided Blade directive.
use Rmunate\AgentDetection\Agent;
// Detecting the Current Agent
if (Agent::detect()->isIpad()) {
# It's an iPad...
}
// Assigning a Custom Agent
if (Agent::set("Mozilla/5.0...")->isIpad()) {
# It's an iPad...
} Blade Directive: This directive validates the in-use connection agent and, based on this, acts as a condition.
@ifAgentIsIpad
<h1>It's an iPad</h1>
@else
<h1>It's Not an iPad</h1>
@endIfAgentIsIpadValidate if it's an iMac
You can perform this validation from controllers or directly from the view using the provided Blade directive.
use Rmunate\AgentDetection\Agent;
// Detecting the Current Agent
if (Agent::detect()->isIMac()) {
# It's an iMac...
}
// Assigning a Custom Agent
if (Agent::set("Mozilla/5.0...")->isIMac()) {
# It's an iMac...
} Blade Directive: This directive validates the in-use connection agent and, based on this, acts as a condition.
@ifAgentIsIMac
<h1>It's an iMac</h1>
@else
<h1>It's Not an iMac</h1>
@endIfAgentIsIMacValidate if it's a Tablet
You can perform this validation from controllers or directly from the view using the provided Blade directive.
use Rmunate\AgentDetection\Agent;
// Detecting the Current Agent
if (Agent::detect()->isTablet()) {
# It's a Tablet...
}
// Assigning a Custom Agent
if (Agent::set("Mozilla/5.0...")->isTablet()) {
# It's a Tablet...
} Blade Directive: This directive validates the in-use connection agent and, based on this, acts as a condition.
@ifAgentIsTablet
<h1>It's a Tablet</h1>
@else
<h1>It's Not a Tablet</h1>
@endIfAgentIsTabletValidate if it's a Crawler (Robot)
You can perform this validation from controllers or directly from the view using the provided Blade directive.
You can use isCrawler() or isRobot()
use Rmunate\AgentDetection\Agent;
// Detecting the Current Agent
if (Agent::detect()->isCrawler()) { // Agent::detect()->isRobot()
# It's a Crawler (Robot)...
}
// Assigning a Custom Agent
if (Agent::set("User-Agent of Crawler")->isCrawler()) { // Agent::set("User-Agent of Crawler")->isRobot()
# It's a Crawler (Robot)...
} Blade Directive: This directive validates the in-use connection agent and, based on this, acts as a condition.
@ifAgentIsCrawler //@ifAgentIsRobot
<h1>It's a Crawler (Robot)</h1>
@else
<h1>It's Not a Crawler (Robot)</h1>
@endIfAgentIsCrawler //@endIfAgentIsRobotValidate if the Agent Matches an Expression
You can perform this custom validation using the match($expression) method from controllers or directly from the view using the provided Blade directive.
use Rmunate\AgentDetection\Agent;
// Detecting the Current Agent
if (Agent::detect()->match('Firefox')) {
# The Agent contains the expression...
}
// Assigning a Custom Agent
if (Agent::set("Custom User-Agent")->match('Firefox')) {
# The Agent contains the expression...
} Blade Directive: This directive validates the in-use connection agent contains a custom expression, and based on this, acts as a condition.
@ifAgentMatch('Firefox')
<h1>The Agent contains the expression</h1>
@else
<h1>The Agent does not contain the expression</h1>
@endIfAgentMatchValidate if the Agent Connects from a Specific IP or Port
To obtain the source IP address of the request, you can use the following PHP code:
use Rmunate\AgentDetection\Agent;
$ip = Agent::remoteAddress(); //'127.0.0.1'
$port = Agent::remotePort(); //55660Blade Directive: This directive validates if the request comes from a specific IP.
@ifAgentRemoteIp('127.0.0.1')
<h1>Authorized IP</h1>
@else
<h1>Unauthorized IP</h1>
@endIfAgentRemoteIpBlade Directive: This directive validates if the request comes from a specific port.
@ifAgentRemotePort('55660')
<h1>Authorized Port</h1>
@else
<h1>Unauthorized Port</h1>
@endIfAgentRemotePortGet the Client Connection's Operating System
To obtain the name of the client connection's operating system, you can use the following method:
use Rmunate\AgentDetection\Agent;
$os = Agent::detect()->clientOS(); // Mac | Windows...
// It will return the name of the operating systemGet Browser Data
To obtain browser data from the client connection, you can use the following PHP method:
use Rmunate\Server\Agent;
$browser = Agent::detect()->browser();This code will return an array with information about the browser, including the browser name, version, and platform. For example:
array:3 [▼
"name" => "Apple Safari"
"version" => "537.36"
"platform" => "Macintosh"
]Get Robot Data
To obtain data about any robot that may exist within the client connection, you can use the following PHP method:
use Rmunate\Server\Agent;
//$robotData = $agente = Agent::intersect()->getIfIsCrawler();
//null
$robotData = $agente = Agent::set('Mozilla/5.0 (compatible; Sosospider/2.0; +http://help.soso.com/webspider.htm)')->getIfIsCrawler();
// "Sosospider"Creator
- 🇨🇴 Raúl Mauricio Uñate Castro
- Email: raulmauriciounate@gmail.com
License
This project is under the MIT License.
🌟 Support My Projects! 🚀
Make whatever contributions you see fit; the code is entirely yours. Together, we can do amazing things and improve the world of development. Your support is invaluable. ✨
If you have ideas, suggestions, or just want to collaborate, we're open to it all! Join our community and be part of our journey to success! 🌐👩💻👨💻
