User Agent Class Concept in CodeIgniter 4 Tutorial

Share this Article
Reading Time: 4 minutes

User Agent Class is used to capture the user device identity like browser used, operating system used, mobile type etc. In CodeIgniter 4, we have inbuilt class for this. The User Agent class is always available directly from the current IncomingRequest instance. We will see in this article.

By default there are several user agents configured inside codeigniter 4 application.

Inside this article we will see the complete concept of User Agent Class concept in CodeIgniter 4.

Learn More –

Let’s get started.

CodeIgniter 4 Installation

To create a CodeIgniter 4 setup run this given command into your shell or terminal. Please make sure composer should be installed.

$ composer create-project codeigniter4/appstarter codeigniter-4

Assuming you have successfully installed application into your local system.

What is User Agent Class?

The User Agent Class provides functions that help identify information about the browser, mobile device, or robot visiting your site.

How to User Agent Class?

The User Agent class is always available directly from the current IncomingRequest instance. By default, you will have a request instance in your controller that you can retrieve the User Agent class from:

$agent = $this->request->getUserAgent();

User Agent Definition & Usage

Open UserAgents.php from /app/Config folder.

Inside this class file, we can find several type pre defined user agents. Also if we want to add in a custom way, then also we can add into this class file.

Let’s see How to use it in application?

$ php spark make:controller Site --suffix

It will create a file called SiteController.php inside /app/Controllers


namespace App\Controllers;

class SiteController extends BaseController
	public function checkAgent()
		$agent = $this->request->getUserAgent();

		if ($agent->isBrowser()) {
			$currentAgent = $agent->getBrowser() . ' ' . $agent->getVersion();
		} elseif ($agent->isRobot()) {
			$currentAgent = $this->agent->robot();
		} elseif ($agent->isMobile()) {
			$currentAgent = $agent->getMobile();
		} else {
			$currentAgent = 'Unidentified User Agent';

		echo $currentAgent;

	public function checkBrowser()
		$agent = $this->request->getUserAgent();

		if ($agent->isBrowser('Safari')) {

			echo 'You are using Safari.';
		} elseif ($agent->isBrowser()) {

			echo 'You are using a browser.';

	public function checkMobileType()
		$agent = $this->request->getUserAgent();

		if ($agent->isMobile('iphone')) {
			// .. code here
		} elseif ($agent->isMobile("blackberry")) {
			// .. code here
		} else {
			// .. code here

We hope this article helped you to learn User Agent Class Concept in CodeIgniter 4 Tutorial in a very detailed way.

Buy Me a Coffee

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.