3

I'm building a version of my company's website with the jQuery Mobile framework. While it'd be fairly easy to do a javascript redirect to get users of mobile devices to our mobile site, I don't want to stop people from being able to view the classic website either.

What's the best way to accomplish this?

Our normal website is at:

us.companyname.com/

Our mobile website will be at:

us.companyname.com/mobile

There are some limitations, as we don't hold the domain name, our UK counterparts do, so getting anything done at the companyname.com level takes some patience. Basically, if anyone from North America comes to companyname.com, they'll get automatically redirected to us.companyname.com.

I do have full access to our website (written primarily in PHP & Expression engine, before I was here), and I'm free to do whatever as long as I don't mess anything up.

Hosemeyer
  • 1,264
  • 4
  • 17
  • 29

2 Answers2

5

First, such things i would not do this with Javascript/Query, cause what is if the user has not activate js? It is better to do this server-side.

Something like this:

$mobile = array("IPHONE", "IPAD");
$flagMobileVersion = false;
if($_SESSION['version']!="mobile"){ //happens only at first visit
for($i=0;$i<=count($mobile)-1;$i++){
    if(!strrpos(strtoupper($_SERVER['HTTP_USER_AGENT']), $mobile[$i]))
    {
        $flagMobileVersion = true;
        break;
    }
}

if($flagMobileVersion) {
$_SESSION['version'] = "mobile";
Header("www.mydomain.net/mobile"); //on first Visit
}
Chugworth
  • 480
  • 2
  • 5
  • +1, working with a cookie or session variable is definitely the best way to go. I just realized that my solution could fall apart if the user clicks a link back to the home page, unless you set a more persistent way of checking whether or not the user wants the mobile version. – Michael Martin-Smucker Feb 07 '11 at 18:14
  • Thank you so much! While we didn't use your exact implementation, we did decide to go server-side with it. – Hosemeyer Feb 10 '11 at 19:30
0

Would something like this work:

All you'd have to do differently is wrap the window.location redirection code on the main page inside an if statement that makes sure the noredirect flag isn't set to true. The only challenge would be isolating the noredirect query string, but this Stack Overflow question might help.

Community
  • 1
  • 1
Michael Martin-Smucker
  • 11,927
  • 7
  • 31
  • 36