Hmmm...That looks close to being exactly what I need.
I’m not sure If I’ve utilised the script properly though...I’m not sure if it matters, but my mobile site isn’t a MODx site, its just a static site using the jQuery mobile framework. My main site (that I’m trying to divert mobile users from) is a MODX site though.
I’ve put the script into a snippet and calling it uncached at the head of my template. When I try loading the site on my iPhone, it redirects to the home page (id = 1) and a message pops up saying that it "couldn’t be opened because too many red’rects are in operation". Or something like that!
This is what I’m trying:
<?php
/* Mobile_redirect
01/28/11 1:40 pm cet
01/28/11 6:14 am est
11/24/09 10:31 am est
original code: http://www.brainhandles.com/techno-thoughts/detecting-mobile-browsers
usage: call in the top of your template
[!Mobile_redirect? &mID=`1`!]
[!Mobile_redirect? &url=`http://redirectdomain.com`!]
[!Mobile_redirect? &exclude=`iPad`!]
link back to main page:
add &views=1 to the link
ex: <a href="http://yourmainsite.com?views=1">Link Back to Main site</a>
*/
// set the MODx document ID of the mobile page
$mid = isset($mID) ? $mID : "1";
// set the url you want to redirect to
// http://domain.com or http://sub.domain.com
$url = isset($url) ? $url : 'm.sitename.co.uk/jquery';
// add any names you want to exclude
$exclude = isset($exclude) ? explode(',', $exclude) : array();
if(isset($_GET['views']) || $_SESSION['views'] =='1'){
$_SESSION['views'] = 1;
}else{
if(isset($_SERVER["HTTP_X_WAP_PROFILE"]) || preg_match("/wap\.|\.wap/i",$_SERVER["HTTP_ACCEPT"])) {
if($mid =="" && $url !=""){
header("Location: ".$url."");
}else{
$goto = $modx->makeUrl($mid);
$modx->sendRedirect($goto);
}
}
if(isset($_SERVER["HTTP_USER_AGENT"])){
$uastring = $_SERVER["HTTP_USER_AGENT"];
if(preg_match("/Creative\ AutoUpdate/i",$_SERVER["HTTP_USER_AGENT"])) {
return false;
}
$check_for_mobiles = true;
foreach($exclude as $not_mobil){
if( stripos($uastring, $not_mobil) !== false ) {
$check_for_mobiles = false;
break;
}
}
if ( $check_for_mobiles == true ) {
$mobiles = array("midp", "j2me", "avantg", "iphone", "docomo", "novarra", "palmos", "palmsource", "240x320", "opwv", "chtml", "pda", "windows\ ce", "mmp\/", "blackberry", "mib\/", "symbian", "wireless", "nokia", "hand", "mobi", "phone", "cdm", "up\.b", "audio", "SIE\-", "SEC\-", "samsung", "HTC", "mot\-", "mitsu", "sagem", "sony", "alcatel", "lg", "erics", "vx", "NEC", "philips", "mmm", "xx", "mobi", "panasonic", "sharp", "wap", "sch", "rover", "pocket", "benq", "java", "pt", "pg", "vox", "amoi", "bird", "compal", "kg", "voda", "sany", "kdd", "dbt", "sendo", "sgh", "gradi", "jb", "\d\d\di", "moto");
foreach($mobiles as $mobil){
if( stripos($uastring, $mobil) !== false ){
if($mid =="" && $url !=""){
header("Location: ".$url."");
}else{
$goto = $modx->makeUrl($mid);
$modx->sendRedirect($goto);
}
exit;
}
}
}
}
}
?>