Jump to content

Redirecting between mobile and dekstop versions


Recommended Posts

So i have a problem about redirectings between versions. I wrote a code which actually works well with mobile version on mobiles it goes to mobile version but it somehow doesnt work on desktop version> maybe anyone knows where the problem might be.

 if($this->mobiledetect->isMobile() == true){
         $current_url = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
                   if(strpos($current_url, '/m/') !== false)
                   {                      
                       redirect($current_url);
                   }
                   else {
                       $current_url = "http://" . $_SERVER['HTTP_HOST'] . '/m' . $_SERVER['REQUEST_URI'];
                       redirect($current_url);
                   }                   
            }
            else{
            $current_url = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
            if(strpos($current_url, '/m/') !== false){
                 $current_url = explode('/m/' , $current_url);
                    $current_url = $current_url[0] . "/" . $current_url[1] ;
                    redirect($current_url);  
            }
            else{  
                redirect($current_url);
            }
         }

If i leave the code with the last redirect  web doesnt load and says too many redirects and if i delete last redirect, web still goes on mobile version if i add /m/ in url it doesnt delete the m

Why would you ever redirect to the current URL? All that does is start an infinite loop.

 

The layout of your URLs is also odd. So if there's an /m/ anywhere in the path, that's automatically a mobile URL? What if the “m” has an entirely different meaning?

 

A far more sane approach would be to set up a mobile (sub)domain and do a simple check:

  • A mobile device accessing the desktop domain is redirected to the mobile domain
  • A desktop UA accessing the mobile domain is redirected to the desktop domain

In every other case, nothing happens.

As said, that code is just plain wrong. And I too prefer the dedicated subdomain method rather than a URL prefix.

 

That code also doesn't provide any way for a user to give their preference. Maybe they want to see the full site on their phone? Maybe you want to test out the mobile site on your desktop?

Nicest method is with a cookie and putting something in the URL to switch between versions.

$mobile = (isset($_COOKIE["mobile"]) ? (bool)$_COOKIE["mobile"] : $this->mobiledetect->isMobile());
$mobileurl = (strncmp($_SERVER["REQUEST_URI"], "/m/", 3) == 0);

// use a ?sticky query string parameter to force the desktop/mobile site with a cookie
if (isset($_GET["sticky"])) {
	// awkwardness to copy the session cookie parameters
	$params = session_get_cookie_params();
	$params["lifetime"] || $params["lifetime"] = 0;
	$params["path"] || $params["path"] = "/";
	$params["domain"] || $params["domain"] = $_SERVER["HTTP_HOST"];
	setcookie("mobile", ($mobileurl ? "1" : "0"), $params["lifetime"], $params["path"], $params["domain"]);

	// redirect to the url without the ?sticky
	$query = array_diff_key($_GET, array("sticky" => 0));
	$url = "http://" . $_SERVER["HTTP_HOST"] . strtok($_SERVER["REQUEST_URI"], "?") . ($query ? "?" . http_build_query($query) : "");
	redirect($url);
}

// mobile device on a non-mobile page
else if ($mobile && !$mobileurl) {
	$url = "http://" . $_SERVER["HTTP_HOST"] . "/m" . $_SERVER["REQUEST_URI"];
	redirect($url);
}

// non-mobile device on a mobile page
else if (!$mobile && $mobileurl) {
	$url = "http://" . $_SERVER["HTTP_HOST"] . substr($_SERVER["REQUEST_URI"], 2); // remove leading /m
	redirect($url);
}
then

<a href="http://www.example.com/path/to/page">Desktop version that redirects to mobile if (a) a mobile device and no cookie or (b) mobile=1 cookie</a>
<a href="http://www.example.com/m/path/to/page">Mobile version that redirects to desktop if (a) a non-mobile device and no cookie or (b) mobile=0 cookie</a>
<a href="http://www.example.com/path/to/page?sticky">Desktop version and sets a mobile=0 cookie</a>
<a href="http://www.example.com/m/path/to/page?sticky">Mobile version and sets a mobile=1 cookie</a>

Amm  because its not my webpage, i am just editing it a bit. So the problem was that /m version had a different controller. And i was doing all the checking in dekstop version so if i manually entered /m it went to a different controller which didn't have the checking for mobile device. So your code works perfectly now THANK YOU A LOT :)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.