0

I've seen code that detects whether someone is using a mobile browser in Javascript (e.g. a jQuery script) and I've seen some that work in PHP (or other server-side language). But I've never seen a good explanation for whether one is a better choice than the other in all or any situations. Is there a reason why one is a better choice?

2
  • That depends on what you are doing with the result of that check. If you are redirecting, or including an entirely different css or js file, server-side is better. If you are making content panels wrap rather than float, then maybe js is better, or possibly media queries.
    – Kevin B
    Commented Oct 26, 2012 at 20:03
  • Use feature detection rather than UA sniffing. That way you're actually detecting the things you will be using.
    – Jasper
    Commented Oct 26, 2012 at 21:14

6 Answers 6

4

The typical answer: it depends on why you are doing the check...

From my standpoint, here is what I usually consider:

  • If you want to present the user a different experience (mobile, tablet, laptop, etc) based on browser, do it at the server.

  • If you want to present the same general experience, but need to account for browser compatibility issues, do it at the client.

It is also considered by some in the UX field to be "bad form" to present the user an empty page and fill it in dynamically. Instead, a preliminary page should be populated and content can be dynamically added or altered. If this is a concern for you, a combination of server side and client side may be necessary.

4
  • I do want the first one, and that makes sense.
    – Don Rhummy
    Commented Oct 26, 2012 at 20:08
  • Do you have any suggestions for a good PHP script? I found this: github.com/serbanghita/Mobile-Detect but don't know if there's a standard one that's accepted as being very accurate and reliable.
    – Don Rhummy
    Commented Oct 26, 2012 at 20:11
  • @DonRhummy I have always used browscap.ini in the past, but the author recently announced EOL'ing the project. I have not had the time to find a replacement.
    – jheddings
    Commented Oct 26, 2012 at 20:19
  • Note that browscap will introduce latency into your application as it's not a small thing to run. This latency is more than nothing, using a Windows Server 2008 R2 64-bit server with nothing else running browscap added noticeable latency.
    – Jasper
    Commented Oct 26, 2012 at 21:15
1

I'd say the better way would be on server side, because for Javascript you need to wait until the page is rendered, while on server side it happens before.

1

If you're trying to detect this in order to do decide what javascript features are available, you'll have greater accuracy, without any major loss of speed if you do this in JavaScript.

If you're going to completely change what sort of page is rendered, like a full website or a mobile website, you're better off doing this server side.

3
  • The second is what I'm looking for, so that makes sense.
    – Don Rhummy
    Commented Oct 26, 2012 at 20:08
  • I would avoid doing this detection by yourself, it gets very complicated, very quickly as you try to support a huge number of devices from various manufacturers. mobile.sniffer is one project in python that does this
    – JeffS
    Commented Oct 26, 2012 at 20:10
  • Do you have any suggestions for a good PHP script? I found this: github.com/serbanghita/Mobile-Detect but don't know if there's a standard one that's accepted as being very accurate and reliable.
    – Don Rhummy
    Commented Oct 26, 2012 at 20:11
0

As Ricebowl stated, never trust the client. However, I feel that it's almost always a problem if you do trust the client. If your application is worth writing, it's worth properly securing. If anyone can break it by writing their own client and passing data you don't expect, that's a bad thing. For that reason, you need to validate on the server.

2
  • Even validating on the server doesn't stop what you're suggesting. Anyone can spoof a client and the server would never be able to tell.
    – Don Rhummy
    Commented Oct 26, 2012 at 20:10
  • You can always hackproof server validations with logics. Depends on the way you code. Commented Oct 26, 2012 at 20:11
0

Is green better than red?

Everything has its benefits and drawbacks. For example, doing it server side is more reliable, doing it client-side means less work for the server.

In fact, the client may have JavaScript disabled (see the NoScript extension for Firefox, and ScriptNo for Chrome, that allows a smart user to only enable JS on sites where you actually need it - a nice side effect is that it also eliminates almost all ads these days, as they largely seem to rely on JS from third party domains now). So just using the User-Agent string is more reliable, but less flexible.

If you work JS-heavy, you might get away with a dumb server, i.e. you do not need slow PHP, but you can serve all your data with high-performance static serving, through the various CDNs etc. - but anything that requires JS will work less good with search spiders, and some users will likely just block it.

4
  • True. I have seen it mentioned that one possible issue with client-side is the browser might be missing features to even make the detection work (e.g. scripting turned off). Not sure if that's valid or not.
    – Don Rhummy
    Commented Oct 26, 2012 at 20:06
  • clientside detection is more reliable. The only detection you have serverside is the UA-String which can be easily faked. The more important point is how much impact the detection has on the rendering of your site.
    – Christoph
    Commented Oct 26, 2012 at 20:06
  • @Christoph I'm going to present completely different sites based on whether it's mobile or desktop, so that's the detection I'm looking for here. I will also have detection for IE vs Chrome, etc but that'll be handled client-side.
    – Don Rhummy
    Commented Oct 26, 2012 at 20:13
  • Then of course you have to go with the serverside detection because clientside in not feasible here for you would have to either load all versions or doing some ajax calls to fetch the correct content which would result in slow page loading.
    – Christoph
    Commented Oct 26, 2012 at 20:28
0

As a web developer, UX and UI programmer I figure if anyone wants to change their UA, it's fine. They get to deal with the incompatibilities. As for a mobile vs desktop I would recommend the light version of browscap.ini instead of searching for device check for ismobiledevice. The if statement will be true or false, then you can also check tablet also. In the if mobile clause check the istablet key in the associative array. You can use it for phone or tablet css.

1
  • Welcome to StackOverflow, Nathan! Please notice that the asker wanted to know if checking in general is preferable on client- or server-side. Your answer, though informative, does not actually address the question. Commented Apr 13, 2015 at 8:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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