Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. Linux, Apache, MySQL, PHP
  4. best way to determine mobile device

best way to determine mobile device

Scheduled Pinned Locked Moved Linux, Apache, MySQL, PHP
question
10 Posts 4 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jassim Rahma
    wrote on last edited by
    #1

    Hi, What's the best way to determine the mobile device then redirect to the /mobile? Regards, Jassim

    L M 2 Replies Last reply
    0
    • J Jassim Rahma

      Hi, What's the best way to determine the mobile device then redirect to the /mobile? Regards, Jassim

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      As a start, read these, they should help you ... http://webdesignerwall.com/tutorials/css3-media-queries[^] http://webdesignerwall.com/tutorials/responsive-design-with-css3-media-queries[^] then, if necessary, do some Googling and further reading. [added] You could have accepted Graham's answer in the Web Dev forum

      1 Reply Last reply
      0
      • J Jassim Rahma

        Hi, What's the best way to determine the mobile device then redirect to the /mobile? Regards, Jassim

        M Offline
        M Offline
        Mohibur Rashid
        wrote on last edited by
        #3

        Detecting if its a mobile device Please follow the above link. It looks good

        J 1 Reply Last reply
        0
        • M Mohibur Rashid

          Detecting if its a mobile device Please follow the above link. It looks good

          J Offline
          J Offline
          Jassim Rahma
          wrote on last edited by
          #4

          Detecting if its a mobile device

          Please follow the above link. It looks good

          but how can I implement it? Do i have to put it in every page? is it possible to put it in one place for the entire application?

          G M 2 Replies Last reply
          0
          • J Jassim Rahma

            Detecting if its a mobile device

            Please follow the above link. It looks good

            but how can I implement it? Do i have to put it in every page? is it possible to put it in one place for the entire application?

            G Offline
            G Offline
            Gerben Jongerius
            wrote on last edited by
            #5

            You could do either one of the two. But I would suggest creating a generic php file with this piece of code in a method. Including that php file in every page and calling that method. EG: dection.php <-- contains the check + redirect Sample index.php:

            J 1 Reply Last reply
            0
            • J Jassim Rahma

              Detecting if its a mobile device

              Please follow the above link. It looks good

              but how can I implement it? Do i have to put it in every page? is it possible to put it in one place for the entire application?

              M Offline
              M Offline
              Mohibur Rashid
              wrote on last edited by
              #6

              Put it in the initial page. That is the first page user would open. such as index page or login page. If it figured out that the client device is mobile save that information to session. As a matter of fact its not a big deal. you dont even need to remember whether it is a mobile device or not. But If the set of functions are different for mobile and pc then you can use the session variable to determine about which function to call. more over when it would redirect to page allocated for mobile do not put any link to those mobile page that redirect to pc pages

              1 Reply Last reply
              0
              • G Gerben Jongerius

                You could do either one of the two. But I would suggest creating a generic php file with this piece of code in a method. Including that php file in every page and calling that method. EG: dection.php <-- contains the check + redirect Sample index.php:

                J Offline
                J Offline
                Jassim Rahma
                wrote on last edited by
                #7

                sorry but need more help plz that mentioned code will be placed in a separet file.. that's file.. but it will be between tage? right? then followed by the if($mobile === true)? so is the following code correct?:

                <?php
                function detect_mobile()
                {
                    $_SERVER['ALL_HTTP'] = isset($_SERVER['ALL_HTTP']) ? $_SERVER['ALL_HTTP'] : '';

                    $mobile\_browser = '0';
                
                    $agent = strtolower($\_SERVER\['HTTP\_USER\_AGENT'\]);
                
                    if(preg\_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|iphone|ipad|ipod|android|xoom)/i', $agent))
                        $mobile\_browser++;
                
                    if((isset($\_SERVER\['HTTP\_ACCEPT'\])) and (strpos(strtolower($\_SERVER\['HTTP\_ACCEPT'\]),'application/vnd.wap.xhtml+xml') !== false))
                        $mobile\_browser++;
                
                    if(isset($\_SERVER\['HTTP\_X\_WAP\_PROFILE'\]))
                        $mobile\_browser++;
                
                    if(isset($\_SERVER\['HTTP\_PROFILE'\]))
                        $mobile\_browser++;
                
                    $mobile\_ua = substr($agent,0,4);
                    $mobile\_agents = array(
                                        'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',
                                        'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',
                                        'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-',
                                        'maui','maxo'
                
                G 1 Reply Last reply
                0
                • J Jassim Rahma

                  sorry but need more help plz that mentioned code will be placed in a separet file.. that's file.. but it will be between tage? right? then followed by the if($mobile === true)? so is the following code correct?:

                  <?php
                  function detect_mobile()
                  {
                      $_SERVER['ALL_HTTP'] = isset($_SERVER['ALL_HTTP']) ? $_SERVER['ALL_HTTP'] : '';

                      $mobile\_browser = '0';
                  
                      $agent = strtolower($\_SERVER\['HTTP\_USER\_AGENT'\]);
                  
                      if(preg\_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|iphone|ipad|ipod|android|xoom)/i', $agent))
                          $mobile\_browser++;
                  
                      if((isset($\_SERVER\['HTTP\_ACCEPT'\])) and (strpos(strtolower($\_SERVER\['HTTP\_ACCEPT'\]),'application/vnd.wap.xhtml+xml') !== false))
                          $mobile\_browser++;
                  
                      if(isset($\_SERVER\['HTTP\_X\_WAP\_PROFILE'\]))
                          $mobile\_browser++;
                  
                      if(isset($\_SERVER\['HTTP\_PROFILE'\]))
                          $mobile\_browser++;
                  
                      $mobile\_ua = substr($agent,0,4);
                      $mobile\_agents = array(
                                          'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',
                                          'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',
                                          'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-',
                                          'maui','maxo'
                  
                  G Offline
                  G Offline
                  Gerben Jongerius
                  wrote on last edited by
                  #8

                  That would indeed work. If you put the entire function in the external php file and the code below in all files that need the check for mobile devices.

                  if (detect_mobile()) {
                  header('Location: blog');
                  }

                  Please note though that the redirect that you put in you code would not work as is. You would need to replace the 'blog' part with a URL to the mobile site.

                  J 2 Replies Last reply
                  0
                  • G Gerben Jongerius

                    That would indeed work. If you put the entire function in the external php file and the code below in all files that need the check for mobile devices.

                    if (detect_mobile()) {
                    header('Location: blog');
                    }

                    Please note though that the redirect that you put in you code would not work as is. You would need to replace the 'blog' part with a URL to the mobile site.

                    J Offline
                    J Offline
                    Jassim Rahma
                    wrote on last edited by
                    #9

                    I am now getting this error:

                    Parse error: syntax error, unexpected T_VARIABLE in /home/jassimrahma/JassimRahma.com/detection.php on line 4

                    this is what I put in my index.php 1st line:

                    <?php
                    include_once('detection.php');
                    if (detect_mobile()) {
                    header('Location: http://m.jassimrahma.com');
                    }
                    ?>

                    and detection.php is here:

                    <?php
                    function detect_mobile()
                    {
                        $_SERVER['ALL_HTTP'] = isset($_SERVER['ALL_HTTP']) ? $_SERVER['ALL_HTTP'] : '';

                        $mobile\_browser = '0';
                    
                        $agent = strtolower($\_SERVER\['HTTP\_USER\_AGENT'\]);
                    
                        if(preg\_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|iphone|ipad|ipod|android|xoom)/i', $agent))
                            $mobile\_browser++;
                    
                        if((isset($\_SERVER\['HTTP\_ACCEPT'\])) and (strpos(strtolower($\_SERVER\['HTTP\_ACCEPT'\]),'application/vnd.wap.xhtml+xml') !== false))
                            $mobile\_browser++;
                    
                        if(isset($\_SERVER\['HTTP\_X\_WAP\_PROFILE'\]))
                            $mobile\_browser++;
                    
                        if(isset($\_SERVER\['HTTP\_PROFILE'\]))
                            $mobile\_browser++;
                    
                        $mobile\_ua = substr($agent,0,4);
                        $mobile\_agents = array(
                                            'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',
                                            'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',
                                            'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-&#
                    
                    1 Reply Last reply
                    0
                    • G Gerben Jongerius

                      That would indeed work. If you put the entire function in the external php file and the code below in all files that need the check for mobile devices.

                      if (detect_mobile()) {
                      header('Location: blog');
                      }

                      Please note though that the redirect that you put in you code would not work as is. You would need to replace the 'blog' part with a URL to the mobile site.

                      J Offline
                      J Offline
                      Jassim Rahma
                      wrote on last edited by
                      #10

                      Thanks I am using now this and very simple and works fine for me.. http://code.google.com/p/php-mobile-detect/[^]

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups