Introduction
When you're building a web application, there may be times when you want to find out some information about your user's browser and operating system. In this Quickfire post, we're going to take a look at when you might want to do this. We're then going to take a look at how to use the Agent (jenssegers/agent) package to do this in your own Laravel application.
Use Cases
Let's take a look at some use cases where you might want to find out either the browser or operating system that your users are using. Each of these are situations that I've personally encountered when working on projects in the past:
1. Analytics
One of the most obvious use cases that you'll probably think of will be analytics. Of course, your project could use something like Google Analytics, but there might be times when you're not able to use it. This could be down to something such as having agreement in a contract with a client that prohibits you from sending data to third-party services. So, if want to be able to find out what types of browsers and devices your website's users are using, you might want to add this functionality yourself. This can be particularly useful for if you're trying to assess whether to continue supporting specific browsers (such as Internet Explorer).
2. Blocking legacy browsers
You might have a project that is intended to only work with modern browsers, such as Chrome and Firefox, and deny access to anyone using legacy browsers. To do this, you could potentially add a piece of middleware on your web routes that checks the user's browser, and if they are using something like Internet Explorer, they could be redirected to a "browser not supported" page.
3. Redirecting to an OS-specific downloads page
You might want to target specific operating systems or browsers and offer content that is unique to each one. For example, if your website is being used to advertise software that can be downloaded on different operating systems, you might want to detect what the visitor is using and redirect them to the downloads page that's related to their OS.
I'm not sure on the technicalities of how they detect the OS or make the redirect, but Slack offer this style of OS-specific redirecting if you go to their downloads page. As an example, if I go to https://slack.com/intl/en-gb/downloads/, I'll be redirected to the MacOS-specific download page at https://slack.com/intl/en-gb/downloads/mac?geocode=en-gb.
4. Showing logged-in devices
In your application, you might want to offer a section on the user's profile page where they can see all the devices that they're logged in using. If this is the case, you'll likely want to show them things such as their location, and the device and browser they were using.
5. Detecting bots
You might want to be able to detect bots and execute a different set of code if they are/aren't a bot. For example, on my site's freebies page, whenever a real user downloads an image, I increment the download counter. However, if the file is downloaded by a bot scraping or crawling the site, I don't increment the download counter. By doing this, it means that the number of downloads is more realistic and not unfairly inflated by bots.
Getting the Device, Browser and Operating System
Installing the Package
To get started with using the jenseggers/agent package, we'll need to install if first using the following command:
1composer require jenssegers/agent
Getting the Device
Now, let's say that in a controller, we want to find out what device was used to make the request. To do this we could write something like this:
1use Jenssegers\Agent\Facades\Agent; 2 3class UserController extends Controller 4{ 5 public function index() 6 { 7 $device = Agent::device(); 8 9 // ...10 }11}
For my particular use case, when I make a request to the controller, $device
is will be equal to Macintosh
because I'm using a Macbook.
As well as the device()
method, the package also provides other methods that you can use to find out if the user's device is a desktop, tablet or phone. You can call these methods on the Agent
facade like so:
Check if the request was made via a desktop device:
1Agent::isDesktop();
Check if the request was made via a tablet:
1Agent::isTablet();
Check if the request was made via a phone:
1Agent::isPhone();
Getting the Browser
The pacakge also provides a browser()
method that can be used to find out what browser the request was made from. This method can be used like so:
1$browser = Agent::browser();
In my particular use case, when I make the request, $browser
is equal to Firefox
.
You can also find out the browser version by passing the result of Agent::browser()
to the version()
method like so:
1$browser = Agent::browser();2$version = Agent::version($browser);
For me, $version
would be equal to 93.0
.
Getting the Operating System
The package also provides a platform()
method that you can use for finding the operating system that the request was made from. You can use it like so:
1$platform = Agent::platform();
For me, $platform
would be equal to OS X
.
In a similar way to how you could get the version number for the browser, you can also use the version()
method for getting the version of the operating system like so:
1$platform = Agent::platform();2$version = Agent::version($platform);
For me, $version
would be equal to 10.15
.
Detecting Bots
As I mentioned earlier in the article, I use bot detection to prevent my freebies from being download by bots and crawlers and incrementing the counter. To do this, I make use of the isRobot()
method that's provided by the package. You can use it like so:
1Agent::isRobot();
Conclusion
Hopefully, this post should have given you a brief overview of how to get the user's device type, browser and operating system information in your Laravel application. It should have also given you some examples of when and why you might want to do this.
If this post helped you out, I'd love to hear about it. Likewise, if you have any feedback to improve this post, I'd also love to hear that too.
If you're interested in getting updated each time I publish a new post, feel free to sign up for my newsletter below.
Keep on building awesome stuff! 🚀