iphone WD app...

Don’t mean to revive an old topic but I want to say thanks for a great mobile template. I implemented it and also hashed together some code to check for a mobile browser and auto-redirect to the mobile page, with the option to return to the desktop version, if desired.

The code to perform the checking is done in php and was found on the net at http://detectmobilebrowsers.mobi/ This site offers code at no cost for non-commercial use.

Additional code to create and test for cookies was then added to check to see if the mobile visitor had already been to the site and requested to use the desktop version rather than the mobile version.

So, how do you do it?

  1. First, create your mobile.php file using the information in this thread (or use any mobile page you want!). Once you have the mobile page working to your satisfaction you can move on to the next step. I renamed iphone.php to mobile.php because I use it for all smartphone and mobile browsers.

  2. Next, add the code from detectmobilebrowsers.mobi to your main entry web page, or better yet, to the top of every page (very easy if you have a template that every page uses). Here is how every page on my site begins:

<?php
include('mobile_device_detect.php');
if (isset($_COOKIE["DWCmobile"]))
  echo "";
else
mobile_device_detect(true,true,true,true,true,true,'http://deltaweathercam.com/mobile.php',false);
?>

Note that the code for which devices are selected as mobile can be changed by altering the ‘true’ values. See the .mobi site for details, its easy. Mine is set to route all mobiles to the mobile page.

Also note that the mobile detection only gets invoked in the absence of a cookie which is set when a visitor hits the mobile page. This results in all mobile visitors going to the mobile page first, but then allows them to ask for the desktop version from there if desired. The cookie allows them to bypass the mobile check but it expires in an hour which means the next visit they start again at the mobile page. For the purists, the echo “” is a leftover from some testing, and is there in the event you want to send a blurb to mobile users.

  1. The mobile page also needs some code to create the cookie. This is at the top of my mobile.php page:
<?php
setcookie("DWCmobile", "Been There", time()+3600);
?>

The cookie name DWCmobile should be changed to something relevant to your site, but also remember to change the cookie reference in the code at the top of each page in previous code callout! The text Been There is not significant, it is just what the cookie contains. You can also change the expiration time which is currently set to the current time plus 3600 seconds (1 hour).

  1. Finally, I added a small link at the very top left corner of my desktop page that links to the mobile page, and a link on the mobile page that goes to the desktop. You can see these links at http://deltaweathercam.com/index.php and http://deltaweathercam.com/mobile.php. Don’t be fooled by the width of the mobile page, it is elastic and will shrink in width as needed.

The most common problem encountered after implementing this type of check for the user agent is a message similar to:

Warning: Cannot modify header information - headers already sent by (output started at /path/to/yourfilename.php:#) in /path/to/mobile_device_detect.php on line x

This is because your page is sending ‘something’ before calling the file. There are several solutions but one of the easiest is to configure your php to enable output buffering. Normally, session, cookie or HTTP header data in a PHP script must be sent before any output is generated by the script. If this is not possible in your application, you can enable what PHP calls output buffering, with the output_buffering variable. With output buffering on, PHP stores the output of your script in a special memory buffer and sends it only when explicitly told to do so. This allows you to send special HTTP headers and cookie data even in the middle or at the end of your script. Use of output buffering can slightly degrade performance but only on a very busy site. The setting in php.ini is: output_buffering = Off

That’s about all there is to it. I’d love to see of the experts here make some changes and improvements. Perhaps add an option for a mobile visitor to set a permanent cookie so they never go to the mobile page, or even a configurable one.

I had to make some adjustments to my web site because the iPad is detected and treated as an iPhone/iPod by many site detection checkers, including this one. Seems that although it specifically says iPad, it still contains the word ‘mobile’.

Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.10

The solution isn’t especially hard to code, but I suspect it will be overlooked by many sites for a while. My feeling is that is should NOT be treated as a mobile device since most sites render better in full mode. I have made additions to my checking algorithm to specifically use the standard mode unless mobile/iPhone rendering is manually requested.

I added the an additional ‘case’ code as the first check in mobile_device_detect.php:


... function mobile_device_detect($iphone=true,$android=true,$opera=true,$blackberry=true,$palm=true,$windows=true,$mobileredirect=false,$desktopredirect=false){

$mobile_browser = false; // set mobile browser as false till we can prove otherwise
$user_agent = $_SERVER[‘HTTP_USER_AGENT’]; // get the user agent value - this should be cleaned to ensure no nefarious input gets executed
$accept = $_SERVER[‘HTTP_ACCEPT’]; // get the content accept value - this should be cleaned to ensure no nefarious input gets executed

switch(true){ // using a switch against the following statements which could return true is more efficient than the previous method of using if statements

case (eregi(‘ipad’,$user_agent)); // we find the words ipad in the user agent
$mobile_browser = false; // set mobile browser to false
break; // break out and skip the rest if we’ve had a match on the ipad

case (eregi(‘ipod’,$user_agent)||eregi(‘iphone’,$user_agent)); // we find the words iphone or ipod in the user agent
$mobile_browser = $iphone; // mobile browser is either true or false depending on the setting of iphone when calling the function
if(substr($iphone,0,4)==‘http’){ // does the value of iphone resemble a url
$mobileredirect = $iphone; // set the mobile redirect url to the url value stored in the iphone value
} // ends the if for iphone being a url
break; // break out and skip the rest if we’ve had a match on the iphone or ipod


After using the iPad for a few weeks now I am convinced that there is no reason to treat it as a mobile browser, despite the fact that it contains the word mobile in its user agent string.