Home » Javascript » Up-to date and future proof way of detecting iOS in PHP (or Server side)

Up-to date and future proof way of detecting iOS in PHP (or Server side)

Posted by: admin November 1, 2017 Leave a comment

Questions:

I know similiar question have been asked quite often but many of them are outdated, and things changed quite a lot, therefore I’m asking the community again (in October 2017) how can we detect if the user is using and iOS device or not, and I highlight iOS, I don’t care about any other platforms.

Reason why I need detection

  • I’m displaying pictures on my website and the user can click on some of them to download the full high-res picture. On the server side I’m using X-SENDFILE to push the file to the user, which works nicely on everything except iOS which lacks the download function, therefore in iOS case I need to modify the headers and present the image as an image rather than an attachment so the user can tap on it and save it to the gallery manually.

I can use user-agent sniffing (like I did in the past):

$iPod    = stripos($_SERVER['HTTP_USER_AGENT'],"iPod");
$iPhone  = stripos($_SERVER['HTTP_USER_AGENT'],"iPhone");
$iPad    = stripos($_SERVER['HTTP_USER_AGENT'],"iPad");

but this won’t cut it for in-app browsers (like Facebook, Twitter, etc), yes you can keep adding rules to the list, but it will be a constant catch-up game and how do you actually really keep a track of all the possible 3rd party apps which changes the agent string.
I don’t want to keep going back and updating my page, it’s a bit ridiculous imho.

So my question to the community is there any better way to detect iOS?

Prefered solutions in order

  1. Detect from Server side
  2. Detect from client side (javascript)
  3. Use an external library (similar to https://github.com/serbanghita/Mobile-Detect, maybe even payed, which can be linked to the page rather than built in)

I’m not too keen about the 3rd option, because that again will rely on the library being constantly updated, what if the development stops, it’s another thing which I must keep track of and forgetting to do so can lead to functionality being broken in future.

Thank you.

If you do downvote the question, please at least give a comment on why?

Answers: