PHP Code to Redirect Mobile/Handheld Users
by c.bavota | Posted in Tutorials | 24 comments
With the amount of people walking around using iPhones or some sort of Blackberry or PDA, it makes sense to provide an alternative landing page for Mobile/Handheld users that is formatted for their device. I was recently hired to create a mobile page by a client and found a great piece of code to help with the whole redirection issue.
The folks over at Detect Mobile Browsers have developed a great tool that checked to see if a user has reached your site through a Mobile/Handheld device, and if so will redirect to the page your choice.
Just download the code off of their site and use their handy Function Generator to configure it how you want.
To configured it to treat everything as a Mobile/Handheld device use this code.
<?php
include('mobile_device_detect.php');
mobile_device_detect(true,true,true,true,'http://your-site.com/your-site-mobile-page',false);
?>
To add an if statement to only make it redirect if it is the home page use this.
<?php
if(is_home()) {
include('mobile_device_detect.php');
mobile_device_detect(true,true,true,true,'http://your-site.com/your-site-mobile-page',false);
}
?>
Add this to the top of your header.php file and uploaded their mobile_device_detect.php file to your main directory. If you are not using WordPress, just add the first bit of code above (without the if statement) to the top of your index.php file.
NOTE: This will only work if your site is built with PHP.



Hi,
try to use Apache Mobile Filter, is very useful to manage mobile device access, it’s based on WURFL as device repository.
For more info: http://www.idelfuschini.it/apache-mobile-filter.html
I have published the new version of Apache Mobile Filter, now the filter is give to you the information of capabilities as apache environment.
Now you can develope in any language (php,jsp, ruby etc.) and have the information of mobile capability.
Read more info here: http://www.idelfuschini.it/it/apache-mobile-filter-v2x.html
Is this working for all mobile devices?
Hi,
To help people to do mobile device detection we have published two free ways to do device detection: detectFree and Movila Detection proxy.
DetectFree works as a service. You need to add a small JavaScript script in to your website and it checks whether the browser is mobile or not.
Movila Detection proxy is a full HTTP proxy which has mobile device detection as a one feature.
Link: Mobile device detection
Hope this helps.
The “Apache Mobile Filter” is one of the modules of “Apache Module Registry” portal (http://modules.apache.org/search.php?id=1787)
Thanks for these modules, i was searching for this only.
Uhh! This is a dream come true. It solves SOOOO many problems.
Thanks for it, it is working very well
Hi there may I reference some of the information found in this entry if I reference you with a link back to your site?
Fantastic site I have been looking for such information for a long time, glad to have found it and will come again soon. Thanks
I am glad that I finally understand how to do it… thanks
I am not on wordpress. I have my header as a standalone in ‘header.php’ The difference between ‘mobile.php’ and ‘mobile_device_detect.php’ is the first 65 lines of description have been deleted.
I am receiving the following error message:
“Warning: Cannot modify header information – headers already sent by (output started at …/index.php:9) in …/mobile.php on line 100″
line 9 of index.php =
line 100 of mobile.php is line 165 of mobile_device_detect.php=
header('Location: '.$redirect); //redirect to the right url for this deviceany ideas? I’m new to everything
The problem is that you obviously have something being outputted to the page before you try to redirect through the header command. Nothing can be outputted to the page before a header command is executed. You can have functions and such but nothing can be outputted.
line 9 of index.php=
include ('header.php');I have a question: why to redirect it? what is the relevance?
I want to redirect to antoher page. I use this statement in my if condition.. EVERY TIME condiotin is true but this error occurs. I AM SICK OF THIS
Warning: Cannot modify header information – headers already sent by (output started at C:\xampp\htdocs\Unnamed Site 2\work\databasecon\dbcon.php:12) in C:\xampp\htdocs\Unnamed Site 2\work\save_userdata.php on line 23
That error occurs because something has been sent out before the redirect. Your redirect must be included before anything is displayed on your web page.
This is not working for me………I am using wordpress – my code is
in my header.php file and I placed the mobile_device_detect.php file in the root directory of the wordpress installation (same directory as the .htaccess file).
this is my code…
require_once('mobile_device_detect.php'); mobile_device_detect(true,true,true,true,true,true,true,'http://www.abacusecommercesolutions.mobi',false);You got a lot of trues in there. Make sure to follow the tutorial. If you are having issues, you should check out http://detectmobilebrowsers.mobi/
I did actually. I used their function generator and that was the output it gave me then copied the files to where I said I did….. Have you got any more ideas….?
thanks
This method works great – I use it for http://www.digisolved.com and it works great!