Quantcast
Channel: Technology – Workbox
Viewing all articles
Browse latest Browse all 35

Smartphone or Desktop? Script Shows the Right Website

$
0
0

Quick summary: A handy server-side script that determines user-agent so you can serve desktop, tablet or mobile versions of your site.

Download it here.

About the Script and User-Agent

One of the best ways to determine whether to serve a desktop or mobile (or tablet) version of your site is user-agent. A user-agent is a client app (like browsers, search bots, mobile apps, etc.) that is used to access websites.

When an application like your browser visits a website, it usually sends information about itself to the webserver. This information is a simple line of text (part of the HTTP request) that starts with “User-agent” or “User-Agent.” Typically, this line includes information such as app name/version, operating system and language.

A website can analyze the user-agent information because the value is stored in a server variable – $ SERVER (variables set by the web server or otherwise directly related to the execution environment of the current script).

$_SERVER['HTTP_USER_AGENT' ] - Contents of the User-Agent: header from the current request, if there is one. This is a string denoting the user agent which is accessing the page. A typical example is: Mozilla/4.5 [en] (X11; U; Linux 2.2.9 i586).

The script developed by Serban Ghita (more about the script below) analyzes this variable and figures out if it contains phrases or words that signify a specific browser/device. For example: iPhone, Nexus, HTC, Palm – those words mean that the user is holding a mobile device. Words like iPad, Kindle or models like GT-P6210 or GT-P6800 mean that it’s a tablet.

Based on this parsing we can serve the most appropriate version of the website to the user.

Nexus vs. Nexus?

Often the most challenging part is differentiating between a tablet and a smartphone. A prime example is the Nexus devices – some are smartphones, and some are tablets. Below you can see an image that illustrates how the user agent string may be used to tell a tablet from a smartphone.

user-agent-script-samples01a

 

 

 

 

 

 

 

 

A big shout out to the author of this great browser/device detection script – Serban Ghita. Here’s a link to his profile on github.com: https://github.com/serbanghita/Mobile-Detect/blob/master/README.md.

You can also help support maintaining and updating this awesome script like we did (help the developer pay for hosting and various expenses):

Click here to lend your support to: Funding the development of Mobile_Detect 3.0 and make a donation at www.pledgie.com !


Viewing all articles
Browse latest Browse all 35

Trending Articles