How to redirect website to desktop version?

My site has two versions: Desktop and Mobile.

When I access by mobile "www.meusite.com.br", I am redirected to the mobile version: "m.meusite.com.br".

To do this, I'm using this very cool project called Mobile Detect

So far so good.

The website in the Mobile version, has a button to access the Desktop version "switch to Desktop version". And that's where the problem comes from.

When I click the button to access the Desktop version, I'm redirected from the "m.meusite.com.br" to the "www.meusite.com.br". the page loads and the script of Mobile Detect again loads and I am redirected to the "m.meusite.com.br" again. That is, it loops.

To better exemplify:

- - - > "Switch to Desktop version" Button is clicked;
--- >The page is redirected to the " www.meusite.com.br"; --- >"Www" page is loaded; --- >How I'm on the mobile and I'm on the " www.", we Mobile Detect script again is loaded and back to the "m.meusite.com.br';

Website code version Desktop :

<?php
  require_once 'Mobile_Detect.php';
  $detect = new Mobile_Detect;
?>
<!DOCTYPE html>
<html>
 <head>
<meta charset="utf8">
  <?php if( $detect->isMobile() ) : ?>

    <script type="text/javascript">
         window.location.href = "http://m.meusite.com.br";  
    </script>

  <?php endif ?>

 </head>
<body style="padding: 0; margin: 0">

<div class="wrapper" style="height: 100vh; display: flex; justify-content: center; align-items: center; flex-direction: column">
  <h1>VERSÃO DESKTOP</h1>      
</div>
 </body>
</html>

Website code version Mobile :

<!DOCTYPE html>
<html>
<head></head>
<body style="padding: 0; margin: 0">
    <div class="wrapper" style="height: 100vh; display: flex; justify-content: center; align-items: center; flex-direction: column">
      <h1>VERSÃO MOBILE</h1>
      <?php    
          require_once "../Mobile_Detect.php";
          $detect = New Mobile_Detect;

           if( $detect->isMobile() && (!isset($_GET['force_desktop']) || $_GET['force_desktop'] == 'false')) : ?>
               <a href="http://www.meusite.com.br&force_desktop=true">REDIRECIONAR PARA DESKTOP</a>
            <?php endif; ?>
    </div>
</body>
</html>

Thinking of a logic of forcing the page to stay in the desktop version when I'm on the mobile, I'm using this code inside the button that redirect to the desktop:

 if( $detect->isMobile() && (!isset($_GET['force_desktop']) || $_GET['force_desktop'] == 'false')) : ?>
     <a href="http://www.meusite.com.br&force_desktop=true?">REDIRECIONAR PARA DESKTOP</a>
 <?php endif; ?>

However, it gives Page Not Found error. I believe I am erring on the parameter to pass in the URL.

How can I solve this? How can I force redirection to Desktop version when I'm on mobile?

Author: Zkk, 2016-10-21

1 answers

You can check if the value of your GET exists, if it exists the device check is not done.

<?php
  require_once 'Mobile_Detect.php';
  $detect = new Mobile_Detect;
?>
<!DOCTYPE html>
<html>
 <head>
<meta charset="utf8">

  <?php if( !isset($_GET['force_desktop']) && empty($_GET['force_desktop'])) : ?>

  <?php if( $detect->isMobile() ) : ?>

    <script type="text/javascript">
         window.location.href = "http://m.meusite.com.br";  
    </script>

  <?php endif ?>
  <?php endif ?>
  <?php var_dump($_GET); ?>
 </head>
<body style="padding: 0; margin: 0">

<div class="wrapper" style="height: 100vh; display: flex; justify-content: center; align-items: center; flex-direction: column">
  <h1>VERSÃO DESKTOP</h1>     
</div>
 </body>
</html>
 -1
Author: Talisson Ferreira, 2016-10-21 14:13:59