Detecting Mobile Browsers Part 2
Dec 5th, 2007 by Greg Bulmash
While a lot of people have written in with kudos on my PHP code for detecting mobile browsers, others have written in with questions on how to use it.
The code in question is a PHP function that returns a true or false value depending on whether the browser viewing the page has been identified as mobile or not. If it's identified as mobile, it returns a "true" status. If it's not, it returns a "false" status. So, if you use the code exactly as presented in the original article, you're going to put it somewhere near the end of the PHP code block in your page. Somewhere near the beginning of the page, you'll want a PHP code block like this...
if(checkmobile()){
//code to execute if a mobile browser is detected
}
Now what code you'll execute is up to you. It might be that you simply select a mobile style sheet instead of your regular one. To do that, you'd do something like...
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<HEAD>
<TITLE>My Page Title</TITLE>
<?php
if(checkmobile()){
echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"/mobilestyle.css\">";
} else {
echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"/regularstyle.css\">";
}
?>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
...
Now remember, this assumes you've put the mobile browser detection code somewhere in the page between <?PHP ... ?> tags.
Some of you, on the other hand, may want to send mobile browsers over to an entirely different version of your site. The simplest way is to do...
<?php
if(checkmobile()) header("Location:http://www.whatever-url-you-want.com");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<HTML>
...
Note that this is at the VERY VERY top of the page, before anything else. That's because, you need to do the redirect before any other information has been sent to the user's browser.
You can put the code for catching mobile browsers right in that PHP block, after the "if(checkmobile())" line and before the "?>" line. I haven't included it in the examples just to keep this page from being humongously long.
Depending on how your server is set up, any file that contains PHP may require that the file name end in .php instead of .htm or .html. So make sure you know what your server requires.
Hope this helps some of you who aren't familiar with programming. If you need further help, I suggest buying a book on PHP if you're in a DIY mood or hiring someone who knows it if you're not.
[...] UPDATE #2: Need help using the code in this article? I've written a second article with code to help you handle your mobile users when this piece of code detects them. [...]
You might want to put an exit(); after the header(...); line, so that the page stops sending needlessly after the redirect is sent. Like so:
if(checkmobile())
{
header("Location:http://www.whatever-url-you-want.com");
exit();
}
// etc.
Regarding the exit() suggestion, w3c recommendations are that you include a little markup that provides a 'Click here' link in case the redirect fails to work, so perhaps a conditional bit of XHTML would be a better option.
This is a great script, but what if I STILL want to direct iPhone users to another site (and not the mobile site), say iphone.mysite.com where all other mobiles are directed to mobile.mysite.com. (and desktop browsers still bypass and go to http://www.mysite.com)
Thx
Nick
Right now the checkmobile() function returns "true" or "false". It would have to be rejiggered to return an A/B/C option (mobile, non-mobile, iphone). Then instead of if(checkmobile()), you'd have if (checkmobile()=="iphone") or something to that extent so that you could execute different code.
Hi,
I have the same question as Nick Fox. What's the entire PHP code that I'd need to add to add to allow desktop browsers through, but to redirect iPhone users to one page, and mobile users to another? Any help would be really appreciated.
Thanks!