Jump to content

mobile re-direct


unplugged_web

Recommended Posts

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

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

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

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

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...