S
S
Sergey2016-06-05 23:03:40
PHP
Sergey, 2016-06-05 23:03:40

How to redirect from the main site to mobile and vice versa?

Good evening!
Faced such a problem: there is a main site, they recently made a mobile version on a separate subdomain. The developer added a script where the device type was determined, and the person was transferred (or not transferred to the mobile version). Everything would be fine, but if he suddenly needs to switch to the full version of the site? So how to be here? I found this solution:

<?php

$iphone = strpos($_SERVER['HTTP_USER_AGENT'],"iPhone");
$android = strpos($_SERVER['HTTP_USER_AGENT'],"Android");
$palmpre = strpos($_SERVER['HTTP_USER_AGENT'],"webOS");
$berry = strpos($_SERVER['HTTP_USER_AGENT'],"BlackBerry");
$ipod = strpos($_SERVER['HTTP_USER_AGENT'],"iPod");
$mobile = strpos($_SERVER['HTTP_USER_AGENT'],"Mobile");
$symb = strpos($_SERVER['HTTP_USER_AGENT'],"Symbian");
$operam = strpos($_SERVER['HTTP_USER_AGENT'],"Opera M");
$htc = strpos($_SERVER['HTTP_USER_AGENT'],"HTC_");
$fennec = strpos($_SERVER['HTTP_USER_AGENT'],"Fennec/");
$winphone = strpos($_SERVER['HTTP_USER_AGENT'],"WindowsPhone");
$wp7 = strpos($_SERVER['HTTP_USER_AGENT'],"WP7");
$wp8 = strpos($_SERVER['HTTP_USER_AGENT'],"WP8");

if($ipad || $iphone || $ipod || $android || $symb || $operam || $htc || $winphone || $berry) {
    if(isset($_REQUEST['ver']) and $_REQUEST['ver'] === 'full') {
        if(empty($_COOKIE['ver']))
            setcookie('ver', 'full', time() + 86400, '/', '.site.ru');
    } else {
        header('Location: http://m.site.ru');
        exit;
    }
} 

?>

But there is a problem on phones and it says that the page could not be loaded due to too many redirects. Please tell me how can I solve this problem? Or another solution?
Thanks in advance
UPD1: I generally think that the algorithm should be as follows:
1. Check if there is a cookie:
1.1 yes:
1.1.1 "mobile" - redirect to the mobile page
1.1.2 "desktop" - redirect to the desktop page
1.2 no:
1.2.0 determine the device type:
1.2.1 mobile - set the "mobile" cookie, redirect to the mobile page
1.2.2 desktop - set the "desktop" cookie, redirect to the desktop page
Then in this case, you can manually set the desired cookie in order to go to the desktop from the mobile phone, and from the desktop to the mobile phone. The question is how to implement this with php magic?
27c229196dcb4fb189cd1ba03307c855.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey Shatokhin, 2016-06-05
@Sovigod

Have you built this code into the larger version of the site?
Then, when switching to it from the mobile version, just add the argument ver=full
And there something is not done with the cookie. It is set when necessary, but not checked.

K
Kuzma Shpagin, 2016-06-14
@demon72

header('Location: http://m.site.ru/');try. It might help.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question