简体   繁体   中英

Is it possible to serve top navi only in mobile?

using mobile detection (js or php), is it possible to display top navi made for mobile only? also i see a lot of mobile detection in php - where is it supposed to be placed?

{php}
function isMobileBrowser($user_agent = '') {
  foreach (array('iPhone','Android','Windows CE', 'PPC', 'Smartphone', 'IEMobile', 'Opera Mini') as $mobile_browser_ua_snippet) {
    if (stristr($user_agent, $mobile_browser_ua_snippet)) {
      return true;
    }
  }
  return false;
}
{/php}

{if isMobileBrowser($_SERVER['HTTP_USER_AGENT'])} 
{include file="templates/layouts/zones/mobileMenu.html"}
{/if}

now i got error: syntax error: unbalanced parenthesis in if statement => I am not sure abt that

Your options are:

1) Execute the PHP Mobile Detection within each PHP script/page served (and/or store the result in a Session Variable, so the Detection need only run once per visit). Use the results to switch on/off parts of the markup you render (ie if the visitor is using a mobile device, then set $isMobile to true , and when you are creating the HTML, wrap the "top navi" section in a PHP conditional of if( $isMobile==true ){ ... "top navi" ... } .

2) If your host allows it, create a subdomain like m.server.com (where "server.com" is your domain). You can then either use a .htaccess rule to force any mobile devices to the "m.*" subdomain, or, again, use PHP to do the same. Within that subdomain, have all of the same pages/content but optimised for a mobile device browser.

3) Use a CSS Media Queries to make the "top navi" element visible. Using Mobile Specific HTML, CSS & Javascript

Of course, yes.

I'd prefer to create a function:

function isMobileBrowser($user_agent = '') {
  foreach (array('Windows CE', 'PPC', 'Smartphone', 'IEMobile', 'Opera Mini') as $mobile_browser_ua_snippet) {
    if (stristr($user_agent, $mobile_browser_ua_snippet)) {
      return true;
    }
  }
  return false;
}

You suggest to extract array of substrings of mobile browsers user agents from the foreach Then, in your code, depending on what do you use to output HTML, you do a simple condition like:

if (isMobileBrowser($_SERVER['HTTP_USER_AGENT'])) {
include 'mobile_navi.inc.php';
}

I'd also suggest to create mobile version of your whole website. Let's say your sites domain name is mysite.com, you create mobile version at m.website.com and then you just redirect your mobile visitors to m.website.com just like this:

// THIS CODE SHOULD REMAIN **BEFORE** ANY OUTPUT!!!
if (isMobileBrowser($_SERVER['HTTP_USER_AGENT'])) {
  header('Location: http://m.website.com'.$_SERVER['REQUEST_URI']);
}

Or you can do it in apache's mod_rewrite (You will have as many RewriteConds as mobile brosers user agents substrings). I believe it's able to be done in nginx configuration file as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM