Redirect if handheld device

Permalink 2 users found helpful
Hi all...

First off, I have seen the C5Touch tutorial and although fantastic (which it is), I have a project which requires actual redirection to a new C5 site designed specifically for Handheld devices.

The theme isn't the issue here, the clients wants a very small amount of reference info on the iphone version while the full site has a mountain of content.

Has anyone managed to get redirection of handheld devices to work and if so, any chance of a little help?

I am a PHP dud so please be forgiving. :-)

 
teknojunkey replied on at Permalink Reply
teknojunkey
i have been using this ...

http://detectmobilebrowser.com/...

hope it helps
mobius2000 replied on at Permalink Reply
Thanks heaps... This seems pretty cool.

As I said, i am a dud at PHP, do I just drop the generated PHP code into my header.php within my themes elements folder (obviously after changing the redirect address)?

Thanks again for pointing this out.
synlag replied on at Permalink Best Answer Reply
synlag
via
http://www.russellbeattie.com/blog/mobile-browser-detection-in-php...

helper comes in compendium theme.

<?php
defined('C5_EXECUTE') or die(_("Access Denied."));
/* mobile browser detection byhttp://www.russellbeattie.com/blog/mobile-browser-detection-in-php... */
class AgentDetectionHelper {
   var $isMobile = false;
   var $isBot = false;
   var $op = strtolower($_SERVER['HTTP_X_OPERAMINI_PHONE']);
   var $ua = strtolower($_SERVER['HTTP_USER_AGENT']);
   var $ac = strtolower($_SERVER['HTTP_ACCEPT']);
   var $ip = $_SERVER['REMOTE_ADDR'];
   public static function getAgent(){
      return $this->$isMobile = strpos($this->$ac, 'application/vnd.wap.xhtml+xml') !== false
         || $this->$op != ''
         || strpos($this->ua, 'sony') !== false 
         || strpos($this->ua, 'symbian') !== false
andreyman3d2k replied on at Permalink Reply
Hi,

I am also looking to perform a redirect for mobile, and this seems to be the most promising thread I found. I am pretty soft when it comes to PHP, and looking for some guidance on how to implement this code. What file(s) to I put it into, and where do I enter the URLs for redirect?

Any help at all would be greatly appreciated. Thank you!

Andrey
jordanlev replied on at Permalink Reply
jordanlev
That code is weird... I think what you do is copy all of that code above (make sure you click the "View entire code block" link to see the whole thing), paste it into a new file called "agent_detection.php" and save that file into your site's "helpers" directory. Then at the top of your theme templates (or in your header element), put this code:
<?php
$ah = Loader::helper('agent_detection');
$ah->getAgent();
?>

...then further down in your template whenever you want to have something that's different for mobile, do this:
<?php if ($ah->isMobile): ?>
   <!--Put your mobile markup here -->
<?php else: ?>
   <!--Put your normal markup here -->
<?php endif; ?>
jordanlev replied on at Permalink Reply
jordanlev
Oops, just saw that you wanted to redirect for mobile. If that's the case, then you need to put this code at the very top of your theme header (it must be the absolute first thing -- it can't be after the doctype and there can't even be any spaces before it otherwise you'll get errors):
<?php
$ah = Loader::helper('agent_detection');
$ah->getAgent();
if ($ah->isMobile) {
   header('Location:http://example.com');
   die();
}
?>
andreyman3d2k replied on at Permalink Reply
Hi,

I just tried this, and got this error when I load the page:

Parse error: syntax error, unexpected '(', expecting ',' or ';' in [my site path] /helpers/agent_detection.php on line 12

Andrey
Mnkras replied on at Permalink Reply
Mnkras
take a look at theme_options_lite, it has a mobile browser detection
andreyman3d2k replied on at Permalink Reply
Thanks, Mnkras -- I am not looking to change theme, but to redirect... I'm sure there's a way to re-purpose the code from the add-on for redirection, but it's a bit beyond my skill level with php.

Andrey
Mnkras replied on at Permalink Reply
Mnkras
take out the file in the libraries folder, thats the code that does the checking
andreyman3d2k replied on at Permalink Reply
I found it -- what do I do with it?
Mnkras replied on at Permalink Reply
Mnkras
Loader::library('Browser');
$browser = new Browser();
if($browser->isMobile()) {
//mobile
}
andreyman3d2k replied on at Permalink Reply
I see - the Browser.php has to be placed into the main libraries folder, and your snippet goes into the header. Works now. Thank you!
andreyman3d2k replied on at Permalink Reply
Aha, I figured it out -- thanks. One question: this code seems to be much longer than the code in the preceding post -- does it significantly slow down page loading?
Mnkras replied on at Permalink Reply
Mnkras
There should be no noticeable difference, (the code above will not work,
vars cannot have functions in them)
On Oct 8, 2011 11:09 PM, "Concrete5 Community" <discussions@concretecms.com>
wrote:
andreyman3d2k replied on at Permalink Reply
Thanks, Mnkras - one more question:

I would like to have an option on the bottom of the mobile site to 'View full site' -- however if I link it to the homepage, it will just perform the detection again and re-direct to mobile version. Is there any way around that? Thank you!

Andrey
Mnkras replied on at Permalink Reply
Mnkras
Theme Options Lite has that built in.

by simply adding ?site=full or ?site=mobile it will force the webpage to display and save the selection (full or mobile)
andreyman3d2k replied on at Permalink Reply
So you mean if I dohttp://www.example.com/?site=mobile... it should redirect to the mobile site?

This isn't working for me... (nor the other way around, from mobile to full). Did I implement incorrectly? I just placed the browser.php into the libraries folder to get the detection to work.

Thanks,

Andrey
andreyman3d2k replied on at Permalink Reply
Hi Mnkras, still struggling with this, would be grateful for any guidance. In the models folder for Theme Options Lite I found a file called mobile_switcher.php, which contans the function SetMobile -- which appears to do what you described. This file appears to be called by the controller.php file. However, I cannot grasp how/what/where to move this functionality to my theme, so it will work with the mobile detection implemented per our above conversation.

Again, any help appreciated, and very thankful for your guidance so far.

Andrey
andreyman3d2k replied on at Permalink Reply
Hi Mnkras,

I hope you don't mind if I bug you one last time - I got everything working, including the mobile detection, and the mobile/full redirect. There's one thing I've been completely struggling with though: I would like to have the iPad go to the full version of the site, not the mobile. I've tried editing the code everywhere the ipad is mentioned in the detection file, but it still goes to mobile. Do you by any chance have any ideas on this?

Thanks for all your help,

Andrey
Mnkras replied on at Permalink Reply
Mnkras
If you change/remove the relevant code it should work fine, im not sure. Have you cleared the cache? also, on the iPad go to ?site=unset or clear your browser's cookies
MrNiceGaius replied on at Permalink Reply
MrNiceGaius
The AgentDectectorHelper code (posted above) is weird... I've managed to get it working.

The parse error (~line12) was being caused by the $_SERVER('HTTP_X_OPERA_SOMETHING') not being set. I also added some additional user agents strings iphone, ipod, android ( ipad excluded ).

<?php
defined('C5_EXECUTE') or die(_("Access Denied."));
class AgentDetectionHelper{
   var $isMobile = false;
   var $isBot = false;
   var  $op // http operamini phone
      ,$ua // http user agent
      ,$ac // http accept
      ,$ip // remote IP address
      ;
      function AgentDetectionHelper(){
      $this->op = (isset($_SERVER['HTTP_X_OPERAMINI_PHONE'])) ? strtolower( $_SERVER['HTTP_X_OPERAMINI_PHONE']) : null;
         $this->ua = (isset($_SERVER['HTTP_USER_AGENT'])) ? strtolower( $_SERVER['HTTP_USER_AGENT']) : null;
         $this->ac = (isset($_SERVER['HTTP_ACCEPT'])) ? strtolower( $_SERVER['HTTP_ACCEPT']) : null;
         $this->ip = (isset($_SERVER['REMOTE_ADDR'])) ? $_SERVER['REMOTE_ADDR'] : null;


... just tested and is working ...
Nornik replied on at Permalink Reply
There are alot of great replies in this post. I have done this and wanted to give you another option. This option uses loaded themes and the TeraWurfl project for the phone database and the device recognition. The code may be abit sloppy but I was in a hurry and it works fine.

1) Download TerWurfl from sourceforge and setup the database
2) In the root Library Folder of your C5 install put the TerWurfl folder in that folder but name it mobi (should be /library/mobi).
3) In the root Library folder add a file named theme_switcher.php and that file will be coded like so
<?php 
defined('C5_EXECUTE') or die(_("Access Denied."));
class ThemeSwitcher {
public function mobicheck($view) {
// Create sessions for full, mobile and mobile to full
if ($_GET["m"] == "f") {
session_start();
$_SESSION['mobi'] = 'full';
}else{
if ($_GET["m"] == "m") {
session_start();
$_SESSION['mobi'] = 'mobi'; }}
if ($_SESSION['mobi'] !== "full") {
// Include the Tera-WURFL file
require_once('mobi/TeraWurfl.php');

4) In the root config folder create a site_events.php file. The code for that will be
<?php 
Events::extend('on_start', 'ThemeSwitcher', 'mobicheck', 'libraries/theme_switcher.php');
?>

5) In the site.php file add
define('ENABLE_APPLICATION_EVENTS', true);


Make sure the mobile theme(s) you want are installed (but not activated) and the script will call the theme based on what you set it to. You can do multi themes (ex iphone, PDA and/or WAP) if you want. We set up 1 wurfl database and use the same one for everyone. Much easier to update 1 database. Just make sure the TeraWurlConfig.php file has the right database info.

Hopefully this helps and hopefully I didn't forget anything. Sorry been on a 36hr code binge so if I missed something, just let me know.
Enlive replied on at Permalink Reply
Enlive
Does this work with full page caching?
FatTony1952 replied on at Permalink Reply
FatTony1952
I have something to add to this discussion. I have a site that uses the YouTube Background and it screws up on Android devices. I have been using the old fashion trial and error messages to get it to disable the video when a mobile browser is detected.

I have been unsuccessful to this point, but this thread has gotten me to the point where I don't get any more errors, but the video doesn't disable.

I have asked this same question in the Support for that package, but this solution may work for other packages as well.
Nornik replied on at Permalink Reply
I would give that a custom area name and exclude it in the mobile theme. That would be the easiest route. You could add some custom coding to the player package to not output if mobile is true. That is if you are using a package to render the video.
FatTony1952 replied on at Permalink Reply
FatTony1952
I actually have it figured out now. I'll post this in the YouTube Background forum as well, but here is the new view.php code for that package.

<?php  
defined('C5_EXECUTE') or die(_("Access Denied."));
$url = parse_url($videoURL);
parse_str($url['query'], $query);
$c = Page::getCurrentPage();
$vWidth=425;
$vHeight=100;
class AgentDetectionHelper{
   var $isMobile = false;
   var $isBot = false;
   var  $op // http operamini phone
      ,$ua // http user agent
      ,$ac // http accept
      ,$ip // remote IP address
      ;


You will want to input whatever mobile device you're coding for on this line (which is line 106 in my file)

if($isMobile='android'){
Malko replied on at Permalink Reply
Hi everyone!
I have the same issue and im looking for a help please!

I will use a mobile site which is a summary of my regualr site for my mobile users cleints where they can directly find the needed info. So, with the mobile site's theme come already the instrucion codes to redirect it at the mobile users. So i need to insert the redirection script on my regular's root site "index.php" but i cannont find it? I want my website as thishttp://www.myC5website.com/mobile... on the mobile subdomain comes my mobile's site.
how can i do it please?
im stuck right there. Well, i don't need a responsive theme cause it's another story for what i meant.
Hope to hear from you soon

Best!

Malko