PEAR is discontinued

This is a static archive preserving historical releases and metadata. No new packages or updates will be published. For updates to existing packages, or to publish new packages, please use Composer and Packagist.

Home » Networking » Net_UserAgent_Detect » Bug #2025

UserAgent_detect should detect more than one brower per page

Details

Request #2025UserAgent_detect should detect more than one brower per page
Submitted2004-07-30 16:50 UTC
Fromthomas dot moulard at wanadoo dot fr
StatusClosed
PackageNet_UserAgent_Detect
PHP Version4.3.3
OSwindows XP
Roadmaps(Not assigned)

Comments

[2004-07-30 16:50 UTC] thomas dot moulard at wanadoo dot fr

Description:
------------
The static data which are stored can't be reset, so only one ip can be parsed per page!

The aim of this example is printing a "who is online" page ($session is the result of a query...)

Reproduce code:
---------------
while ($session->fetch()) {
$UserAgent =& new Net_UserAgent_Detect($session->UserAgent, array(NET_USERAGENT_DETECT_BROWSER, NET_USERAGENT_DETECT_OS, NET_USERAGENT_DETECT_FEATURES));

$session->browser = $UserAgent->getBrowserString();
$session->os = $UserAgent->getOSString();
$session->js = "v. ".$UserAgent->getFeature('javascript');
$session->dhtml = ($UserAgent->getFeature('dhtml')=='1')?'enable':'disable';
$session->dom = ($UserAgent->getFeature('dom')=='1')?'enable':'disable';
$session->sidebar = $UserAgent->getFeature('sidebar');
$session->gecko = $UserAgent->getFeature('gecko');

unset($UserAgent);
}

Expected result:
----------------
For example:

array {
array {
browser => Ms ie
os => windows XP
etc...
}
array {
browser => Firefox
os => debian
etc...
}
}

Actual result:
--------------
array {
array {
browser => Ms ie
os => windows XP
etc...
}
array { // EVEN IF I PUT A DIFFERENT USERAGENT, THE RESULT DOES NOT CHANGE!!
browser => Ms ie
os => windows XP
etc...
}
}

[2004-12-08 00:12 UTC] philip at projectie dot com

Had the same problem, I agree it worked slightly counter-intuitive. Fixed it by altering the detect() function slightly. See below:

Altered code:
-------------

function detect($in_userAgent = null, $in_detect = null)
{

// {{{ set up static properties
static $hasRun;

$options = &Net_UserAgent_Detect::_getStaticProperty['options');

if (!isset($options['re-evaluate']) OR $options['re-evaluate'] == TRUE)
{
// echo "Doing regular run (as before) <br />";
if (!empty($hasRun))
{
return;
}
} else {
// re-evaluating the options properly set up (such as the user agent
}
$hasRun = TRUE;

$in_userAgent = isset($options['userAgent']) && is_null($in_userAgent) ? $options['userAgent'] : null;

etc.

How to call it:
---------------
while ($session->fetch()) {
Net_UserAgent_Detect::setOption('userAgent', $session->UserAgent);
Net_UserAgent_Detect::setOption('re-evaluate, TRUE);

$session->browser = $UserAgent->getBrowserString();
$session->os = $UserAgent->getOSString();

Notes:
------
This will re-evaluate the userAgent string on each request. If you are evaluating different user agents in a loop, but using the same user agent to retrieve multiple properties, then you can set re-evaluate to FALSE after the first property of the loop is called. So this means adding the following line after your $session->browser line:

Net_UserAgent_Detect::setOption('re-evaluate, FALSE);

This will make it a tiny bit faster.

[2004-12-08 00:15 UTC] philip at projectie dot com

OOOPS!

TRUE should be FALSE in my submitted line of course (its rather self-explanatory):

if (!isset($options['re-evaluate']) OR $options['re-evaluate'] == FALSE)
{

etc.