unplugged_web Posted August 3, 2010 Share Posted August 3, 2010 Does anybody know how to detect if the user is using a mobile device and then redirect them to a different site accordingly? I've searched google and have found a few things but none of them seemed to work.Essentially what I want to do is this:Check if the browser is a mobile or web browser.If it is a web browser send them go to http://www.mydomain.comif it's a mobile browser send them to http://www.adifferentdomain.comThanks Link to comment Share on other sites More sharing options...
justsomeguy Posted August 3, 2010 Share Posted August 3, 2010 The browser user agent string should indicate what browser and device they're using. You can look at that to figure out if they are a mobile device. I wouldn't be surprised if there was a list of mobile user agent strings online somewhere. Link to comment Share on other sites More sharing options...
wirehopper Posted August 3, 2010 Share Posted August 3, 2010 This uses .jsp, but the same approach could be used with PHP. It also includes a link with user agents.https://core.forge.funambol.org/wiki/HowToM...ageForThePortal Link to comment Share on other sites More sharing options...
ShadowMage Posted August 3, 2010 Share Posted August 3, 2010 Is the mobile page functionally different? Or different content? Or does it just have different styles?If it will only have different styles, you might consider the @media rule. Link to comment Share on other sites More sharing options...
unplugged_web Posted August 6, 2010 Author Share Posted August 6, 2010 Thanks I eventually came up with this: <?phpfunction mobile() { if(isset($_SERVER["HTTP_X_WAP_PROFILE"])) { return true; } if(preg_match("/wap\.|\.wap/i",$_SERVER["HTTP_ACCEPT"])) { return true; } if(isset($_SERVER["HTTP_USER_AGENT"])){ $user_agents = array("midp", "j2me", "avantg", "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", "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($user_agents as $user_string){ if(preg_match("/".$user_string."/i",$_SERVER["HTTP_USER_AGENT"])) { return true; } } } if(preg_match("/iphone/i",$_SERVER["HTTP_USER_AGENT"])) { return false; } return false;}if (mobile()) { header('Location: http://wap.mydomain.com');}?> thanks for your help. Link to comment Share on other sites More sharing options...
unplugged_web Posted August 6, 2010 Author Share Posted August 6, 2010 sorry I'm back again, on Android I keep getting and error saying:"error on line 10 column 12, enterty ref" I can get it to load on an android if I choose to repass the page as HTML, but most people going to the page won't do that.The code I'm using is: <?phpfunction mobile() { if(isset($_SERVER["HTTP_X_WAP_PROFILE"])) { return true; } if(preg_match("/wap\.|\.wap/i",$_SERVER["HTTP_ACCEPT"])) { return true; } if(isset($_SERVER["HTTP_USER_AGENT"])){ $user_agents = array("Android", "midp", "j2me", "avantg", "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", "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", "mobile"); foreach($user_agents as $user_string){ if(preg_match("/".$user_string."/i",$_SERVER["HTTP_USER_AGENT"])) { return true; } } } if(preg_match("/iphone/i",$_SERVER["HTTP_USER_AGENT"])) { return false; } return false;}if (mobile()) { header('Location: http://waqp.mydomain.com');}?> Link to comment Share on other sites More sharing options...
justsomeguy Posted August 6, 2010 Share Posted August 6, 2010 That's not a PHP error, that's a client-side error. Link to comment Share on other sites More sharing options...
unplugged_web Posted August 7, 2010 Author Share Posted August 7, 2010 That's not a PHP error, that's a client-side error.Is there anyway I can prevent this form happening? Link to comment Share on other sites More sharing options...
wirehopper Posted August 7, 2010 Share Posted August 7, 2010 Validate the client side code.http://validator.w3.org/mobile/http://validator.czweb.org/wml-validator.php Link to comment Share on other sites More sharing options...
unplugged_web Posted August 9, 2010 Author Share Posted August 9, 2010 Validate the client side code.http://validator.w3.org/mobile/http://validator.czweb.org/wml-validator.php Sorry to be dumb, but should I validate the php file or the mobile page? If I go direct to the mobile site then it loads straightaway, but if I go to the php file then I get that error. I've set a redirect in the php file so that it will automatically show the mobile version - which validates with flying colors. The php file returns a few minor errors - but line 10 of the php file is part of the redirect code and is actually just '}'<?phpfunction mobile() { if(isset($_SERVER["HTTP_X_WAP_PROFILE"])) { return true; } if(preg_match("/wap\.|\.wap/i",$_SERVER["HTTP_ACCEPT"])) { return true; } if(isset($_SERVER["HTTP_USER_AGENT"])){ $user_agents = array("Android", "midp", "j2me", "avantg", "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", "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", "mobile"); foreach($user_agents as $user_string){ if(preg_match("/".$user_string."/i",$_SERVER["HTTP_USER_AGENT"])) { return true; } } } if(preg_match("/iphone/i",$_SERVER["HTTP_USER_AGENT"])) { return false; } return false;}if (mobile()) { header('Location: http://waqp.mydomain.com');}?> Link to comment Share on other sites More sharing options...
wirehopper Posted August 9, 2010 Share Posted August 9, 2010 Validate the mobile page. Link to comment Share on other sites More sharing options...
unplugged_web Posted August 9, 2010 Author Share Posted August 9, 2010 Validate the mobile page.okay I validated the mobile site and that has no errors in it at all. It also loads without any problems on the android. It's the php file won't load on the android though Link to comment Share on other sites More sharing options...
Ingolme Posted August 9, 2010 Share Posted August 9, 2010 It sounds like if you had an Ampersand in the code that follows the redirect and the mobile parser is reading it.I recommend an exit; statement after the header() so that the HTML that follows isn't read by the mobile device. if (mobile()) { header('Location: http://waqp.mydomain.com'); exit;} Link to comment Share on other sites More sharing options...
unplugged_web Posted August 9, 2010 Author Share Posted August 9, 2010 It sounds like if you had an Ampersand in the code that follows the redirect and the mobile parser is reading it.I recommend an exit; statement after the header() so that the HTML that follows isn't read by the mobile device.if (mobile()) { header('Location: http://waqp.mydomain.com'); exit;} that sounds exactly what I need - thanks I'll give that a go Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.