How to redirect main website to mobile and back?

Good evening!
Faced such problem: there is main site, recently made a mobile version on a separate subdomain. The developer added a script, which determines the type of device, and people have spread (or not spread to the mobile version). All anything, but if he suddenly need to upgrade to the full version of the site? How can it be? 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 was a problem on phones and says that the page failed to load because too many redirects. Please tell me how can I solve this problem? Or another solution?

Thanks in advance

UPD1: I do think that the algorithm should be the following:
1. Check whether there is a cook:
1.1 Yes:
1.1.1 mobile redirect to a mobile page
1.1.2 "desktop" redirect to a desktop page
1.2 no:
1.2.0-defined device type:
1.2.1 cell - set cookie 'mobile', redirected on the mobile page
1.2.2 desktop set cook "desktop", the desktop redirected on the page

Then, in this case, you can manually put your desired cookie to mobile telephones to go on the desktop, and from desktop - to mobile. The question of how to implement it using the magic of php?

27c229196dcb4fb189cd1ba03307c855.png
July 8th 19 at 16:16
2 answers
July 8th 19 at 16:18
You this code in a larger version of the site built?
Then, when you switch from the mobile version just add the argument ver=full
And there with the cookie is something. When it is necessary, but not checked.
and how to fix? - Agustin_Schaefer commented on July 8th 19 at 16:21
July 8th 19 at 16:20
header('Location: http://m.site.ru/');try it. Can help.

Find more questions by tags JavaScriptMobile developmentPHP