Guest User

http://blog.unijimpe.net/detectar-el-browser-con-php/

a guest
Jun 23rd, 2010
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.49 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Class to detect which browser is currently accessing the page/site
  5.  * @author Paul Scott
  6.  * This class is very loosely based on scripts by Gary White
  7.  * @copyright Paul Scott
  8.  * @package browser
  9.  */
  10.  
  11. class browser
  12. {
  13.     /**
  14.      * @var string $name
  15.      */
  16.     var $name = NULL;
  17.    
  18.     /**
  19.      * @var string $version
  20.      */
  21.     var $version = NULL;
  22.    
  23.     /**
  24.      * @var $useragent
  25.      */
  26.     var $useragent = NULL;
  27.    
  28.     /**
  29.      * @var string $platform
  30.      */
  31.     var $platform;
  32.    
  33.     /**
  34.      * @var string aol
  35.      */
  36.     var $aol = FALSE;
  37.    
  38.     /**
  39.      * @var string browser
  40.      */
  41.     var $browsertype;
  42.    
  43.     /**
  44.      * Class constructor
  45.      * @param void
  46.      * @return void
  47.      */
  48.     function browser()
  49.     {
  50.         $agent = $_SERVER['HTTP_USER_AGENT'];
  51.         //set the useragent property
  52.         $this->useragent = $agent;
  53.     }
  54.    
  55.     /**
  56.      * Method to get the browser details from the USER_AGENT string in
  57.      * the PHP superglobals
  58.      * @param void
  59.      * @return string property platform
  60.      */
  61.     function getBrowserOS()
  62.     {
  63.         $win = eregi("win", $this->useragent);
  64.         $linux = eregi("linux", $this->useragent);
  65.         $mac = eregi("mac", $this->useragent);
  66.         $os2 = eregi("OS/2", $this->useragent);
  67.         $beos = eregi("BeOS", $this->useragent);
  68.        
  69.         //now do the check as to which matches and return it
  70.         if($win)
  71.         {
  72.             $this->platform = "Windows";
  73.         }
  74.         elseif ($linux)
  75.         {
  76.             $this->platform = "Linux";
  77.         }
  78.         elseif ($mac)
  79.         {
  80.             $this->platform = "Macintosh";
  81.         }
  82.         elseif ($os2)
  83.         {
  84.             $this->platform = "OS/2";
  85.         }
  86.         elseif ($beos)
  87.         {
  88.             $this->platform = "BeOS";
  89.         }
  90.         return $this->platform;
  91.     }
  92.    
  93.     /**
  94.      * Method to test for Opera
  95.      * @param void
  96.      * @return property $broswer
  97.      * @return property version
  98.      * @return bool false on failure
  99.      */
  100.     function isOpera()
  101.     {
  102.         // test for Opera      
  103.         if (eregi("opera",$this->useragent))
  104.         {
  105.             $val = stristr($this->useragent, "opera");
  106.             if (eregi("/", $val)){
  107.                 $val = explode("/",$val);
  108.                 $this->browsertype = $val[0];
  109.                 $val = explode(" ",$val[1]);
  110.                 $this->version = $val[0];
  111.             }else{
  112.                 $val = explode(" ",stristr($val,"opera"));
  113.                 $this->browsertype = $val[0];
  114.                 $this->version = $val[1];
  115.             }
  116.             return TRUE;
  117.         }
  118.         else {
  119.             return FALSE;
  120.         }
  121.     }
  122.    
  123.     /**
  124.      * Method to check for FireFox
  125.      * @param void
  126.      * @return bool false on failure
  127.      */
  128.     function isFirefox()
  129.     {
  130.         if(eregi("Firefox", $this->useragent))
  131.         {
  132.             $this->browsertype = "Firefox";
  133.             $val = stristr($this->useragent, "Firefox");
  134.             $val = explode("/",$val);
  135.             $this->version = $val[1];
  136.             return true;
  137.         }
  138.         else {
  139.             return FALSE;
  140.         }
  141.     }
  142.    
  143.     /**
  144.      * Method to check for Konquerer
  145.      * @param void
  146.      * @return prop $browser
  147.      * @return prop $version
  148.      * @return bool true on success
  149.      */
  150.     function isKonqueror()
  151.     {
  152.         if(eregi("Konqueror",$this->useragent))
  153.         {
  154.             $val = explode(" ",stristr($this->useragent,"Konqueror"));
  155.             $val = explode("/",$val[0]);
  156.             $this->browsertype = $val[0];
  157.             $this->version = str_replace(")","",$val[1]);
  158.             return TRUE;
  159.         }
  160.         else {
  161.             return FALSE;
  162.         }
  163.        
  164.     }//end func
  165.    
  166.     /**
  167.      * Method to check for Internet Explorer v1
  168.      * @param void
  169.      * @return bool true on success
  170.      * @return prop $browsertype
  171.      * @return prop $version
  172.      */
  173.     function isIEv1()
  174.     {
  175.         if(eregi("microsoft internet explorer", $this->useragent))
  176.         {
  177.             $this->browsertype = "MSIE";
  178.             $this->version = "1.0";
  179.             $var = stristr($this->useragent, "/");
  180.             if (ereg("308|425|426|474|0b1", $var))
  181.             {
  182.                 $this->version = "1.5";
  183.             }
  184.             return TRUE;
  185.         }
  186.         else {
  187.             return FALSE;
  188.         }
  189.     }//end function
  190.    
  191.     /**
  192.      * Method to check for Internet Explorer later than v1
  193.      * @param void
  194.      * @return bool true on success
  195.      * @return prop $browsertype
  196.      * @return prop $version
  197.      */
  198.     function isMSIE()
  199.     {
  200.         if(eregi("msie", $this->useragent) && !eregi("opera",$this->useragent))
  201.         {
  202.             $this->browsertype = "MSIE";
  203.             $val = explode(" ",stristr($this->useragent,"msie"));
  204.             $this->browsertype = $val[0];
  205.             $this->version = $val[1];
  206.            
  207.             return TRUE;
  208.         }
  209.         else {
  210.             return FALSE;
  211.         }
  212.     }//end function
  213.    
  214.     /**
  215.      * Method to check for Galeon
  216.      * @param void
  217.      * @return bool true on success
  218.      */
  219.     function isGaleon()
  220.     {
  221.         if(eregi("galeon",$this->useragent))
  222.         {
  223.             $val = explode(" ",stristr($this->useragent,"galeon"));
  224.             $val = explode("/",$val[0]);
  225.             $this->browsertype = $val[0];
  226.             $this->version = $val[1];
  227.             return TRUE;
  228.         }
  229.         else {
  230.             return FALSE;
  231.         }
  232.     }//end func
  233.    
  234.     /**
  235.      * Now we do the tests for browsers I can't test...
  236.      * If someone finds a bug, please report it ASAP to me please!
  237.      */
  238.    
  239.     /**
  240.      * Method to check for WebTV browser
  241.      * @param void
  242.      * @return bool true on success
  243.      * @return prop $browsertype
  244.      * @return prop $version
  245.      */
  246.     function isWebTV()
  247.     {
  248.         if(eregi("webtv",$this->useragent))
  249.         {
  250.             $val = explode("/",stristr($this->useragent,"webtv"));
  251.             $this->browsertype = $val[0];
  252.             $this->version = $val[1];
  253.             return TRUE;
  254.         }
  255.         else {
  256.             return FALSE;
  257.         }
  258.     }
  259.    
  260.     /**
  261.      * Method to check for BeOS's NetPositive
  262.      * @param void
  263.      * @return bool true on success
  264.      * @return prop $browsertype
  265.      * @return prop $version
  266.      */
  267.     function isNetPositive()
  268.     {
  269.         if(eregi("NetPositive", $this->useragent))
  270.         {
  271.             $val = explode("/",stristr($this->useragent,"NetPositive"));
  272.             $this->platform = "BeOS";
  273.             $this->browsertype = $val[0];
  274.             $this->version = $val[1];
  275.             return TRUE;
  276.         }
  277.         else {
  278.             return FALSE;
  279.         }
  280.     }
  281.    
  282.     /**
  283.      * Method to check for MSPIE (Pocket IE)
  284.      * @param void
  285.      * @return bool true on success
  286.      */
  287.     function isMSPIE()
  288.     {
  289.         if(eregi("mspie",$this->useragent) || eregi("pocket", $this->useragent))
  290.         {
  291.             $val = explode(" ",stristr($this->useragent,"mspie"));
  292.             $this->browsertype = "MSPIE";
  293.             $this->platform = "WindowsCE";
  294.             if (eregi("mspie", $this->useragent))
  295.                 $this->version = $val[1];
  296.             else {
  297.                 $val = explode("/",$this->useragent);
  298.                 $this->version = $val[1];
  299.             }
  300.             return TRUE;
  301.         }
  302.         else {
  303.             return FALSE;
  304.         }
  305.     }
  306.    
  307.     /**
  308.      * Method to test for iCab
  309.      * @param void
  310.      * @return bool true on success
  311.      */
  312.     function isIcab()
  313.     {
  314.         if(eregi("icab",$this->useragent))
  315.         {
  316.             $val = explode(" ",stristr($this->useragent,"icab"));
  317.             $this->browsertype = $val[0];
  318.             $this->version = $val[1];
  319.             return TRUE;
  320.         }
  321.         else {
  322.             return FALSE;
  323.         }
  324.     }
  325.    
  326.     /**
  327.      * Method to test for the OmniWeb Browser
  328.      * @param void
  329.      * @return bool True on success
  330.      */
  331.     function isOmniWeb()
  332.     {
  333.         if(eregi("omniweb",$this->useragent))
  334.         {
  335.             $val = explode("/",stristr($this->useragent,"omniweb"));
  336.             $this->browsertype = $val[0];
  337.             $this->version = $val[1];
  338.             return TRUE;
  339.         }
  340.         else {
  341.             return FALSE;
  342.         }
  343.     }
  344.    
  345.     /**
  346.      * Method to check for Phoenix Browser
  347.      * @param void
  348.      * @return bool true on success
  349.      */
  350.     function isPhoenix()
  351.     {
  352.         if(eregi("Phoenix", $this->useragent))
  353.         {
  354.             $this->browsertype = "Phoenix";
  355.             $val = explode("/", stristr($this->useragent,"Phoenix/"));
  356.             $this->version = $val[1];
  357.             return TRUE;
  358.         }
  359.         else {
  360.             return FALSE;
  361.         }
  362.     }
  363.    
  364.     /**
  365.      * Method to check for Firebird
  366.      * @param void
  367.      * @return bool true on success
  368.      */
  369.     function isFirebird()
  370.     {
  371.         if(eregi("firebird", $this->useragent))
  372.         {
  373.             $this->browsertype = "Firebird";
  374.             $val = stristr($this->useragent, "Firebird");
  375.             $val = explode("/",$val);
  376.             $this->version = $val[1];
  377.             return TRUE;
  378.         }
  379.         else {
  380.             return FALSE;
  381.         }
  382.     }
  383.    
  384.     /**
  385.      * Method to check for Mozilla alpha/beta
  386.      * @param void
  387.      * @return bool true on success
  388.      */
  389.     function isMozAlphaBeta()
  390.     {
  391.         if(eregi("mozilla",$this->useragent) &&
  392.            eregi("rv:[0-9].[0-9][a-b]",$this->useragent) &&
  393.            !eregi("netscape",$this->useragent))
  394.        
  395.         {
  396.             $this->browsertype = "Mozilla";
  397.             $val = explode(" ",stristr($this->useragent,"rv:"));
  398.             eregi("rv:[0-9].[0-9][a-b]",$this->useragent,$val);
  399.             $this->version = str_replace("rv:","",$val[0]);
  400.             return TRUE;
  401.         }
  402.         else {
  403.             return FALSE;
  404.         }
  405.     }//end function
  406.  
  407.     /**
  408.      * Method to check for Mozilla Stable versions
  409.      * @param void
  410.      * @return bool true on success
  411.      */
  412.     function isMozStable()
  413.     {
  414.         if(eregi("mozilla",$this->useragent) &&
  415.            eregi("rv:[0-9]\.[0-9]",$this->useragent) &&
  416.            !eregi("netscape",$this->useragent))
  417.         {
  418.             $this->browsertype = "Mozilla";
  419.             $val = explode(" ",stristr($this->useragent,"rv:"));
  420.             eregi("rv:[0-9]\.[0-9]\.[0-9]",$this->useragent,$val);
  421.             $this->version = str_replace("rv:","",$val[0]);
  422.             return TRUE;
  423.         }
  424.         else {
  425.             return FALSE;
  426.         }
  427.     }
  428.    
  429.     /**
  430.      * Method to check for Lynx and Amaya
  431.      * @param void
  432.      * @return bool true on success
  433.      */
  434.     function isLynx()
  435.     {
  436.         if(eregi("libwww", $this->useragent))
  437.         {
  438.             if (eregi("amaya", $this->useragent))
  439.             {
  440.                 $val = explode("/",stristr($this->useragent,"amaya"));
  441.                 $this->browsertype = "Amaya";
  442.                 $val = explode(" ", $val[1]);
  443.                 $this->version = $val[0];
  444.             } else {
  445.                 $val = explode("/",$this->useragent);
  446.                 $this->browsertype = "Lynx";
  447.                 $this->version = $val[1];
  448.             }
  449.             return TRUE;
  450.         }
  451.         else {
  452.             return FALSE;
  453.         }
  454.     }
  455.    
  456.     /**
  457.      * method to check for safari browser
  458.      * @param void
  459.      * @return bool true on success
  460.      */
  461.     function isSafari()
  462.     {
  463.         if(eregi("safari", $this->useragent))
  464.         {
  465.             $this->browsertype = "Safari";
  466.             $this->version = "";
  467.             return TRUE;
  468.         }
  469.         else {
  470.             return FALSE;
  471.         }
  472.     }
  473.    
  474.     /**
  475.      * Various tests for Netscrape
  476.      * @param void
  477.      * @return bool true on success
  478.      */
  479.     function isNetscape()
  480.     {
  481.         if(eregi("netscape",$this->useragent))
  482.         {
  483.             $val = explode(" ",stristr($this->useragent,"netscape"));
  484.             $val = explode("/",$val[0]);
  485.             $this->browsertype = $val[0];
  486.             $this->version = $val[1];
  487.             return TRUE;
  488.         }
  489.         elseif(eregi("mozilla",$this->useragent) &&
  490.                 !eregi("rv:[0-9]\.[0-9]\.[0-9]",$this->useragent))
  491.         {
  492.             $val = explode(" ",stristr($this->useragent,"mozilla"));
  493.             $val = explode("/",$val[0]);
  494.             $this->browsertype = "Netscape";
  495.             $this->version = $val[1];
  496.             return TRUE;
  497.         }
  498.         else {
  499.             return FALSE;
  500.         }
  501.     }//end func
  502.    
  503.     /**
  504.      * Method to check for AOL connections
  505.      * @param void
  506.      * @return bool true on Success
  507.      */
  508.     function isAOL()
  509.     {
  510.         if (eregi("AOL", $this->useragent)){
  511.             $var = stristr($this->useragent, "AOL");
  512.             $var = explode(" ", $var);
  513.             $this->aol = ereg_replace("[^0-9,.,a-z,A-Z]", "", $var[1]);
  514.             return TRUE;
  515.         }
  516.         else {
  517.             return FALSE;
  518.         }
  519.     }
  520.    
  521.     /* Soporte Chrome por Charles */
  522.     function isChrome()
  523.     {
  524.     if(eregi("chrome", $this->useragent))
  525.     {
  526.     $this->browsertype = "Chrome";
  527.     $val = stristr($this->useragent, "Chrome");
  528.     $val = explode("/",$val);
  529.     $val = explode(" ",$val[1]);
  530.     $this->version = $val[0];
  531.     return true;
  532.     }
  533.     else {
  534.     return FALSE;
  535.     }
  536.     }
  537.    
  538.     /**
  539.      * Method to tie them all up and output something useful
  540.      * @param void
  541.      * @return array
  542.      */
  543.     function whatBrowser()
  544.     {
  545.         $this->getBrowserOS();
  546.         $this->isOpera();
  547.         $this->isFirefox();
  548.         $this->isKonqueror();
  549.         $this->isIEv1();
  550.         $this->isMSIE();
  551.         $this->isGaleon();
  552.         $this->isNetPositive();
  553.         $this->isMSPIE();
  554.         $this->isIcab();
  555.         $this->isOmniWeb();
  556.         $this->isPhoenix();
  557.         $this->isFirebird();
  558.         $this->isLynx();
  559.         $this->isSafari();
  560.         //$this->isMozAlphaBeta();
  561.         //$this->isMozStable();
  562.         //$this->isNetscape();
  563.         $this->isAOL();
  564.         $this->isChrome();
  565.         return array('browsertype' => $this->browsertype,
  566.                      'version' => $this->version,
  567.                      'platform' => $this->platform,
  568.                      'AOL' => $this->aol);
  569.     }
  570. }//end class
  571. ?>
Add Comment
Please, Sign In to add comment