How to get user agent information in laravel 5

By : PHPErrorCode October 6, 2017 400 Views laravel

In this post we will get User IP all user ajent(computer, mobile and browsers) information using lravel 4 or 5 package. In the present age, it is compulsory for every website to get user ajent information.

Mobile_Detect or Computer_Detect is a lightweight package is able to identify the visitors browser and compter or mobile deveices almost perfectly by useing multiple well tested packages and services together Ajent.

User Agent is a PHP Computer/mobile user agent parser with support for Laravel 4 or 5 version, based on Mobile or Computer deveices. It provides both Computer support and additional functionality. This Laravel 4 and 5 package utilizes Mobile Detect PHP Class.

In this example i give you step by step get information of User Ajent.

Step 1:Installation

will to install package, you can open a terminal and type the following command

composer require jenssegers/agent

Step 2:Laravel Setting

You go to this path config/app.php

Then add the service provider below line.

Jenssegers\Agent\AgentServiceProvider::class,

Then add the Agent alias below line.

'Agent' => Jenssegers\Agent\Facades\Agent::class,

Step 3: Basic Usage

Start by creating an Agent instance or use the Agent Facade

use Jenssegers\Agent\Agent;
$agent = new Agent();

create a new controller like AjentController and add below code

isRobot())
    	{
    		$data['robot'] = $agent->robot();
    	}

    	$data['regexp'] = $agent->match('regexp');
    	$data['languages'] = $agent->languages();

    	$data['browser'] = $agent->browser();
		$data['browser_version'] = $agent->version($data['browser']);

		$data['platform'] = $agent->platform();
		$data['platform_version'] = $agent->version($data['platform']);

		$data['device'] = $agent->device();

		// Check if the user is using a desktop device.
		if ($agent->isDesktop()) 
		{
			$data['device_type'] = "Desktop";
		}

		// Check if the user is using a phone device.
		if ($agent->isPhone()) 
		{
			if($agent->isMobile()){$data['device_type'] = "Mobile";}
			if($agent->isTablet()){$data['device_type'] = "Tablet";}

			if($agent->isAndroidOS()){$data['device_os']= "Android";}
			if($agent->isNexus()){$data['device_os']= "Nexus";}
			if($agent->isSafari()){$data['device_os']= "Safari";}
			if($agent->is('iPhone')){$data['device_os']= "iPhone";}
		}

		// get my real
    	$data['my_ip'] = $_SERVER['REMOTE_ADDR']; 

    	dd($data);
    }
}
Output
user ajent information in laravel

Basic Details

  • Check if the user is a robot or Not.
  • $agent->isRobot();

 

  • if the user is a robot then Get the robot name else false
  • $robot = $agent->robot();

 

  • Search the user agent with a regular expression
  • $agent->match('regexp');

 

  • Get the browser's accept languages. Like: ['nl-nl', 'nl', 'en-us', 'en']
  • $languages = $agent->languages();

 

  • Get the browser name. Like (Chrome, IE, Safari, Firefox, ...) and version Like (60.0.3112.10)
  • $browser = $agent->browser();
  • $version = $agent->version($browser);

 

To get the platform name and version you can use:

  • $platform = $agent->platform();
  • $version = $agent->version($platform);

 

  • Get the device name, if mobile. Like (iPhone, Nexus, AsusTablet, ...)
  • $device = $agent->device();

 

  • Check if the user is using a desktop device or not.
  • $agent->isDesktop();

 

  • Check if the user is using a phone device or not.
  • $agent->isPhone()

 

  • Check for mobile device:
  • $agent->isMobile();
  • $agent->isTablet();
  •  

 

  • Magic method that does the same as the previous is() method:
  • $agent->isAndroidOS();
  • $agent->isNexus();
  • $agent->isSafari();

and

  • $agent->is('Windows');
  • $agent->is('Firefox');
  • $agent->is('iPhone');
  • $agent->is('OS X');