简体   繁体   中英

how to direct desktop website away from mobile website on desktop with php

If a desktop user open mobile website, the code would redirect the user to desktop website.

<?php
// Apple iOS devices
    $iPhone = strpos($_SERVER['HTTP_USER_AGENT'],'iPhone');
    $iPod = strpos($_SERVER['HTTP_USER_AGENT'],'iPod');
    $iPad = strpos($_SERVER['HTTP_USER_AGENT'],'iPad');

// Android devices
    $Android = strpos($_SERVER['HTTP_USER_AGENT'],'Android');

// All mobile devices
    $Mobile = strpos($_SERVER['HTTP_USER_AGENT'],'Mobile');

// BlackBerry devices
    $BlackBerry = strpos($_SERVER['HTTP_USER_AGENT'],'BlackBerry');

// Nokia SymbianOS devices
    $Symbian = strpos($_SERVER['HTTP_USER_AGENT'],'Symbian');

// HTC devices
    $HTC = strpos($_SERVER['HTTP_USER_AGENT'],'HTC_');  

// Windows Phone devices
    $WP7 = strpos($_SERVER['HTTP_USER_AGENT'],'WP7');
    $WP8 = strpos($_SERVER['HTTP_USER_AGENT'],'WP8');

// Browser's OS devices
    $webOS = strpos($_SERVER['HTTP_USER_AGENT'],'webOS');

// Mobile browser (Opera, Firefox)
    $Opera_M = strpos($_SERVER['HTTP_USER_AGENT'],'Opera M');
    $Fennec = strpos($_SERVER['HTTP_USER_AGENT'],'Fennec/');

if ($iPhone || $iPod || $iPad || $Android || $Mobile || $BlackBerry || $Symbian || $HTC || $WP7 || $WP8 || $webOS || $Opera_M || $Fennec == true) 
{ 
header('Location: http://m.website.com/');
//OR
echo "<script>window.location='http://m.website.com'</script>";
}
?>

Can I add if and else to redirect user to desktop website such as

else {header('Location: http://website.com/');
//OR
echo "<script>window.location='http://website.com'</script>";
}

The else part seems not work because the browser on desktop and mobile display too many redirect error. Thanks

Assuming :

function isMobile() {
    /* return true if is on tablet / phone */
}

On mobile version :

if (!isMobile()) {
    header('Location: website.com');
    exit;
}

On desktop version :

if (isMobile()) {
    header('Location: m.website.com');
    exit;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM