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
?>
Useful info.. a bit simple in php
ReplyDeleteVeri good and sencile, i saw another but this is better... Cong...Srinvas!!!
ReplyDeletenice...
ReplyDeletecan 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....
ReplyDeletecool!
ReplyDeletesimple sweet amazing......
ReplyDeleteGood, simple... I like it>
ReplyDeleteBut 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
ReplyDeleteIt was my mistake now modified ..
preg_match would be faster me thinks...
ReplyDelete$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.
ReplyDeleteThanks for the tips!
ReplyDeleteYou can also use the Wurfl service. A bit more complete.
ReplyDeletehttp://labs.thesedays.com/2010/07/29/an-easy-way-to-detect-mobile-devices/
wow!
ReplyDeleteHow About Nokia, Sony Ericcson, Samsung, Philips ??
ReplyDeletePlease Share....
@owd
ReplyDeleteHere'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/.
ReplyDeleteThanks, this is good script!
ReplyDelete#!/usr/bin/perl
ReplyDelete2 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.
ReplyDeleteWhere should I put each part of the code? I'm noob in php
ReplyDeleteWell, 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.
ReplyDeleteThis method works great - I use it for www.digisolved.com and it works great!
ReplyDeleteFor some reason I'm getting a T_VARIABLE parse error on line 8 with the code above. Is anyone else having this issue?
ReplyDeleteFurther 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.
ReplyDeleteI 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?
ReplyDeleteWow It is nice info. I like it. Thanks a lot.
ReplyDeleteI tested your demo on my blackberry and it doesn't redirect :/
ReplyDeleteIm using this php script:
ReplyDeleteHi. Thanks, great. Using it on http://www.lane.no/
ReplyDeleteFor 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.
ReplyDeleteCode 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!
ReplyDeleteThanks so much worked perfect for me...Really simple.
ReplyDeleteI see the 3 sets of code above but I am new to this and don't know where to put the code.
ReplyDeleteWhere do we put
$_SERVER and user_agent.php and
Index.php or Home.php
thank you very much.
ReplyDeleteyour tutorial is really easy to understand for beginners :)
HTC Desire not redirecting in demo at all so Im not gonna try this :(
ReplyDeletenice post, thanks for share
ReplyDeleteThanks! Worked for me :)
ReplyDeleteThanks dude!
ReplyDeletefor 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.
ReplyDeletehi,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??
ReplyDeletewhy so complicated???
ReplyDeletewhats 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!
ReplyDeletethis is very usefull script for mobile web
ReplyDeleteThanks 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.
ReplyDeleteWaiting 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
ReplyDeleteWhy 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.
ReplyDelete<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.
ReplyDeletei try do this one for mysite.And i success to repair mysite.
ReplyDeleteThanks
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.
ReplyDeleteYou 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.
ReplyDeletethere is a problem with blackberry, this script doesn't recognise bb10.
ReplyDeleteuser agent is new, changed. any idea?
i'm trying with:
$berry10 = strpos($_SERVER['HTTP_USER_AGENT'],"BB10.+Mobile");
It does the job!
ReplyDeleteThank you very much!
this is very usefull script for mobile web
ReplyDeleteusing this on my project. thank u very much, sir!
ReplyDeleteThank you!
ReplyDeleteVery useful!
Can someone Please explain to me how to implement this code. I'm a newbie.
ReplyDeleteI 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.
ReplyDeletewhat should it be for windows phone?
ReplyDeletethanks..
ReplyDeletefantabulous bro...
ReplyDeleteAwesome.. Ive used the same for my personal website redirection(PHP solution instead of javascript). Thankyou for sharing this.
ReplyDeleteImplamentation
ReplyDeletecreate 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...
ReplyDeleteSaved me from hours of extra work thanks!
ReplyDeletefunction isMobileAgent($agent){
ReplyDelete//add unique mobile user agent string
$mobile=array("Android","BlackBerry","iPhone","--add-more-list--");
foreach($mobile as $v){
if(strpos($agent,$v)!==false) return true;
}
return false;
}
//use script
if(isMobileAgent($_SERVER['HTTP_USER_AGENT'])) echo "Mobile Users";
else echo "PC Users";
Hi, thanks a million for this script! i was nearly desperate as one of my sites could be redirected because of its responsive design...
ReplyDeleteMy question is... i can't see symbian (nokia) on it. Can i add it manually and it works or wouldn't it work?
Example:
$nokia = strpos($_SERVER['HTTP_USER_AGENT'],"nokia");
if ($iphone || $android || $palmpre || $ipod || $berry || $symbian == true)
would that work?
Had some trouble by submitting my post so i dont know it came through... Here again:
ReplyDeleteWould it also work with Symbian Phone ie: Nokia if i add it to the script?
Example:
$nokia = strpos($_SERVER['HTTP_USER_AGENT'],"nokia");
if ($iphone || $android || $palmpre || $ipod || $berry || $symbian == true)
{
How to redirect the page when search in browser like google
ReplyDeleteI quote "Try to open labs.9lessons.info with mobile devices it will be redirect to touch.9lessons.info (mobile site)"
ReplyDeleteBut when I tried it on my Samsung Galaxy Grand aneroid phone, it sent me to: labs.9lessons.info/login.php
and not to the mobile site
This is simple and works super
ReplyDeleteHow its work:
ReplyDelete$nokia = strpos($_SERVER['HTTP_USER_AGENT'],"nokia");
if ($iphone || $android || $palmpre || $ipod || $berry || $symbian == true)
{
"Variable name :$nokia and you called variable in the If statement is
|| $symbian? Is it correct??? "
hello guys nice info . tanks
ReplyDeleteGood information :)
ReplyDeleteThank you so much. much simple code.
ReplyDeletenice script, very helpfull, thank you
ReplyDeleteThanks for sharing this script!
ReplyDeleteNice script,
ReplyDeleteHow to redirect the page on Windows Phone?
thanks for this share
ReplyDeleteok thanks for the informations
ReplyDeleteThanks again and we look forward to hearing from you guys!
ReplyDeleteGreat script.
ReplyDeletepost useful things for my blog. thank's
ReplyDeleteThis is simple and works super
ReplyDeletethanks for share information
I really think your information is a good for me to lesson many more.
ReplyDeleteThank you and nice tobe here.
Regards,
Come here looking for something new and good lesson with 9lessons.info...thank you
ReplyDeleteGreat posting in here...of course great people...would thank you for you for many lesson
ReplyDeleteHhmm...i am familiar with html code in blogger than PHP, i am still confiuse...
ReplyDeleteafter I took time to read from an article that you share. I was able to reap the benefits of what is contained in the content of this material
ReplyDeleteThis information has long'm looking for. Finally I found it on your website. Thank you. Another time I will travel back to your website.
ReplyDeleteI like the articles you post
ReplyDeleteI'm really excited to be here with your thinking make this post awesome
ReplyDeletethanx but please tell me about tab? please
ReplyDeleteNice post but for this m allways suggesting uh use jquery
ReplyDelete< script >
$(document).ready(function(){
if($(window).width() < 480){
window.location = "http://m.facebook.com";
}
});
This script its from width of screen
hello
ReplyDeletehow put demo in blogger
thanks.. it's very usefull
ReplyDeleteThanks
ReplyDelete