Cld Posted September 8, 2016 Share Posted September 8, 2016 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 Quote Link to comment https://forums.phpfreaks.com/topic/302102-redirecting-between-mobile-and-dekstop-versions/ Share on other sites More sharing options...
Jacques1 Posted September 8, 2016 Share Posted September 8, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/302102-redirecting-between-mobile-and-dekstop-versions/#findComment-1537152 Share on other sites More sharing options...
requinix Posted September 8, 2016 Share Posted September 8, 2016 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> Quote Link to comment https://forums.phpfreaks.com/topic/302102-redirecting-between-mobile-and-dekstop-versions/#findComment-1537155 Share on other sites More sharing options...
Cld Posted September 8, 2016 Author Share Posted September 8, 2016 Thank you the great code requinix, but i still if i press a link with /m on my pc i still get the mobile version but still Thanks a lot because mobile version works perfectly now Quote Link to comment https://forums.phpfreaks.com/topic/302102-redirecting-between-mobile-and-dekstop-versions/#findComment-1537160 Share on other sites More sharing options...
Jacques1 Posted September 8, 2016 Share Posted September 8, 2016 Have you considered debugging your code? Does the mobile detection work at all? Which branch is executed, and why does the redirect not happen? You know, basic var_dump() debugging. Quote Link to comment https://forums.phpfreaks.com/topic/302102-redirecting-between-mobile-and-dekstop-versions/#findComment-1537162 Share on other sites More sharing options...
Cld Posted September 8, 2016 Author Share Posted September 8, 2016 Thank you for your answers i found the problem Quote Link to comment https://forums.phpfreaks.com/topic/302102-redirecting-between-mobile-and-dekstop-versions/#findComment-1537163 Share on other sites More sharing options...
requinix Posted September 8, 2016 Share Posted September 8, 2016 Thank you for your answers i found the problem Which was... what? Please share with the rest of the class. Quote Link to comment https://forums.phpfreaks.com/topic/302102-redirecting-between-mobile-and-dekstop-versions/#findComment-1537165 Share on other sites More sharing options...
Cld Posted September 8, 2016 Author Share Posted September 8, 2016 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 Quote Link to comment https://forums.phpfreaks.com/topic/302102-redirecting-between-mobile-and-dekstop-versions/#findComment-1537166 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.