Newscoop 4 Cookbook

Browser

(This chapter is based on the PHP browser detection script by Harald Hope, available under GNU GPL v3 from: http://techpatterns.com/downloads/scripts/browser_detection_php_ar.txt)

The browser object is set at the beginning of the main template based on the request URL. The browser object can be called as follows:

{{ $gimme->browser }} or {{ $gimme->browser->browser_name }}

The above statement returns the full browser name string, if available, otherwise it returns ''.  The statement:

{{ $gimme->browser->browser_number }}

returns the browser version number, if available, otherwise it returns ''. The statement:

{{ $gimme->browser->browser_working }}

returns the working shorthand browser name: ie, op, moz, konq, saf, ns4, webkit, and some others. If not shorthand, it will probably just return the full browser name, like lynx. The statement:

{{ $gimme->browser->dom }}

returns true/false if it is a basic dom browser, ie >= 5, opera >= 5, all new mozillas, safaris, or konquerors. The statement:

{{ $gimme->browser->ie_version }}

tests to see what general IE it is. Possible return values:

     ie9x - all new msie 9 or greater - note that if in compat mode, 7,8,9 all show as 7
     ie7x - all new msie 7 or greater
     ie5x - msie 5 and 6, not mac
     ieMac - msie 5.x mac release
     ie4 - msie 4
     old - pre msie 4

The statement:

{{ $gimme->browser->mobile_data }}

returns an array of data about mobiles. Note the browser/os number data is very unreliable so don't count on that. No Blackberry version handling is done explicitly. Make sure to test if this is an array, because if it's not mobile it will be null, not an array listed by array index number:

     0 - $mobile_device
     1 - $mobile_browser
     2 - $mobile_browser_number
     3 - $mobile_os
     4 - $mobile_os_number
     5 - $mobile_server
     6 - $mobile_server_number
     7 - $mobile_device_number

    Note: $mobile_browser only returns if a specifically mobile browser is detected, like minimo. Same for mobile os, with the exception of GNU/Linux. Otherwise the standard script os/browser data is used. $mobile_server is a handheld service like docomo, novarro-vision, etc. Sometimes the string will contain no other usable data than this to determine if it's handheld or not.

{{ if $gimme->browser->ua_type != "mobile" }}   {{ assign var="browserdetect_mobile_device" value="false" }}   {{ assign var="browserdetect_mobile_os" value="false" }}   {{ assign var="browserdetect_mobile_os_number" value="false" }} {{ else }}   {{ assign var="mobile_data" value=`$gimme->browser->mobile_data` }}   {{ php }}   $mobile_data = $this->get_template_vars('mobile_data');   $this->assign('browserdetect_mobile_device', $mobile_data[0]);   $this->assign('browserdetect_mobile_os', $mobile_data[3]);   $this->assign('browserdetect_mobile_os_number', $mobile_data[4]);   {{ /php }} {{ /if }} 

The statement:

{{ $gimme->browser->moz_data }}

returns array of mozilla / gecko information. Return Array listed by index number:

      0 - $moz_type [moz version - the specific brand name that is, eg: firefox)
      1 - $moz_number - the full version number of $moz_type (eg: for firefox: 3.6+2b)
      2 - $moz_rv - the Mozilla rv version number, math comparison version. This tells you what gecko engine is running in the browser (eg rv: 1.8)
      3 - $moz_rv_full - rv number (for full rv, including alpha and beta versions: 1.8.1-b3)
      4 - $moz_release_date - release date of the browser

{{ if $gimme->browser->browser_working == "moz" }}   {{ assign var="browser_data" value=`$gimme->browser->moz_data` }}   {{ php }}   $browser_data = $this->get_template_vars('browser_data');   $this->assign('browserdetect_name', $browser_data[0]);   $this->assign('browserdetect_engine', "gecko");   $this->assign('browserdetect_engineversion', $browser_data[2]);   $this->assign('browserdetect_version', $browser_data[1]);   {{ /php }} {{ /if }} 

The statement:

{{ $gimme->browser->os }}

returns which os is being used - win, nt, mac, OR iphone, blackberry, palmos, palmsource, symbian, beos, os2, amiga, webtv, linux, unix. The statement:

{{ $gimme->browser->os_number }}

returns windows versions, 95, 98, ce, me, nt: 4; 5 [windows 2000]; 5.1 [windows xp]; 5.2 [Server 2003]; 6.0 [Windows Vista], 6.1 [Windows 7]. Only win, nt, mac, iphone return os numbers (mac/iphone return 10 if OS X.) OR returns GNU/Linux distro/unix release name, otherwise returns null.

{{ $gimme->browser->run_time }}

The time it takes this script to execute from start to point of returning value. Requires PHP 5 or greater. Returns time in seconds to 8 decimal places: 0.00245687. Run time does not count the time used by PHP to include/parse the file initially. That total time is about 5-10x longer. Because subsequent script run-throughs go very fast, you will see the seconds go from something like 0.00115204 for the first time, to something like 0.00004005 for second and more runs. The statement:

{{ $gimme->browser->safe }}

returns true/false, you can determine what makes the browser be safe lower down, currently it's set for ns4 and pre version 1 mozillas not being safe, plus all older browsers. The statement:

{{ $gimme->browser->true_ie_number }}

returns the true version of msie running, ignoring the compat mode version.

Note that php will turn 7.0 to 8 when adding 1, so keep that in mind in your tests. 7.1 will become 8.1 as expected, however. This test currently only tests for 7.x -> 8.x

FYI: in PHP, 7.0 == 7 is true but 7.0 === 7 is NOT true. If this is null but set, then it is NOT running in compatibility mode. The statement:

{{ $gimme->browser->ua_type }}

returns one of the following:

      bot (web bot)
      bro (normal browser)
      bbro (simple browser)
      mobile (handheld)
      dow (downloading agent)
      lib (http library)

The statement:

{{ $gimme->browser->webkit_data }}

returns array of webkit data. The Return Array is listed by index number:

      0 - $webkit_type [webkit version name (Eg. chrome)]
      1 - $webkit_type_number [webkit version number (Eg. Chrome's: 1.2)]
      2 - $browser_number [the actual webkit version number (Eg. Webkit's: 436)]

{{ if $gimme->browser->browser_working == "webkit" }}   {{ assign var="browser_data" value=`$gimme->browser->webkit_data` }}   {{ assign var="browserdetect_engineversion" value=`$gimme->browser->browser_number` }}   {{ php }}   $browser_data = $this->get_template_vars('browser_data');   $this->assign('browserdetect_name', $browser_data[0]);   $this->assign('browserdetect_engine', "webkit");   $this->assign('browserdetect_version', $browser_data[1]);   {{ /php }} {{ /if }}