mattlundstrom
6/15/2012 - 10:47 PM

PHP Mobile Redirect

PHP Mobile Redirect

<html>
<head>
<script type="text/javascript">
	function redirect(url)
	{
		window.location.href = url;
	}
</script>
</script>
</head>

<body>
<?php
	$browser = $_SERVER['HTTP_USER_AGENT'];
	$mobilesite = 'mobile.html'; // SITE TO FORWARD TO
	$normalsite = 'normal.html'; // SITE TO FORWARD TO
	$isMobile = false;

	// IF ANY OF THESE STRINGS ARE FOUND IN USER AGENT
	$agentslist = array(
		'"iPhone"',
		'"Blackberry"',
		'"Opera Mini"',
		'"Mobile Safari"', // ANDROID NEXUS ONE
		'"webOS"', // PALM PRE
		'"Palm"' // OLDER PALM
		);
	$count = count($agentslist);

	for ($i = 0; $i < $count; $i++)
	{
		if (preg_match($agentslist[$i], $browser))
		{
			$isMobile = true;
		}
	}
	
	if ($isMobile == true)
	{
		// IF MOBILE
		echo '<script type="text/javascript">redirect("'. $mobilesite .'")</script>';

	}
	else
	{
		// IF NOT MOBILE
		echo '<script type="text/javascript">redirect("'. $normalsite .'")</script>';
	}

		
	
?>
</body>
</html>