Redirect website to mobile site and from mobile to desktop?

跟風遠走 提交于 2019-12-24 03:36:11

问题


How can I handle the situation if the user wants from mobile to go to desktop site?

I am using this library for redirect to mobile, and how to create a redirect. The code below is that I am using mobile_detect.php library:

require_once 'controller/Mobile_Detect.php';
$detect = new Mobile_Detect;

// Any mobile device (phones or tablets).
if ( !$detect->isMobile() ) {

   //the link to mobile site

}

But how to handle vice versa?

Thanks for helping


回答1:


<a href="self.php?desktop=1">Switch to desktop</a>

<?php
if(!isset($_SESSION['desktop'])) {
    $_SESSION['desktop'] = false;
}

if(isset($_GET['desktop']) && $_GET['desktop'] == 1) {
    $_SESSION['desktop'] = true;
}

if(!$_SESSION['desktop']) {
    if ( !$detect->isMobile() ) {

       //the link to mobile site

    }

}

On your mobile site you give the opportunity to people to switch to desktop (the page where the redirect is done) with passing a get parameter of desktop=1. Normally, if the parameter is not passed, and if the session is not set, it's false. On false value of $_SESSION['desktop'], you continue your script so it redirects to mobile. But once the param is passed, it changes the session to true, and you block will not be executed, so the normal (desktop) content of the site will be visible




回答2:


i use this

put this into your main site (non mobile)

    @include("Mobile_Detect.php");

$detect = new Mobile_Detect();

if ($detect->isMobile() && isset($_COOKIE['mobile']))

{

$detect = "false";

}

elseif ($detect->isMobile())

{
header("Location:http://whaterver.com");
}

then in your mobile app page put

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

Then you can link back to the full site with a regular link because the "full site" will check for the cookie and wont redirect if its set. Which it will be because they mobile app page has set it.



来源:https://stackoverflow.com/questions/22262036/redirect-website-to-mobile-site-and-from-mobile-to-desktop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!