Jump to content



Latest News: (loading..)

- - - - -

mobile site direct help


  • Please log in to reply
5 replies to this topic

#1   ken0306

ken0306
  • Members
  • 173 posts
  • Real Name:Ken0306
  • Gender:Male

Posted 27 July 2012 - 11:36 PM

Hi, I want to direct my normal site to a mobile site when people using mobile phone surfing my site.

here is the imobile script

<?php
/*
isMobile.php
A PHP script to detect mobile phones.
v1.1 :: 23 September 2008
Check out my blog
http://www.growlingranger.com
Or subscribe to my RSS feed
http://www.growlingranger.com/?feed=rss2
*/
function isMobile() {
// Check the server headers to see if they're mobile friendly
if(isset($_SERVER["HTTP_X_WAP_PROFILE"])) {
  return true;
}
// If the http_accept header supports wap then it's a mobile too
if(preg_match("/wap\.|\.wap/i",$_SERVER["HTTP_ACCEPT"])) {
  return true;
}
// Still no luck? Let's have a look at the user agent on the browser. If it contains
// any of the following, it's probably a mobile device. Kappow!
if(isset($_SERVER["HTTP_USER_AGENT"])){
  $user_agents = array("midp", "j2me", "avantg", "docomo", "novarra", "palmos", "palmsource", "240x320", "opwv", "chtml", "pda", "windows\ ce", "mmp\/", "blackberry", "mib\/", "symbian", "wireless", "nokia", "hand", "mobi", "phone", "cdm", "up\.b", "audio", "SIE\-", "SEC\-", "samsung", "HTC", "mot\-", "mitsu", "sagem", "sony", "alcatel", "lg", "erics", "vx", "NEC", "philips", "mmm", "xx", "panasonic", "sharp", "wap", "sch", "rover", "pocket", "benq", "java", "pt", "pg", "vox", "amoi", "bird", "compal", "kg", "voda", "sany", "kdd", "dbt", "sendo", "sgh", "gradi", "jb", "\d\d\di", "moto");
  foreach($user_agents as $user_string){
   if(preg_match("/".$user_string."/i",$_SERVER["HTTP_USER_AGENT"])) {
	return true;
   }
  }
}

// None of the above? Then it's probably not a mobile device.
return false;
}
// Here's our code to which calls the above function
if (isMobile()) {
// if the function returned true, it's a mobile.
// echo "mobile"; // delete this line in your code, and uncomment the next line
//  include_once(DIR_WS_CLASSES . 'javapopup.php');
header('Location: /mysite/mobile/'); // let's redirect the page to the mobile site
} else {
 header('Location: /');
}
// if not, we're in a standard browser and our page
// can proceed as normally formatted for the web!
// echo "web"; // delete this line in your code
// header('Location: ../'); // let's redirect the page to the mobile site
?>

I using the iMoblie script to run it, and it works fine, it will direct correctly, how ever, I also want my customer be able to go back to normal site to surfing( because some function my mobile site doesn`t has it).

I add this file under application_top.php
include_once(DIR_WS_CLASSES . 'isMobile.php');

now it will direct mobile site all the time. I was thinking about let the script expire each session, but it looks like does not really work, because the mobile phone browser does not really close each time you close the page.

Any ideal how to make it happen?

Thank you
ken

#2   ecartz

ecartz
  • Community Sponsor
  • 1,956 posts
  • Real Name:Matt
  • Gender:Male

Posted 28 July 2012 - 04:45 PM

How do you know what pages you want to be on the regular site?

One possibility would be to take that code out of application_top.php and put it in some pages but not others.  Another possibility would be to have it check something else when it does the isMobile()  check.  A third possibility would be to change your osCommerce site to be more mobile friendly so that you don't have to go to a separate site.

Incidentally, that code does not look correct.  If not mobile, it looks like it redirects the user from the requested page to the index page.
Always backup before making changes.

#3   MrPhil

MrPhil
  • Members
  • 4,138 posts
  • Real Name:Phil
  • Gender:Male

Posted 28 July 2012 - 05:52 PM

I just googled for htaccess mobile, and found much discussion on using .htaccess to switch between mobile and non-mobile domains and subdomains (two distinct osC implementations would be needed). See especially http://stackoverflow.com/questions/3680463/mobile-redirect-using-htaccess . That might be cleaner than trying to switch within PHP. You already seem to be headed in the direction of two distinct osC installations, one customized for mobile usage.

If you want a customer who would otherwise automatically be sent to the mobile site to force their browser to the main site, they could add something to the URL that would be sensed in .htaccess and suppress the mobile check. Perhaps something like &mobile=no added to the URL, which could be checked something like
RewriteCond  %{QUERY_STRING}  !mobile=no   [NC]
at the top of the RewriteCond checks to put it into the mobile version. Of course, once the visitor has specified they don't want mobile, that flag should be automatically added to any link addresses created by osC, so that visits to other pages will keep going to the main site. That would mean modifying osC code to always build the Query String or SEO-friendly URL with mobile=no if that has been specified.

Anyway, that's one way of doing it that would probably work. There are surely many other ways to do it (session information, cookies), but that should get you started.

#4 ONLINE   bruyndoncx

bruyndoncx

    Problem Thinker

  • Members
  • 2,657 posts
  • Real Name:Carine Bruyndoncx
  • Gender:Female
  • Location:Belgium/ Antwerp/ Turnhout/ Arendonk

Posted 28 July 2012 - 10:08 PM

I've come across a script last week that detects if mobile,it sets a cookie, and another cookie when the full site is requested from the mobile device.

Edit: found it
http://www.xpert.net/knowledgebase.php?action=displayarticle&id=2

Edited by bruyndoncx, 28 July 2012 - 10:10 PM.

Hava a nice day !
Carine Bruyndoncx


PS frustraded by the lack of feedback
<!--
Did you know 99% of all people benefiting from my posts, won't bother to repay the favor.
Wouldn't it be great if you are part of that exclusive 1% ?
post your findings in my responsive liive shop review thread ?
-->

#5   ken0306

ken0306
  • Members
  • 173 posts
  • Real Name:Ken0306
  • Gender:Male

Posted 30 July 2012 - 05:12 PM

cool, I will go try on these code and post back the result. thank you for all your guys help.

#6   mattjt83

mattjt83
  • Partner
  • 247 posts
  • Real Name:Matt Toste
  • Gender:Male
  • Location:CA, USA

Posted 11 April 2013 - 09:43 PM

@ken0306

Better to build a responsive website which is more future proof as well as gives you just one fileset to maintain.