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.

Demo: Try to open labs.9lessons.info with mobile devices it will be redirect to touch.9lessons.info (mobile site)
$_SERVER - Server and execution environment information (More info)
<?php
echo $_SERVER['HTTP_USER_AGENT'];
?>
My Android browser Output:
Mozilla/5.0 (Linux; U; Android 2.1-update1; en-in; HTC_Wildfire_A3333 Build/ERE27) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari
echo $_SERVER['HTTP_USER_AGENT'];
?>
My Android browser Output:
Mozilla/5.0 (Linux; U; Android 2.1-update1; en-in; HTC_Wildfire_A3333 Build/ERE27) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari
user_agent.php
Usining strpos() function find position of first occurrence of a string. (more details)
<?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://mobile.site.com/');
//OR
echo "<script>window.location='http://mobile.site.com'</script>";
}
?>
Index.php or Home.php
You have to include like following script.
<?php
include('user_agent.php'); // Redirecting http://mobile.site.info// site.com data
?>









Good
Useful info.. a bit simple in php
Veri good and sencile, i saw another but this is better... Cong...Srinvas!!!
nice...
can anybody find a standard mobile redirect..
example:
using laptop/pc -> twitter.com
if user using mobile device -> m.twitter.com
Very useful and simple script thx....
cool!
simple sweet amazing......
Good, simple... I like it>
But I don't understand something "if ($iphone || $android || $palmpre || $ipod || berry == true)"
The test for the BlackBerry..
1. Why need to "== true"
2. No need for "$" before "berry"
maybe two stupids questions, but if you can explain, it?
Thank you
@Pierre
It was my mistake now modified ..
preg_match would be faster me thinks...
$phones = array('iPhone', 'Android', 'BlackBerry');
if ( preg_match('/('.implode('|', $phones).')/i', $_SERVER['HTTP_USER_AGENT']) ){
redirect
}
It is also more readable imo.
Good this is so useful for me because I want to implement like this code.
Thanks for the tips!
You can also use the Wurfl service. A bit more complete.
http://labs.thesedays.com/2010/07/29/an-easy-way-to-detect-mobile-devices/
wow!
How About Nokia, Sony Ericcson, Samsung, Philips ??
Please Share....
@owd
Here's a good list for you.
http://www.zytrax.com/tech/web/mobile_ids.html
If you're using cPanel, it might be best to create a separate control panel and add an A record for mobile, depending on how much traffic you're getting (mobile.yourdomain.com has its own cPanel, separate from yourdomain.com). That way you cut down on process limits defined by your webhost. It can also serve as a security feature in case your site is ever hacked, as /home/mainsite/ is separate from /home/mobsite/.
Thanks, this is good script!
#!/usr/bin/perl
2 require 'includes/config.inc.pl';
3 require 'includes/functions.inc.pl';
4
5 use CGI;
6 $query = new CGI;
7
8 $useragent = $ENV{HTTP_USER_AGENT};
9 $sitepref = $query->cookie( 'SITEPREF' );
10
11 if ( ismobile( $useragent ) ) {
12 if ( !$sitepref ) {
13 if ( isipad( $useragent ) ) {
14 print $query->redirect( "$IPADURL" );
15 } else {
16 if ( isiphone( $useragent ) ) {
17 print $query->redirect( "$IPHONEURL" );
18 } else {
19 print $query->redirect( "$MOBILEURL" );
20 }
21 }
22 } else {
23 if ( $sitepref=='MOBILE' ) {
24 print $query->redirect( "$MOBILEURL" );
25 } else {
26 if ( $sitepref=='IPHONE' ) {
27 print $query->redirect( "$IPHONEURL" );
28 } else {
29 if ( $sitepref=='IPAD' ) {
30 print $query->redirect( "$IPADURL" );
31 } else {
32 print $query->redirect( "$NORMALURL" );
33 }
34 }
35 }
36 }
37 } else {
38 if ( !$sitepref ) {
39 print $query->redirect( "$NORMALURL" );
40 } else {
41 if ( $sitepref=='MOBILE' ) {
42 print $query->redirect( "$MOBILEURL" );
43 } else {
44 if ( $sitepref=='IPAD' ) {
45 print $query->redirect( "$IPADURL" );
46 } else {
47 if ( $sitepref=='IPHONE' ) {
48 print $query->redirect( "$IPHONEURL" );
49 } else {
50 print $query->redirect( "$NORMALURL" );
51 }
52 }
53 }
54 }
55 }
Superb discussion thank you for sharing.
Where should I put each part of the code? I'm noob in php
Well, the post is really the freshest on this valuable topic. I fit in with your conclusions and will thirstily look forward to your next updates.
This method works great - I use it for www.digisolved.com and it works great!
For some reason I'm getting a T_VARIABLE parse error on line 8 with the code above. Is anyone else having this issue?
Further to my last comment, the only way I could find to stop the issue was to remove $berry from the if/else statement. It still caused an issue even when I tried renaming it. Very strange.
I would like to forward all mobile devices to the mobile site. But give the option to view the full site from the mobile. So how would I disable this script if they choose the full site on the mobile device?
Wow It is nice info. I like it. Thanks a lot.
I tested your demo on my blackberry and it doesn't redirect :/
Im using this php script:
Hi. Thanks, great. Using it on http://www.lane.no/
For anyone interested, I wanted a php redirect that only redirected mobile users on the home page to a mobile site but that also gave them the option of going to the live site. To do this I created a link on the mobile site that creates a cookie and checked for the cookie in the index.php before doing the redirect.
Code added to index.php:
$mobile = 'http://mobile.mysite.com/';
if ((strstr($_SERVER['HTTP_USER_AGENT'], 'iPhone')) && empty($_COOKIE["mobilesitez"]) && $_SERVER['REQUEST_URI']=="/") {
header("Location: " . $mobile);
}
Code added to mobile page:
function set_cookie () {
document.cookie = 'mobilesitez=mobilesitez; domain=.mysite.com;';
window.location = 'http://www.mysite.com/'
}
P.S. I had to remove the javascript style tags and the HTML A tag that calls it in order to post it in this comment. But you should be able to add those yourself. :)
Thanks! Most appreciated!
Thanks so much worked perfect for me...Really simple.
I see the 3 sets of code above but I am new to this and don't know where to put the code.
Where do we put
$_SERVER and user_agent.php and
Index.php or Home.php
thank you very much.
your tutorial is really easy to understand for beginners :)
HTC Desire not redirecting in demo at all so Im not gonna try this :(
nice post, thanks for share
Thanks! Worked for me :)
Thanks dude!
for nokia is just add this
$symbian = strpos($_SERVER['HTTP_USER_AGENT'],"Symbian");
and add $symbian in "if row"
Will this php scipt work if I'm not using php for the site pages? It's all xhtml.
hi,I am completely new to this stuff.i tried to access labs.9lessons.info from my android,got the page ,was not diverted to touch??
why so complicated???
whats wrong with:
php:
$browser = get_browser(null, true);
if ($browser["ismobiledevice"] == true) {
//redirect to mobile page
}
Great script I tried it and it worked simple as 1, 2, 3!
this is very usefull script for mobile web
Thanks everyone, I already have a script that is working fine except that it does not work on Nokia Browsers and they keep viewing the full site which i don't like. Can anyone help with with the code for just detecting Nokia Browers.
Waiting in anticipation, thank you all.
You guys are great
hi i want index.php (have redirect to home page, iphone page, i badpage, blackberry page and mobile default page) but i don't know to creating in php anyone for help me here please
Why not just use a bit of JS to detect a screen size smaller than 480px and redirect? that way you dont have to worry about accounting for all useragents.
<5cript type="text/java5cript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js">
<5cript type="text/java5cript">
$(document).ready(function(){
if($(window).width() < 480){
window.location = "http://m.yoursite.com";
}
});
- had to change the '5cript' tags to post this, so obviously, change the '5' in the '5cript' tags to an 's'
This is a great JS alternative. Thanks for putting it together for us.
i try do this one for mysite.And i success to repair mysite.
Thanks
Love this! Seemed to be only one I found that worked. All the others that I tried gave me a PHP error that headers couldn't be modified and the page never loaded. One thing - I still get the error (with this script) on the Iphone but it does go away when the page is loaded. Any ideas? I dont need help just thoughts on what is causing this.
You don't have to actually echo $_SERVER['HTTP_USER_AGENT']; first, do you? I'm gonna try this out tomorrow, my shift is over now.
there is a problem with blackberry, this script doesn't recognise bb10.
user agent is new, changed. any idea?
i'm trying with:
$berry10 = strpos($_SERVER['HTTP_USER_AGENT'],"BB10.+Mobile");
It does the job!
Thank you very much!
this is very usefull script for mobile web
using this on my project. thank u very much, sir!
Thank you!
Very useful!
Can someone Please explain to me how to implement this code. I'm a newbie.
I have a website in html code not php, what do I need to do? Thank you.
How i can find user request coming from mobile or pc. i create one mobile site and one pc website. if user on mobile then mobile site open and if user on pc then pc website open. my code in jsp.
what should it be for windows phone?
thanks..
fantabulous bro...
Awesome.. Ive used the same for my personal website redirection(PHP solution instead of javascript). Thankyou for sharing this.
Implamentation
create a page called "user_agent.php" and add the code in the window above to it.
then chage your index.html page to index.php and add the code above from the "Index.php or Home.php" window.
then link to your home page via your domain links as .php not .html
this can effect or break any back links on the web directed at the origional .html and not .php
so you may also want another edirect from an .html link index.
how it works.
the client requests your domain
the server {PHP Sopported...} reads the redirect code on the 1st page or the regular page if its a decktop. the "include" code on this pages reads the "user_agent.php" and checks to see if its a mobil device... if it is it redirects to the header if not it continues the origional desktop version of the page.
Really Good Stuff.....Thank you so much Mr.Srinivas Tamada. You have solved my big problem...
Saved me from hours of extra work thanks!