Skip to content

Instantly share code, notes, and snippets.

@mattlundstrom
Created June 15, 2012 22:47
Show Gist options
  • Save mattlundstrom/2939083 to your computer and use it in GitHub Desktop.
Save mattlundstrom/2939083 to your computer and use it in GitHub Desktop.
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment