PHP Mobile Redirect and View Full Site
Ranked #1,262 in Business & Work, #57,902 overall
PHP Mobile Detect/Redirect and View Full Site
Instructions

I have viewed many posts on various blogs in which people are seeking functioning mobile redirects. There are many solutions to detect a mobile phone and redirect the user to your mobile site. A PHP mobile detect/redirect is an ideal solution, and will not bog down your page load times. While there are many other mobile detect and redirect solutions, complete solutions that will allow a mobile user back to view the full site are lacking. In the following article, I will show you functioning PHP script that will detect a mobile user, redirect the user, and give the option for the mobile user to "view the full site."
If you follow these instructions you will have a functioning mobile redirect on your site:
First, go to the following URL and download the mobile_detect.php file:
http://code.google.com/p/php-mobile-detect/
Next, follow the instructions on the page, and upload the mobile_detect.php to your root directory,
Insert the following code on your index or home page:
@include("Mobile_Detect.php");
$detect = new Mobile_Detect();
if ($detect->isMobile() && isset($_COOKIE['mobile']))
{
$detect = "false";
}
elseif ($detect->isMobile())
{
header("Location:http://www.yourmobiledirectory.com");
}
When you initially visit your site, the above script will verify if you are viewing the site on a mobile device. If so, you will be directed to http://www.yourmobiledirectory.com. If you are not on a mobile device you will view the regular site.
You may have noticed the following code:
if ($detect->isMobile() && isset($_COOKIE['mobile']))
{
$detect = "false";
}
In this statement I am checking for both a mobile device and a cookie named "mobile". I am using this statement to override the redirect if the mobile user elects to view the full site. The following code is used on the mobile index or home page to set the cookie:
setcookie("mobile","m", time()+3600, "/");
I have also included a link on the mobile site called, "View Full Site", and this directs the user back to the index or home page of the main site. Once back at the main page the cookie, "Mobile", is detected and thus the mobile redirect is overridden.
I know there are a ton of mobile detect and redirect solutions, but I have noticed there are not a lot of "view full site" options. If you use this please leave me a comment, and let me know how it is working out for you. Also, if you have additional methods , mobile redirects, or more specifically "view full site" options, please drop me a line. I am trying to put together a collection to help others. Thanks.
Links
- Colorado Web Design
- Colorado Web Design - SEO - Upgrades
View Full Site
View Full Site

I have added a screenshot of how the php code should look on the mobile index or landing page. Make certain that you are linking the "View Full Site" link on your page back to your homepage. Also, make sure there is no white space between your php code and your html.
View Full Site - Troubleshooting

Here is a fix. Modify the cookie as follows:
setcookie("mobile","m", time()+3600, "/",".yourdomain.com");
".yourdomain.com"
For example, if your site was, www.redirectsrock.com...
The cookie would be as follows:
setcookie("mobile","m", time()+3600, "/",".redirectsrock.com");
That has been solving most of the problems, especially if you are using a CMS.
Check These Out
Guestbook Comments
-
-
marinavn
May 29, 2012 @ 7:17 pm | delete
- 1000 thanx to you... You save me. Your content helped me on a project at school.
transport marfa mobila transport moloz
-
-
-
barrettathome May 2, 2012 @ 5:11 pm | delete
- Was having a problem with viewing the full site added the url to the end of the setcookie and worked like a champ... setcookie("mobile","m", time()+3600, "/", ".yourdomaine.com"); Is there a way to exclude tablets for this? Do I just comment out the code for the tablets section?
-
-
-
knoxjeffrey
Apr 22, 2012 @ 1:48 pm | delete
- Sorry if this is a basic question but if I don't want the rule to apply for blackerry mobile devices can I just edit out 'BlackBerry' => 'BlackBerry|rim[0-9]+', from Mobile_detect.php and it won't detect as a mobile device and just send to the desktop version?
-
-
-
alan-portman
Apr 5, 2012 @ 3:39 pm | delete
- Ok, I need to ask for help. I want mobile users who go to healthcareersstlouis.com (a joomla site) to redirect to careersinstlouis.com. Here is the index.php from health....com http://codepaste.dev7studios.com/item/cpmikufbg
What am I doing wrong?
-
-
-
crystal-toupin Apr 5, 2012 @ 2:26 am | delete
- I am having a heck of a time with this. I uploaded the code into my index.html as the very first line. However, the code shows up on my page. Where is it supposed to go?
-
-
-
lars_123456
Mar 8, 2012 @ 7:10 am | delete
- Hi,
thanks for the guide!
I´m getting following error:
"Fatal error: Class 'Mobile_Detect' not found in /home/XXXX/public_html/XXXX.php on line 3"
-
-
-
fmostrom
Feb 26, 2012 @ 12:34 pm | delete
- Hi i am just trying out your redirect and got problems, when i click on my "view full page" it gets me back to mobile page all the time. here is my code:
http://pastie.org/3464924
Thanks
-
-
-
code-96
Feb 26, 2012 @ 8:26 pm | delete
- It worked using FireFox with the wml browser plugin, which emulates a mobile device. However, it will not work from my Samsung mobile. I will look into it, if anyone else has any suggestions please post. Thanks.
-
-
-
fmostrom
Feb 27, 2012 @ 10:22 am | delete
- Hi, have checked it dosen´t work for my iphone or with mobile addon in firefox.
-
-
-
fmostrom
Feb 27, 2012 @ 10:38 am | delete
- Hi again, sry for i making a huge misstake :) i had the old redirect in my .htaccess left!!! that redirected me all the time. its working great now. thank you for taking your time code-96
-
-
-
invalidmemory
Feb 5, 2012 @ 12:11 pm | delete
- i finally got a redirect to work with my static mobile site + joomla full website, but i'm not using this way unfortunately. I got it work with a redirect in the htaccess file, which i've tested on all user agent switchers. There's a javascript way too, but i think htaccess works best for joomla users.
-
-
-
hotcube
Feb 4, 2012 @ 12:24 pm | delete
- Hi there, i really like your tutorial! but one thing i noticed in the code. When i browsed it again to my website it gives me the full site version, supposedly it gives me the mobile version for the second time. Any idea?
-
-
-
invalidmemory
Feb 3, 2012 @ 12:43 pm | delete
- hey, tried the fix, but still doesnt seem to work with joomla
-
-
-
invalidmemory
Jan 22, 2012 @ 9:30 am | delete
- hi, i'd rather not post my website address on here, can i send a private message somehow? cheers
-
-
-
code-96
Jan 23, 2012 @ 2:03 pm | delete
- Try the fix above, let me know!!
-
-
-
NCCCWeb
Jan 18, 2012 @ 10:47 am | delete
- I'm having the same issue. I've gotten the redirect to mobile to work, but not the full site link to work, so that it breaks the loop. Can anyone help? I put the code of the homepage and the mobile page here:
http://pastie.org/3207994
-
-
-
code-96
Jan 20, 2012 @ 8:04 pm | delete
- Try changing your cookie to the following:
setcookie("mobile", m, time()+3600, "/", ".niagaraccs.suny.edu");
Try it again
-
-
-
NCCCWeb
Jan 23, 2012 @ 10:03 am | delete
- Thank you for your help! It works!!
-
-
-
NCCCWeb
Jan 23, 2012 @ 2:07 pm | delete
- Worked great! Thanks!
-
-
-
invalidmemory
Jan 18, 2012 @ 7:00 am | delete
- i'm using joomla, but it still doesnt work, i've largely given up, redirect to the mobile site works, but thats it, view full site still loops so i've had to remove view full site link. i think its because the index.php on joomla doesnt have a closing tag, so the cookie doesnt work
-
-
-
code-96
Jan 18, 2012 @ 10:16 am | delete
- What is your site?
-
-
-
JankyLV
Jan 16, 2012 @ 1:32 pm | delete
- I am trying to use this redirect too.. It works. It redirected me to the mobile version of my web page, but I can't get the "view full site" link to work. it just redirects straight back. It is at the top of the page, before the html tag. any ideas? thank you.
-
-
-
code-96
Jan 16, 2012 @ 1:34 pm | delete
- Are you trying to run this on a CMS such as Wordpress, Joomla...etc??
-
-
-
JankyLV
Jan 16, 2012 @ 3:22 pm | delete
- yes, i am using it on a CMS, but different one - GetSimple
-
-
-
code-96
Jan 16, 2012 @ 1:46 pm | delete
- Go to Pastie.org , paste your code, once it has posted copy the url, and post it here. This will allow me to look at your code. Thanks.
-
-
-
JankyLV
Jan 16, 2012 @ 3:35 pm | delete
- there is not much code to be copied. i added the setcookie line at the top of the mobile page, right before html tag. the confusing part is with the desktop version of page. i added it to the index.php, but it wasnt working. then i tried to add it to the template file, that puts the page together.. still no luck..
-
-
-
code-96
Jan 16, 2012 @ 3:40 pm | delete
- I am not familiar with the file structure that GetSimple is using. However if the redirect is working, no need to mess with the index page. We need to figure out if the mobile page is setting a cookie. Have you checked your cookies after visiting the mobile page?? Is the cookie there??
-
-
-
JankyLV
Jan 16, 2012 @ 4:03 pm | delete
- yup, the cookie shows up. what could be the problem then? :|
-
-
-
code-96
Jan 16, 2012 @ 4:33 pm | delete
- Let me see your index page code that includes the redirect above please post it and post the url. Thanks.
-
-
-
JankyLV
Jan 16, 2012 @ 4:41 pm | delete
- http://pastie.org/3197664
the header.php file has the tag in it. i tried to put the code there, but still nothing, so i left it there, where it is now.
-
-
-
code-96
Jan 16, 2012 @ 4:56 pm | delete
- Try changing your cookie to the following:
setcookie("mobile", m, time()+3600, "/", ".ruderacing.lv");
Try it again...
-
-
-
JankyLV
Jan 16, 2012 @ 4:59 pm | delete
- it works! thank you for everything! :) i really appreciate it! :)
-
-
-
invalidmemory
Jan 6, 2012 @ 11:32 am | delete
- Hi there, I've followed every bit of your guide, but when i want to view the full site on my mobile site (by following view full site link at the bottom) it keeps redirecting back to the mobile site, and ignores the cookie....
I have tried placing the php cookie code before the head, after the head, in the body, after the body, everywhere lol, but nothing seems to work, and it keeps redirecting back to the mobile version.
is there any better way of placing the code in the mobile index.php??
my PHP is outside of the html , for example:
My Example
Here is some more HTML
I'm testing the site using my mobile (iphone), plus mobilizer, plus firefox's user agent switcher add-on
regards
-
-
-
code-96
Jan 9, 2012 @ 1:24 pm | delete
- See above View Full Site post. I need to see your code or your site. I have tested many devices and they all work. I wish we could post code on here, maybe I will take this discussion somewhere else.
-
-
-
SproutCreatives
Jan 4, 2012 @ 7:15 pm | delete
- I am trying to use your mobile redirect and that part works fine. For some reason I can't get the "View Full Site" link to work. It simply redirects to mobile version.
When I add to my mobile index.php file I get an error. Warning: Cannot modify header information - headers already sent by (output started at........
-
-
-
code-96
Jan 6, 2012 @ 2:27 am | delete
- That error normally means that you have some white space before or after your PHP tags..... ()
-
-
-
SproutCreatives
Jan 6, 2012 @ 11:18 am | delete
- Yea that's what I read from some searching. But I am still getting the error.
Here is the code that I have used:
-
-
-
code-96
Jan 9, 2012 @ 1:04 pm | delete
- I do not think they will allow code to placed on here. What is your site?? Try to visit mine, www.code-96.com , on your mobile device and let me know if the view full site works. I have tested on many mobile phones, kindles, ipads...etc
-
-
-
invalidmemory
Jan 9, 2012 @ 5:11 pm | delete
- hey, yeah your site works. can you tell me where abouts your putting the set cookie code? ---> setcookie("mobile","m", time()+3600, "/");
are you placing it above the html tag? above the head tag? in the head tag? or body? i think it matters where i'm placing this to make it work.... cheers
-
-
-
code-96
Jan 9, 2012 @ 5:15 pm | delete
- I am putting at the very top of the page, no white space before the opening php tag... Also the closing php tag is placed directly above your opening html tag, again no white space. If you look at the image above the opening php code is line 1. Notice how there is no white space in between the php and html.
-
-
-
SproutCreatives
Jan 9, 2012 @ 6:40 pm | delete
- I can see your full site perfectly on my iPhone. I still am getting the header error on mine. Visit mobi.sproutcreatives(dot)com
-
-
-
SproutCreatives
Jan 9, 2012 @ 6:46 pm | delete
- Problem solved! I was putting the PHP code within the html tags. As soon as I read your other comment about placing it at the top. Worked! Thanks for the follow ups.
-
by code-96
Code 96 is a small Colorado web design firm that targets the small to medium-sized business owner.
- 1 featured lens
- Winner of 4 trophies!
- Top lens » PHP Mobile Redirect and View Full Site