Ver Mensaje Individual
  #4 (permalink)  
Antiguo 25/05/2012, 12:59
matt_1985
 
Fecha de Ingreso: enero-2011
Ubicación: /root
Mensajes: 530
Antigüedad: 13 años, 3 meses
Puntos: 61
Respuesta: Detectar dispositivo móvil

Esta funcion te podria servir .

Código PHP:
Ver original
  1. <?php
  2. function mobile_device_detect($iphone=true,$ipad=true,$android=true,$opera=true,$blackberry=true,$palm=true,$windows=true,$mobileredirect=false,$desktopredirect=false){
  3.        
  4.       $mobile_browser   = false; // set mobile browser as false till we can prove otherwise
  5.       $user_agent       = $_SERVER['HTTP_USER_AGENT']; // get the user agent value - this should be cleaned to ensure no nefarious input gets executed
  6.       $accept           = $_SERVER['HTTP_ACCEPT']; // get the content accept value - this should be cleaned to ensure no nefarious input gets executed
  7.    
  8.       switch(true){ // using a switch against the following statements which could return true is more efficient than the previous method of using if statements
  9.    
  10.         case (preg_match('/ipad/i',$user_agent)); // we find the word ipad in the user agent
  11.           $mobile_browser = $ipad; // mobile browser is either true or false depending on the setting of ipad when calling the function
  12.           $status = 'Apple iPad';
  13.           if(substr($ipad,0,4)=='http'){ // does the value of ipad resemble a url
  14.             $mobileredirect = $ipad; // set the mobile redirect url to the url value stored in the ipad value
  15.           } // ends the if for ipad being a url
  16.         break; // break out and skip the rest if we've had a match on the ipad // this goes before the iphone to catch it else it would return on the iphone instead
  17.    
  18.         case (preg_match('/ipod/i',$user_agent)||preg_match('/iphone/i',$user_agent)); // we find the words iphone or ipod in the user agent
  19.           $mobile_browser = $iphone; // mobile browser is either true or false depending on the setting of iphone when calling the function
  20.           $status = 'Apple';
  21.           if(substr($iphone,0,4)=='http'){ // does the value of iphone resemble a url
  22.             $mobileredirect = $iphone; // set the mobile redirect url to the url value stored in the iphone value
  23.           } // ends the if for iphone being a url
  24.         break; // break out and skip the rest if we've had a match on the iphone or ipod
  25.    
  26.         case (preg_match('/android/i',$user_agent));  // we find android in the user agent
  27.           $mobile_browser = $android; // mobile browser is either true or false depending on the setting of android when calling the function
  28.           $status = 'Android';
  29.           if(substr($android,0,4)=='http'){ // does the value of android resemble a url
  30.             $mobileredirect = $android; // set the mobile redirect url to the url value stored in the android value
  31.           } // ends the if for android being a url
  32.         break; // break out and skip the rest if we've had a match on android
  33.    
  34.         case (preg_match('/opera mini/i',$user_agent)); // we find opera mini in the user agent
  35.           $mobile_browser = $opera; // mobile browser is either true or false depending on the setting of opera when calling the function
  36.           $status = 'Opera';
  37.           if(substr($opera,0,4)=='http'){ // does the value of opera resemble a rul
  38.             $mobileredirect = $opera; // set the mobile redirect url to the url value stored in the opera value
  39.           } // ends the if for opera being a url
  40.         break; // break out and skip the rest if we've had a match on opera
  41.    
  42.         case (preg_match('/blackberry/i',$user_agent)); // we find blackberry in the user agent
  43.           $mobile_browser = $blackberry; // mobile browser is either true or false depending on the setting of blackberry when calling the function
  44.           $status = 'Blackberry';
  45.           if(substr($blackberry,0,4)=='http'){ // does the value of blackberry resemble a rul
  46.             $mobileredirect = $blackberry; // set the mobile redirect url to the url value stored in the blackberry value
  47.           } // ends the if for blackberry being a url
  48.         break; // break out and skip the rest if we've had a match on blackberry
  49.    
  50.         case (preg_match('/(pre\/|palm os|palm|hiptop|avantgo|plucker|xiino|blazer|elaine)/i',$user_agent)); // we find palm os in the user agent - the i at the end makes it case insensitive
  51.           $mobile_browser = $palm; // mobile browser is either true or false depending on the setting of palm when calling the function
  52.           $status = 'Palm';
  53.           if(substr($palm,0,4)=='http'){ // does the value of palm resemble a rul
  54.             $mobileredirect = $palm; // set the mobile redirect url to the url value stored in the palm value
  55.           } // ends the if for palm being a url
  56.         break; // break out and skip the rest if we've had a match on palm os
  57.    
  58.         case (preg_match('/(iris|3g_t|windows ce|opera mobi|windows ce; smartphone;|windows ce; iemobile)/i',$user_agent)); // we find windows mobile in the user agent - the i at the end makes it case insensitive
  59.           $mobile_browser = $windows; // mobile browser is either true or false depending on the setting of windows when calling the function
  60.           $status = 'Windows Smartphone';
  61.           if(substr($windows,0,4)=='http'){ // does the value of windows resemble a rul
  62.             $mobileredirect = $windows; // set the mobile redirect url to the url value stored in the windows value
  63.           } // ends the if for windows being a url
  64.         break; // break out and skip the rest if we've had a match on windows
  65.    
  66.         case (preg_match('/(mini 9.5|vx1000|lge |m800|e860|u940|ux840|compal|wireless| mobi|ahong|lg380|lgku|lgu900|lg210|lg47|lg920|lg840|lg370|sam-r|mg50|s55|g83|t66|vx400|mk99|d615|d763|el370|sl900|mp500|samu3|samu4|vx10|xda_|samu5|samu6|samu7|samu9|a615|b832|m881|s920|n210|s700|c-810|_h797|mob-x|sk16d|848b|mowser|s580|r800|471x|v120|rim8|c500foma:|160x|x160|480x|x640|t503|w839|i250|sprint|w398samr810|m5252|c7100|mt126|x225|s5330|s820|htil-g1|fly v71|s302|-x113|novarra|k610i|-three|8325rc|8352rc|sanyo|vx54|c888|nx250|n120|mtk |c5588|s710|t880|c5005|i;458x|p404i|s210|c5100|teleca|s940|c500|s590|foma|samsu|vx8|vx9|a1000|_mms|myx|a700|gu1100|bc831|e300|ems100|me701|me702m-three|sd588|s800|8325rc|ac831|mw200|brew |d88|htc\/|htc_touch|355x|m50|km100|d736|p-9521|telco|sl74|ktouch|m4u\/|me702|8325rc|kddi|phone|lg |sonyericsson|samsung|240x|x320|vx10|nokia|sony cmd|motorola|up.browser|up.link|mmp|symbian|smartphone|midp|wap|vodafone|o2|pocket|kindle|mobile|psp|treo)/i',$user_agent)); // check if any of the values listed create a match on the user agent - these are some of the most common terms used in agents to identify them as being mobile devices - the i at the end makes it case insensitive
  67.           $mobile_browser = true; // set mobile browser to true
  68.           $status = 'Mobile matched on piped preg_match';
  69.         break; // break out and skip the rest if we've preg_match on the user agent returned true
  70.    
  71.         case ((strpos($accept,'text/vnd.wap.wml')>0)||(strpos($accept,'application/vnd.wap.xhtml+xml')>0)); // is the device showing signs of support for text/vnd.wap.wml or application/vnd.wap.xhtml+xml
  72.  
  73.           $mobile_browser = true; // set mobile browser to true
  74.           $status = 'Mobile matched on content accept header';
  75.         break; // break out and skip the rest if we've had a match on the content accept headers
  76.    
  77.         case (isset($_SERVER['HTTP_X_WAP_PROFILE'])||isset($_SERVER['HTTP_PROFILE'])); // is the device giving us a HTTP_X_WAP_PROFILE or HTTP_PROFILE header - only mobile devices would do this
  78.           $mobile_browser = true; // set mobile browser to true
  79.           $status = 'Mobile matched on profile headers being set';
  80.         break; // break out and skip the final step if we've had a return true on the mobile specfic headers
  81.  
  82.    
  83.     ?>


Saludos