2

As you know Facebook saves login sessions and we can see them in settings where we've logged in.

Now, they show IP address, Phone name (if logged in with phone) and App name in the phone and Browser name in case we use browser.

I want to store these info for my site using PHP and JavaScript. One way is to use inbuilt features of PHP on server side to save this info to my DB when user logs in successfully.

But there's a problem in it. The feature gives a string and I find it hard to extract the browser name, OS name and device name. Further, it doesn't show me app name in case logged in to my service with app.

So is there any better and recommended way to do it? Wouldn't it be better if I handle this client side? The client that makes request to server it self sends Device name, OS name and other info.

2nd option seems to me better but I'm not sure if it actually is.

  • Have you tried adding the missing info to the agent?. So far I know, we can overwrite the user-agent. At least I did in Android. I wonder if it's possible in JavaScript. Or sending custom headers? – Laiv Feb 5 '18 at 21:40
  • @Laiv that's what I need to know more about. Does overwrite here means defining my own formatted string along with everything I need OR getting real info from app/browser and putting it in user-agent header with standard format? Which approach is better and which is right? – Vikas Feb 6 '18 at 1:49
  • There's a little consensus between browsers providers about the UA format. Usually product/version (comments). I would try first adding data to the actual UA (if possible). More info here – Laiv Feb 6 '18 at 7:31
0

The information is always coming from the client. How you get the information to the server is a different question, of course.

User-Agent

When an HTTP client makes a request, it may include some information about itself in the User-Agent header. For example, browsers typically include some information about the browser name and version, and some basic OS information.

This is what the built-in feature of PHP is reading.1 Unfortunately, the format of these strings is not standardized. Parsing this information can be tricky, and should probably be left to a library.

In order to identify an app-based login, all you have to do is make the app's HTTP client provide a User-Agent header. You'll find how to do this in the relevant documentation. Obviously, you'll have to make sure you can parse it on the server.

1 For browser/OS information. The IP address is a separate issue.

Custom Implementation

Alternatively, you can define a more structured message format and include it with your login request (for example).

This gives you more control over what information is sent, and makes server-side parsing easy. The downside is that you have to implement and maintain additional logic, rather than relying on the standard HTTP headers.

I haven't done this myself, but it appears you can also make browsers send a custom user agent value. You might also consider looking for client side libraries that do this kind of thing. Of course, you have to make sure that your browser-version and your app-version are compatible.

Which option is better?

That's difficult to answer without knowing the whole system. However, if you expect requests to only/mostly come from standard browsers and your app, I would start with parsing the user-agent string.

If the information is really important to your users, the additional effort may be justified. The same applies, if you require information that is not typically sent by browsers, but that you can usually acquire via JavaScript.

In any case, note that the information is not reliable - any client can in principle send anything, regardless of the transmission mechanism you implement.

  • In case of Browsers, User-Agent header gives what I need. But problem is in case of App. It gives "okhttp someversion3.2" in this header. That I guess is not what I need. So would it be better to override this header while making request from app side as you mentioned? Secondly, what do you mean by custom user-agent string? Like defining my own format of string or getting real info from browser/app and then setting user-agent in standard format? – Vikas Feb 6 '18 at 1:46
  • Yes, set the user-agent string in your app to something useful. By custom user-agent string I mean that you can make a string with the required infos yourself (getting browser/OS infos using JS) and then send that as the user agent - i.e. define your own user-agent format instead of including the info with your other request data. However, I don't know how reliably that works, as I've never tried that myself. – doubleYou Feb 6 '18 at 8:47
0

In general you don't want to reinvent the wheel if you don't have to. You want to get information about user agents, which relates to the world wide web, not your particular application. So look for an off the shelf software component that can do this for you.

In PHP the standard way to install off the shelf components is Composer, and best website to find them is probably Packagist. If you search packagist for user agent parse you will find several popular PHP libaries that you can use to extract the browser name, OS name and device name from the user agent string.

Review the instructions for a few of these and use one of them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.