The site has 2 templates for the desktop and for the mobile version. And depending on the device, you need to display the desired one. I do this, I determined 2 templates for the site, first mobile with the condition ( siteType=='pda' ), then normal. In the file init.php I do a check.

 $mobi = new \Olepro\Classes\Helpers\MobileDetect(); if($mobi->isMobile() && !isset($_COOKIE['siteType']) && !$_GET['type']) { LocalRedirect("/?type=pda"); exit; } if (isset($_GET['type'])) { switch ($_GET['type']) { case 'pda': setcookie('siteType', 'pda', time() + 3600 * 24 * 30, '/'); define('siteType', 'pda'); break; default: setcookie('siteType', 'original', time() + 3600 * 24 * 30, '/'); define('siteType', 'original'); } } else { $checkType = ''; if (isset($_COOKIE['siteType'])) $checkType = $_COOKIE['siteType']; switch ($checkType) { case 'pda': define('siteType', 'pda'); break; default: define('siteType', ''); } } 

But it works through time. Those. you go to the site from your mobile, the desktop version opens, you update the mobile version page appears. You come back, it can appear either a mobile or desktop version, as lucky. I suspect that this is some kind of a jamb with a cache, but I'm definitely not sure. Never worked with Bitrix

    2 answers 2

    The problem may be that there are no session variables in init.php. The session opens after init. The order of the page, see here: http://dev.1c-bitrix.ru/api_help/main/general/pageplan.php

    At least, it makes no sense to write in cookies. I advise you to use the event onBeforeProlog http://dev.1c-bitrix.ru/api_help/main/events/onbeforeprolog.php

    • Yes, I redid the function on onBeforeProlog. I indicate there directly the value of the SITE_TEMPLATE_ID constant for everyone and anyway when the template is displayed when mobile, when desktop - Oldpunk777
    • Well, here you need to check whether the constant is set. And then narrow the circle to understand what the problem is. - ka3a
    • And by the way, why such a complicated scheme? get settings, cookies, library .. If your MobileDetect correctly defines everything, why not use it only? - ka3a
    • What would be the opportunity to switch to any template and use it. Does the cache not save the page along with the template? - Oldpunk777
    1. I recommend to change a little the source code in itself.

        if (! isset ($ _ COOKIE ['siteType']) &&! $ _ GET ['type']) {
           $ mobi = new \ Olepro \ Classes \ Helpers \ MobileDetect ();
           if ($ mobi-> isMobile ()) {
               LocalRedirect ("/? Type = pda");
               exit;
           }
       }
       $ isMobile = false;
       if (isset ($ _ GET ['type'])) {
           setcookie (
               'siteType', 
               ($ isMobile = $ _GET ['type'] == 'pda')?  'pda': 'original', 
               time () + 3600 * 24 * 30, 
               '/'
           );
       } elseif (isset ($ _ COOKIE ['siteType'])) {
           $ isMobile = $ _COOKIE ['siteType'] == 'pda';
       }
       if ($ isMobile) {
           define ('siteType', 'pda');
       } else {
           define ('siteType', '');
       } 

    2. Try it. In your code there is an ambiguous define then in '' , then in 'original' , and the checks are somewhat confusing.

    3. If it does not work, then show another screenshot of the template settings for the site.

    • The problem is that even if instead of all this code just put define ('siteType', 'pda'); then the wrong template is displayed on some pages. c2n.me/3qBhAKl - Oldpunk777