Previously i wrote an article which explains Detect users internet browser and redirect to different page specific to it but this was just a basic idea on how to do this focusing mainly on detecting the user browser.
In this tutorial you will be learning how to detect if the user is coming from Apple iPhone or iPod and redirect him to the mobile version of your website. We will also see how to move him to normal domain if he comes from non-iPhone (and non-iPod) browser to the mobile version of the site.
We will use the redirect codes in PHP along with browser detection to make our logic work.
Scenario:
We have a website named: domain.com which has a subdomain: m.domain.com which hosts the mobile version of our site. What we want to do is that if some one comes through an iPhone or iPod on our site we want him to be redirected to m.domain.com using PHP.
Code:
Here is the code that will go on top of the domain.com/index.php file:
<?php //setting the iPhone / iPod browser variables $ipod = stripos($_SERVER['HTTP_USER_AGENT'],"iPod"); $iphone = stripos($_SERVER['HTTP_USER_AGENT'],"iPhone"); //detecting device browser if ($ipod == true || $iphone == true){ header( 'Location: http://m.domain.com/' ) ; } ?>
Please note use this code on top of domain.com/index.php ONLY and NOT on m.domain.com/index.php
If you have any queries or specific requirements for this code please tell me in comments i will update it according to your needs.
Hi Nabeel, that code works perfectly. Now i am wondering how to expand the list of mobile devices? Would the following list work? Not sure about the spaces.
Would it be more elegant to use an include file with a list of mobile devices?
Looks like you are new to PHP . . .
do you want my service to do it live on the site you are working on? Is this your own site or some project that you are working on?
if you can wait for a while i can upload code for multiple browsers too btw.
In your case you need to specify each mobile device with it’s own variable name (as re defining the variable clears the previous value and you can’t add it to the previous one in this code logic case – i will post a different one soon too btw)
and make sure you know the correct http_user_agent value for each mobile browser.
You can have this example for your case: if you want to add android to it then use:
Hopefully it makes sense. Do let me know if you need any further assistance.
Hi,
if i have code like below ,
session_start();
check_logged();
if( $logged[‘member’] ){
header(‘Location: http://www.mysite.com/index.php‘);
should i put my script before or after your script and
one more thing, do your script must be on top ?
my script should be on the top
why? because it will check if user is coming from iphone or not, once it is, there is no purpose to stay on this page, and he will be taken to iphone optimized page
if he is not, then run the rest of the code, which is your’s code.
if you need further help, do let me know
Hi Nabeel, yes, new to php and very tired… need a holiday… how did i miss updating the variables? ouch! This is my site that i am working on… and don’t have a lot of cash to pay tech support so i try and work things out for myself. I am very grateful for your help.
ok.. so did the code work for you?
Hi Nabeel, as i said before the code works marvelously. I don’t have a mobile device but i can test by changing the user agent in the web browser, and it redirects the page to the mobile subdomain. I tried lots of other code (that worked for others) but yours was the first code that actually worked for me. Once again many thanks.
You’re welcome Abby
We created an iPod-style drilldown menu to help users traverse hierarchical data quickly and with control. It’s especially helpful when organizing large data structures that don’t translate well into traditional dropdown or fly-out menus.
hi, if i have codes like below
should i put after or before this code
sorry I didn’t receive the code, :s please send to me again without including javascript start and end tag
hi, if i have codes like below
——————————————————-
——————————————————-
should i put after or before this code
——————————————————-
——————————————————-
can you please tell me your browser? and details of how you tried to paste the code, this will help me fix this issue of code in comments. Thanks
Hellooo every one
plz can anyone guide me that can we check the settings of the mobile set of user opening the site? i want to check the location settings of mobile set, can i do it in my php site?
reply ASAP
thanks in adv.
Regards
you can do this by getting the ip address of the user and then tracing the location of that ip
What if i want it to change something on the page instead of a redirect?
you can use the same code to detect the browser and change, add, edit the part of code accordingly.
Tell me the details i might try to help
well currently im loading a short video on my home page that i’d like to replace with an image instead on iphone and other small form factor mobile devices.
check it out on wmlm.co
thats pretty simple, just detect the browser and make a condition for which code to echo!
ugh … noob mistake.. thanks!
You’re welcome Alex! :) Let me know if you need any assistance