PHP Laravel : How to get client browser/ Device?

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: PHP Laravel: Retrieving Client Browser Details in Your Application Introduction: In today's dynamic web development landscape, understanding your user's environment is crucial for delivering customized experiences. This article aims at addressing how you can retrieve client browser details from within a Laravel application to provide tailored features or enhance security measures. Body: 1. Getting the User-Agent Using Request Object: As mentioned in the code snippet, we can use the request object to fetch the header information, specifically the User-Agent. The `$device= $request->header('User-Agent');` line of code stores the User-Agent into a variable named $device. 2. Understanding the Output: In the provided example output `"Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36"`, we can easily identify the browser name and version - in this case, it is Google Chrome. This information helps us tailor specific functionalities or messages to different browsers. 3. Parsing the User-Agent String for More Detailed Information: Although we do get useful information from the standard User-Agent string, sometimes it's not enough. There are several libraries and packages available that can help you parse the header for more detailed browser data. You can install those packages using Composer or create your own custom parser to extract additional details. 4. Storing Client Browser Details in Your Laravel App: After receiving information about the client's browser, we can store it as a property within the application model, such as `User` or any other model that relates to the user. This allows you to retrieve this data from your database and make use of it later on. 5. Using Laravel Eloquent To Store Browser Details: Now, let's see how we can store browser details using Laravel's query builder or Eloquent ORM: ```php public function postUser(Request $request) { $user = new User(); $user->name = $request->Input(['name']); $device = $request->header('User-Agent'); // dd($device); Uncomment this line to view the device details in your log file. $browserDetails = parse_user_agent($device); $user->browser_details = json_encode($browserDetails); $user->save(); return redirect('userSavePage'); } ``` This code snippet demonstrates how to store the parsed browser details alongside the user's name within the database. We have utilized a custom `parse_user_agent()` function to extract more information from the User-Agent string, which can be easily created using the `strpos()`, `explode()`, or other methods, depending on your use case. Conclusion: In this article, we provided an in-depth understanding of how you can retrieve client browser details within a Laravel application using the request object and Laravel Eloquent. By understanding and storing this information, you can deliver tailored experiences or enhance security measures according to different browsers used by your users. Always ensure your code is secure and user data is handled properly.