简体   繁体   English

手动重定向移动桌面版本

[英]Redirecting mobile-desktop versions manually

I have this code in my homepage 我的主页中有此代码

<?php
 if(isset($_SESSION['mobile'])){
   if($_SESSION['mobile']==1){
     echo '
         <script>
           if( /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent) ) {
             window.location = "mobile/index.php";
           }
         </script>';
   }
 }
 else{
   $_SESSION['mobile']=1;
   echo '
         <script>
           if( /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent) ) {
             window.location = "mobile/index.php";
           }
         </script>';
 }
?>

and on the mobile version of my website, to get back to the destop version I inserted a link to this almost empty "redirect.php" page 在我的网站的移动版本上,要返回到停止版本,我插入了指向此几乎为空的“ redirect.php”页面的链接

<?php 
  $_SESSION['mobile']=0; 
  header("location: ../index.php");
  exit;
?>

but I am not getting the desired behavior: once I enter the website with my mobile, I am correctly redirected to the mobile version, but once I click on the link to get the desktop version, I loop back into the mobile version. 但是我并没有得到期望的行为:使用手机进入网站后,我正确地重定向到了手机版本,但是一旦单击链接以获取桌面版本,我便回到了手机版本。 What am I missing? 我想念什么?

Thanks! 谢谢! :) :)

Remember to call session_start() function before using sessions. 记住在使用会话之前先调用session_start()函数。

Better use this to reduce code: 更好地使用它来减少代码:

<?php session_start();

if (isset($_GET["desktop"])) {

    // DESKTOP
    $_SESSION["mobile"] = 0;
    ...

} else {

    // MOBILE
    if (!isset($_SESSION["mobile"])) { $_SESSION["mobile"] = 1; }
    if ($_SESSION["mobile"] == 1){ echo '<script>...</script>'; }
    ...

}
?>

And link to the homepage with ?desktop=1 to switch to the desktop version. 并使用?desktop=1链接到主页以切换到桌面版本。

Based on your current code - You should have a separate session var that tell you that the user has manually reqeusted control. 根据您当前的代码-您应该有一个单独的会话变量,该变量告诉您​​用户已手动请求控件。

<?php
if(!$_SESSION['manuall_overide'])
 if(isset($_SESSION['mobile'])){
   if($_SESSION['mobile']==1){
     echo '
         <script>
           if( /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent) ) {
             window.location = "mobile/index.php";
           }
         </script>';
   }
 }
 else{
   $_SESSION['mobile']=1;
   echo '
         <script>
           if( /Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent) ) {
             window.location = "mobile/index.php";
           }
         </script>';
 }
}
?>

<?php 
  $_SESSION['manuall_overide']=1; 
  header("location: ../index.php");
  exit;
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将移动用户重定向到桌面版本 - Redirecting a mobile user to a desktop version 通过javascript / php重定向移动/桌面用户 - Redirecting mobile/desktop users through javascript/php 从移动设备共享时,移动博客网站不会重定向到桌面 - Mobile blogger site not redirecting to desktop when shared from mobile 如何为移动版和桌面版发布不同的 React 捆绑包? - How to ship different React bundles for Mobile and Desktop versions? 书签:对于移动浏览器,重定向到m。*。*;对于桌面浏览器,重定向到www。*。* - Bookmarklet: Redirecting to m.*.* for Mobile Browsers & www.*.* for Desktop Browsers 使用Javascript重定向到台式机/移动版的相应页面 - Redirecting to respective page of desktop/mobile version using Javascript 将普通的桌面菜单转换为选择选项菜单(对于移动版本) - Transform a normal desktop menu to a select option menu ( for mobile versions) 如何在移动网站上创建“桌面版本”链接而不将其重定向到移动网站 - How to create a “Desktop Version” link on mobile website without it redirecting to the mobile site 重定向桌面设备上的所有用户 - Redirecting all users on desktop devices 重定向到安全移动站点 - Redirecting to Secure Mobile Site
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM