dotMobimobiThinkingmobiForgemobiReadyDeviceAtlasgoMobi

Posted by warsteiner21 3 years 49 weeks ago

pic
 warsteiner21
mobiForge Enthusiast
Posts: 14
Joined: 5 years ago
[offline]

I would like to use Device Atlas to be able to detect the iPhone and then re-direct to an iPhone specific site. If the device is not an iPhone I would like to deliver the standard mobile page.
I have the following code at the top of the standard mobile page:

 
   <?php
	echo '<?xml version="1.0" encoding="utf-8"?>';
        require_once 'da_api.php';
        $tree = da_get_tree_from_file('json/DeviceAtlas.json');
         $ua = $_SERVER['HTTP_USER_AGENT'];
 
                       if(da_get_property($tree, $ua, model) == 'iPhone')
            		{ header('Location: http://mysite.mobi/iphone/'); }	
 
    ?>

Is this the correct use of Device Atlas for this purpose?
Is an 'else' statement needed to deliver the standard (current) mobile page if the device is not an iPhone?
If so, what should that 'else' statement contain?

I have also found the following PHP detection/re-direction, not using Device Atlas

<?php 
if (strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone') !== FALSE) 
{ header('Location: http://www.expressionindesign.com/iphone/'); } 
?>

Is there any disadvantage to using the 2nd method?

Any feedback would be appreciated.
Thanks

Posted by daniel.hunt 3 years ago

pic
 daniel.hunt
dotMobi logo
Mobile Grandmaster
Posts: 230
Joined: 4 years ago
[offline]

Your PHP only solution will, of course, work at the moment. But what would happen if, in the future, the iPhone UA string changed slightly?
A change such as changing "iPhone" to "apple3rdgen" or something like that would mean yuor site would no longer work.

Admittedly, such a change is unlikely, but the reason DeviceAtlas exists is so that developers such as ourselves don't need to know anything about user agents. All we need to care about is devices. The iPhone itself will always be the iPhone :)

As for your original question, whenever you do a PHP Header Location, you must die() after it, to prevent the rest of the page from being executed.
A popular way to do this is to use a wrapper function so that you never forget:

/**
 * @param string $location The URL to redirect to. Only requires http://DOMAIN.COM/ if you are redirecting *away* from your current domain
 */
function my_redirect($location) {
  $location = trim($location);
  if(strlen($location)) {
    header("Location: $location");
    die();
  }
  //return false if $location is an empty string (redirect failed)
  return false;
}
 
$tree = da_get_tree_from_file('json/DeviceAtlas.json');
$ua = $_SERVER['HTTP_USER_AGENT']; 
 
if(strtolower(da_get_property($tree, $ua, model)) == 'iphone') {
  my_redirect('/iphone/');
}
Daniel Hunt
dotMobi

Posted by warsteiner21 3 years ago

pic
 warsteiner21
mobiForge Enthusiast
Posts: 14
Joined: 5 years ago
[offline]

Thank you for your help Daniel.
Can you please confirm that, if the page is accessed by a device other than the iPhone, the my_redirect function will be ignored and the rest of the code will be executed?
Also, is the correct location for this code at the top of the page, above the 1st head tag, as follows:

   <?php
        echo '<?xml version="1.0" encoding="utf-8"?>';
        require_once 'da_api.php';
        $tree = da_get_tree_from_file('json/DeviceAtlas.json');
         $ua = $_SERVER['HTTP_USER_AGENT'];
 
/**
 * @param string $location The URL to redirect to. Only requires http://DOMAIN.COM/ if you are redirecting *away* from your current domain
 */
function my_redirect($location) {
  $location = trim($location);
  if(strlen($location)) {
    header("Location: $location");
    die();
  }
  //return false if $location is an empty string (redirect failed)
  return false;
}
 
$tree = da_get_tree_from_file('json/DeviceAtlas.json');
$ua = $_SERVER['HTTP_USER_AGENT'];
 
if(strtolower(da_get_property($tree, $ua, model)) == 'iphone') {
  my_redirect('/iphone/');
}
?>
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head> etc, etc

Posted by daniel.hunt 3 years ago

pic
 daniel.hunt
dotMobi logo
Mobile Grandmaster
Posts: 230
Joined: 4 years ago
[offline]

Sorry for the delayed response, this seems to have slipped through my net!

A quick glance tells me that what you're trying to do won't work!
If you want to do *ANY* redirecting, you cannot output any content before the Header() function is called.

Hang on while I modify your code slightly.

*edit*

<?php
require_once 'da_api.php';
$tree = da_get_tree_from_file('json/DeviceAtlas.json');
$ua = $_SERVER['HTTP_USER_AGENT'];
 
/**
 * @param string $location The URL to redirect to. Only requires http://DOMAIN.COM/ if you are redirecting *away* from your current domain
 */
function my_redirect($location) {
  $location = trim($location);
  if(strlen($location)) {
    header("Location: $location");
    die();
  }
  //return false if $location is an empty string (redirect failed)
  return false;
}
 
$tree = da_get_tree_from_file('json/DeviceAtlas.json');
$ua = $_SERVER['HTTP_USER_AGENT'];
 
if(strtolower(da_get_property($tree, $ua, model)) == 'iphone') {
  my_redirect('/iphone/');
}
 
echo '<?xml version="1.0" encoding="utf-8"?>';
?>
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head> etc, etc

That slight change should work. Also remember that the API example I've given you isn't actually the most up to date usage. The new API is written with PHP5 OO code!

Have you implemented it yet? How have you been getting on with DeviceAtlas?

Daniel Hunt
dotMobi

Daniel Hunt
dotMobi

Posted by abbyeagle 1 year ago

pic
 abbyeagle
mobiForge Newbie
Posts: 1
Joined: 1 year ago
[offline]

Hi Daniel, how would you modify the code to redirect from

http://www.abbyeagle.com/nlp-hypnotherapy/weight-loss.php

to

http://www.m.abbyeagle.com/nlp-hypnotherapy/weight-loss.php

Where would i enter these paths?

NLP Hypnotherapy Gold Coast