1

I'm looking for a way to redirect mobile users to a mobile landing page (and I'm not looking for a theme switcher). I have a specific page that I'd like to redirect mobile users to: www.mysite.com/m/

Is there a way to utilize Mobile_Detect.php and specify a single page as the redirect target?

2 Answers 2

3

I suppose if you really wanted to, you could do something along the lines of this in a custom module:

function YOURMODULE_init() {

  if ($_SERVER['REQUEST_METHOD']=='GET' // most ajax/ahah etc use POST
      && arg(0)!='admin' // admin theme/backend
      && strpos($_GET['q'], 'sites/default/files')===FALSE // are we looking at anything under files that doesn't "really" exist, eg imagecaches that aren't created yet
      && $_GET['q']!='m') { // aren't already "here"

    include 'PATHTOSCRIPT/Mobile_Detect.php'; 
    $detect = new Mobile_Detect();

    if ($detect->isMobile()) {
      drupal_goto('m');
    }

  }

}

but they could never leave, or rather, be able to go to any other page on your site.

Addition: per @MPD's comments, some further protections.

4
  • 1
    I heard some performance issues about hook_init what you think ?
    – niksmac
    Commented Dec 19, 2012 at 19:19
  • what performance issues have you heard about or are concerned with? I've seen talk here ( drupal.stackexchange.com/questions/36265/… ) about some issues but none too too stressful. Granted, hook_init() is run "last" so I suppose you could put this somewhere deeper/earlier into the stack, but, for what the user asked, I don't see that extra/double drupal bootstrapping to be a really big issue. Opinions mine of course. (And this is a quick and dirty solution as well :)
    – Jimajamma
    Commented Dec 19, 2012 at 19:35
  • @Jimajamma thanks for that snippet. I don't suppose there is a way around this? Commented Dec 19, 2012 at 19:56
  • 2
    hook_init() runs on every request, including imagecache renders, AJAX, admin backend, etc. Drupal 7 offers better alternatives for a lot of what we used hook_init() for in Drupal 6. In this case, I don't think there are any better hooks you can use. Just keep in mind that you may been some additional logic to prevent the redirection on your imagecahce, AJAX, etc if you need them.
    – mpdonadio
    Commented Dec 19, 2012 at 20:13
1

My preference when doing mobile redirection is to use a javascript solution instead of PHP. It doesn't impact the load times significantly, and can easily be adjusted for more complex logic if required (user-agent, platform, etc).

<script type="text/javascript">
if ( (screen.width < 1024) && (screen.height < 768) ) { 
  window.location = 'http://www.mysite.com/m/';
}
</script>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.