Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"VIEW FULL SITE" mobile site option

So I'm working on the mobile version of a site I'm doing, and so far, I'm pulling the mobile sites content from its main counterpart, the main site.

As I study some mobile sites out there, I notice a lot of em have a "view full site" link.

Now I plan on redirecting the mobile visitors via .js in the header tag on main site via a check for screen width etc...(not sure if its the best way but so far the easiest on my brain))(but suggestions also welcome) but something like this

if (screen.width<=XyZ||screen.height<=XyZ) //example iphone size lets say 320x480
window.location.replace("mobile site link here.")

Again I dont know if this is the best way but, on dummy tests, it works on iPhone, some friends Droids, and one Blackberry. But it works.

Anyways, so my question is, if i do this check on every page...how can I possible have a "view full site" option?

like image 868
somdow Avatar asked Dec 28 '22 21:12

somdow


2 Answers

Use PHP to detect mobile users through $_SERVER['HTTP_USER_AGENT']. JavaScript detection may not be reliable, because many mobile browsers do not support JS. A "View Full Site" will set a cookie to reject mobile site, which is detectable. Use cookies to keep track of your user's preferences.

In skeleton

<?php

if (isset($_COOKIE['nomobile'])) {
  $style = "normal";
} else {

if (preg_match('/iPhone|(...etc...)/', $_SERVER['HTTP_USER_AGENT'])) {
   $style = "mobile";
} else {
   $style = "normal";
}

}

For the "View Full Site" page:

<a href="fullsite.php">Full Site</a>

fullsite.php

<?php
   setcookie('nomobile', 'true');
   header('Location: index.php');
?>
like image 64
Ming-Tang Avatar answered Dec 30 '22 11:12

Ming-Tang


First, go to the following URL and download the mobile_detect.php file:

http://code.google.com/p/php-mobile-detect/

Next, follow the instructions on the page, and upload the mobile_detect.php to your root directory, Insert the following code on your index or home page:

    <?php
    @include("Mobile_Detect.php");
    $detect = new Mobile_Detect();
    if ($detect->isMobile() && isset($_COOKIE['mobile']))
    {
    $detect = "false";
    }
    elseif ($detect->isMobile())
    {
    header("Location:http://www.yourmobiledirectory.com");
    }
    ?>

You will notice that the above code is checking for a cookie called "mobile", this cookie is set when the mobile device is redirected to the mobile page. To set the cookie insert the following code on your mobile landing page:

    <?php
    setcookie("mobile","m", time()+3600, "/");
    ?>

View the full article at: http://www.squidoo.com/php-mobile-redirect

like image 31
Matt Avatar answered Dec 30 '22 10:12

Matt