Welcome Guest, Not a member yet? Register   Sign In
How to differentiate app/browser requests?
#1

I've established my backend API using Codeigniter 3, which is utilized by both browsers and an app built with Apache Cordova. I'm seeking guidance on identifying whether an API request originates from the mobile app or a browser, regardless of whether it's accessed via a mobile or desktop platform. How can I achieve this distinction?
Reply
#2

Hello, to distinguish whether an API request originates from a mobile app or a browser, you can utilize the User-Agent header in the HTTP request. The User-Agent header typically contains information about the client making the request, including the device, operating system, and browser.

Here's how you can achieve this in CodeIgniter 3:

1. Accessing the User-Agent Header:
In your CodeIgniter controller, you can access the User-Agent header using the following code:


Code:
php
Copy
$userAgent = $this->input->user_agent();
```


2. Identifying Mobile Apps:
Mobile apps often send custom User-Agent headers that can be used to identify their origin. You can check for specific patterns or keywords in the User-Agent string to differentiate mobile apps from browsers. For example, if your Cordova app sets a custom User-Agent header, you can look for that specific value.


Code:
php
Copy
$isMobileApp = strpos($userAgent, 'Your-Mobile-App-Identifier') !== false;
```

Replace `'Your-Mobile-App-Identifier'` with the actual value you expect in the User-Agent header of your mobile app.


3. Distinguishing Browsers:
For requests originating from browsers, you can assume that they are not from your mobile app. You can use the inverse condition to identify requests from browsers:


Code:
php
Copy
$isBrowser = !$isMobileApp;
```


This condition will be true if the request is not identified as coming from your mobile app.
By implementing these steps, you can differentiate API requests originating from your mobile app from those coming from browsers, regardless of the platform (mobile or desktop) used to access the API.
Reply
#3

This may help:

How To Detect whether API request is coming from mobile app or web Using Spring boot
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB