Redirect your website mobile users to your mobile site in PHP

By RAJESH YADAV Apr 19, 2012
Are you looking for mobile website redirection script. Take a look at this post I want to explain how to detect mobile user agents like Android, Iphone and Blackberry.
Are you looking for mobile website redirection script. Take a look at this post I want to explain how to detect mobile user agents like Android, Iphone and Blackberry. Many people are suggested to redirect with .htaccess file but I had implemented this with PHP. It is very easy just few lines of code.
PHP Code:
<?php
echo $_SERVER['HTTP_USER_AGENT'];
?>

now what you have to do is make a new php file named user_agent.php

<?php
$iphone = strpos($_SERVER['HTTP_USER_AGENT'],"iPhone");
$android = strpos($_SERVER['HTTP_USER_AGENT'],"Android");
$palmpre = strpos($_SERVER['HTTP_USER_AGENT'],"webOS");
$berry = strpos($_SERVER['HTTP_USER_AGENT'],"BlackBerry");
$ipod = strpos($_SERVER['HTTP_USER_AGENT'],"iPod");

if ($iphone || $android || $palmpre || $ipod || $berry == true)
{
header('Location: http://yourmobilesitelink.com/');
//OR
echo "<script>window.location='http://yourmobilesitelink.com'</script>";
}
?>

Index.php or Home.php
You have to include like following script.

PHP Code:
<?php
include('user_agent.php'); // Redirecting http://mobile.site.info
// site.com data
?>


for symbian os add this line in your existing code :

$SymbianOS = strpos($_SERVER['HTTP_USER_AGENT'],"SymbianOS");

and add this function to if statement as below :



if ($SymbianOS || $iphone || $android || $palmpre || $ipod || $berry == true)

RAJESH YADAV

.NET Developer at TechnoGaze Solutions Pvt. Ltd.Administrator of www.CodingFun.inwww.AndroidFun.in

Personal Blog: http://www.CodingFun.in
COMMENT USING

PREMIUM SPONSORS

Hire Mobile & Web Developer on demand. 100% satisfaction. Try for 1 week or Money Back. Local and remote developers available all over USA.

Latest Blogs