Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Request desktop site option on mobile devices

Many Mobile devices like my android phone have a 'request desktop site' option. I'm in the process of building mobile websites and want to make that native feature work.

What is expected on our end as developers? Is there a GET request for example: leave_mobile=1 or is it the device itself changing the user-agent to trick the application thinking its just another desktop?

like image 269
Gilly Avatar asked Apr 25 '13 14:04

Gilly


People also ask

How do I get Amazon desktop site on my phone?

There is a 'Aa' sign in the top left of the search bar. If you click on that you can request desktop site.

How do I request a desktop site in Chrome on Iphone?

Once the website loads, tap and hold on the “Refresh” button next to the address bar. Step 2: A pop-up appears at the bottom of the screen, choose Request Desktop Site from the two options.


1 Answers

I've done some tests on my android devices and read out the user agents and seems like it changes. So What I did to make this native option work was to capture the initial user agent into a session and on each page request compare it with the one being sent. If the user agent is not the same, revalidate if it is a mobile device and if true overwrite session user-agent with new one. If however the new validation fails, it probably wants to find the desktop version so send a new header redirect.

$DesktopSite = 'www.example.com';
$UserAgent = $_SERVER['HTTP_USER_AGENT'];

if (!isset($_SESSION['use_mobile'])){
    $_SESSION['use_mobile'] = 1;
    $_SESSION['user_agent'] = $UserAgent;
} else if ($_SESSION['user_agent'] != $UserAgent){  // Check if user-agent has changed
    if(!preg_match( /*preg match from http://detectmobilebrowsers.com/ */){
        $_SESSION['use_mobile'] = 0;
        $_SESSION['user_agent'] = $UserAgent; // Overwrite old user-agent with new one.
        header("location: $DesktopSite"); // Send visitor to desktop website.
    } else {
        // Visitor still seems to be mobile.
        $_SESSION['use_mobile'] = 1;
        $_SESSION['user_agent'] = $UserAgent; // Overwrite old user-agent with new one.
    }
}

If you enter the mobile website on a desktop for the first time, it will not send you back to the desktop site because it will create the session first. This way you can check the mobile site on a desktop as well instead of being send back. If however you change your user-agent, you will be revalidated and redirected on fail.

Works perfectly. Hope this may help anyone in the future.

like image 167
Gilly Avatar answered Sep 17 '22 11:09

Gilly