We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
  • hello how can i use this php code and how i get the "return" ?
    i use MODX Revolution 2.2.10-pl (advanced)
    <?php
    // GET THE USER_AGENT
    $ua = (isset($_SERVER) && array_key_exists('HTTP_USER_AGENT', $_SERVER)) ? $_SERVER['HTTP_USER_AGENT'] : false;
    
    // TRUE IF MOBILE DEVICE
    $ismobile = (isset($_SERVER['AMF_ISMOBILE'])) ? $_SERVER['AMF_ISMOBILE'] : false;
    
    function browser()
    {
    	global $ua, $ismobile;
    	if(stristr($ua, 'windows') != '') $os = 'win';
    	if(stristr($ua, 'mac') != '') $os = 'mac';
    	if(stristr($ua, 'linux') != '') $os = 'linux';
    	if(stristr($ua, 'android') != '') $os = 'android';
    	if(stristr($ua, 'iphone') != '') $os = 'iphone';
    	if(stristr($ua, 'ipad') != '') $os = 'ipad';
    
    	$mobile = ($ismobile) ? ' mobile' : '';
    	switch($ua)
    	{
    		case stristr($ua, 'firefox') == true:
    			$ver = explode('/',$ua);
    			$ver = explode('.',$ver[count($ver)-1]);
    			$o = 'firefox ff-'.$ver[0].' ff-'.$os;
    		break;
    		case stristr($ua, 'opr') == true:
    			$ver = explode('/',$ua);
    			$ver = explode('.',$ver[count($ver)-1]);
    			$o = 'opera opera-'.$ver[0].' opera-'.$os;
    		break;
    		case stristr($ua, 'safari') != '' && stristr($ua, 'chrome') === false:
    			$o = 'safari safari-'.$os;
    		break;
    		case stristr($ua, 'chrome') == true:
    			$ver = explode('Chrome/',$ua);
    			$ver = explode('.',$ver[1]);
    			$o = 'chrome chrome-'.$ver[0].' chrome-'.$os;
    		break;
    		case stristr($ua, 'msie') == true:
    			$ver = explode('MSIE ', $ua);
    			$ver = explode('.', $ver[1]);
    			$o = 'iexplore ie-'.$ver[0];
    		break;
    		default:
    			$o = 'browserunknown';
    		break;
    	}
    	$o = $o.' '.$os;
    	return $o;
    }
      palma non sine pulvere
      • 3749
      • 24,544 Posts
      You can just paste the code above into a snippet and put a tag for the snippet where you want to use the USER_AGENT.

      You'll have to explain how you want to use the USER_AGENT information if you need more than that.

        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
      • Quote from: BobRay at Jan 15, 2014, 11:11 PM
        You can just paste the code above into a snippet and put a tag for the snippet where you want to use the USER_AGENT.

        You'll have to explain how you want to use the USER_AGENT information if you need more than that.


        yep, i make same...just paste the code in new snippet with name UserAgent and then i call my snippet like this: [[UserAgent]] or [[!UserAgent]] /or [[UserAgent?]] or [[!UserAgent?]]/, but i have no return results?

        in regular php file to get the browser i use: <?php echo browser(); ?>
          palma non sine pulvere
          • 3749
          • 24,544 Posts
          Your browser() function is never called.

          Add this line at the very end (after the closing } of the function):

          return browser();
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
          • Quote from: BobRay at Jan 15, 2014, 11:22 PM
            Your browser() function is never called.

            Add this line at the very end (after the closing } of the function):

            return browser();

            now i get some results, but no meter what browser and os i use i get "firefox ff- ff- " in regular php i get correct results but here...something gone wrong, Bob give me just one more hint smiley
              palma non sine pulvere
              • 3749
              • 24,544 Posts
              Sorry, I don't see how that could happen if you're visiting from different browsers.

              BTW, the $os lines should really be like this (though that's not the problem):

              if(stripos($ua, 'windows') !== false) $os = 'win';


              And the cases should be like this:

              stripos($ua, 'firefox') !== false:
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting
              • Quote from: BobRay at Jan 15, 2014, 11:41 PM
                Sorry, I don't see how that could happen if you're visiting from different browsers.

                BTW, the $os lines should really be like this (though that's not the problem):

                if(stripos($ua, 'windows') !== false) $os = 'win';
                so strange for me sad is not work properly
                  palma non sine pulvere
                  • 3749
                  • 24,544 Posts
                  Sorry, I wasn't thinking. The problem is that the switch statement doesn't work that way. It just matches what's in the switch line to what's in the case line. You need to use if and elseif instead:


                  if (strpos($ua, 'firefox') !== false) {
                      $ver = explode('/',$ua);
                      $ver = explode('.',$ver[count($ver)-1]);
                      $o = 'firefox ff-'.$ver[0].' ff-'.$os;
                  
                  } elseif (stripos($ua, 'opr') !== false) {
                      $ver = explode('/',$ua);
                      $ver = explode('.',$ver[count($ver)-1]);
                      $o = 'opera opera-'.$ver[0].' opera-'.$os;
                  } elseif (stripos($ua, 'safari') !== false && stripos($ua, 'chrome') === false) {
                      $o = 'safari safari-'.$os;
                  } elseif (stripos($ua, 'chrome') !== false) {
                      $ver = explode('Chrome/',$ua);
                      $ver = explode('.',$ver[1]);
                      $o = 'chrome chrome-'.$ver[0].' chrome-'.$os;
                  } elseif (stripos($ua, 'msie') !== false) {
                      $ver = explode('MSIE ', $ua);
                      $ver = explode('.', $ver[1]);
                      $o = 'iexplore ie-'.$ver[0];
                  } else {
                      $o = 'browserunknown';
                  }
                    Did I help you? Buy me a beer
                    Get my Book: MODX:The Official Guide
                    MODX info for everyone: http://bobsguides.com/modx.html
                    My MODX Extras
                    Bob's Guides is now hosted at A2 MODX Hosting
                  • Quote from: BobRay at Jan 15, 2014, 11:55 PM
                    Sorry, I wasn't thinking. The problem is that the switch statement doesn't work that way. It just matches what's in the switch line to what's in the case line. You need to use if and elseif instead:


                    if (strpos($ua, 'firefox') !== false) {
                        $ver = explode('/',$ua);
                        $ver = explode('.',$ver[count($ver)-1]);
                        $o = 'firefox ff-'.$ver[0].' ff-'.$os;
                    
                    } elseif (stripos($ua, 'opr') !== false) {
                        $ver = explode('/',$ua);
                        $ver = explode('.',$ver[count($ver)-1]);
                        $o = 'opera opera-'.$ver[0].' opera-'.$os;
                    } elseif (stripos($ua, 'safari') !== false && stripos($ua, 'chrome') === false) {
                        $o = 'safari safari-'.$os;
                    } elseif (stripos($ua, 'chrome') !== false) {
                        $ver = explode('Chrome/',$ua);
                        $ver = explode('.',$ver[1]);
                        $o = 'chrome chrome-'.$ver[0].' chrome-'.$os;
                    } elseif (stripos($ua, 'msie') !== false) {
                        $ver = explode('MSIE ', $ua);
                        $ver = explode('.', $ver[1]);
                        $o = 'iexplore ie-'.$ver[0];
                    } else {
                        $o = 'browserunknown';
                    }

                    sorry, can you give me all code
                      palma non sine pulvere
                      • 3749
                      • 24,544 Posts
                      I think this would be it (though $mobile is never used for anything or returned):

                      <?php
                      
                      // GET THE USER_AGENT
                      $ua = (isset($_SERVER) && array_key_exists('HTTP_USER_AGENT', $_SERVER))
                          ? $_SERVER['HTTP_USER_AGENT']
                          : false;
                      
                      // TRUE IF MOBILE DEVICE
                      $ismobile = (isset($_SERVER['AMF_ISMOBILE']))
                          ? $_SERVER['AMF_ISMOBILE']
                          : false;
                      
                      if (stristr($ua, 'windows') != '') $os = 'win';
                      if (stristr($ua, 'mac') != '') $os = 'mac';
                      if (stristr($ua, 'linux') != '') $os = 'linux';
                      if (stristr($ua, 'android') != '') $os = 'android';
                      if (stristr($ua, 'iphone') != '') $os = 'iphone';
                      if (stristr($ua, 'ipad') != '') $os = 'ipad';
                      
                      $mobile = ($ismobile) ? ' mobile' : '';
                      
                      if (strpos($ua, 'firefox') !== false) {
                          $ver = explode('/',$ua);
                          $ver = explode('.',$ver[count($ver)-1]);
                          $o = 'firefox ff-'.$ver[0].' ff-'.$os;
                      
                      } elseif (stripos($ua, 'opr') !== false) {
                          $ver = explode('/',$ua);
                          $ver = explode('.',$ver[count($ver)-1]);
                          $o = 'opera opera-'.$ver[0].' opera-'.$os;
                      } elseif (stripos($ua, 'safari') !== false && stripos($ua, 'chrome') === false) {
                          $o = 'safari safari-'.$os;
                      } elseif (stripos($ua, 'chrome') !== false) {
                          $ver = explode('Chrome/',$ua);
                          $ver = explode('.',$ver[1]);
                          $o = 'chrome chrome-'.$ver[0].' chrome-'.$os;
                      } elseif (stripos($ua, 'msie') !== false) {
                          $ver = explode('MSIE ', $ua);
                          $ver = explode('.', $ver[1]);
                          $o = 'iexplore ie-'.$ver[0];
                      } else {
                          $o = 'browserunknown';
                      }
                      
                      return $o;
                        Did I help you? Buy me a beer
                        Get my Book: MODX:The Official Guide
                        MODX info for everyone: http://bobsguides.com/modx.html
                        My MODX Extras
                        Bob's Guides is now hosted at A2 MODX Hosting