PHP - get_browser() results

2019-07-07 06:44发布

I'm using the get_browser() function, and I installed the latest php_browscap.ini file from http://tempdownloads.browserscap.com/, updated my php.ini file to reflect the new location.

Here's my simple code I've gleaned from PHP's site:

<?php
echo $_SERVER['HTTP_USER_AGENT'];
echo "<br>";
echo "<br>";
$browser = get_browser(null, true);
print_r($browser);
?>

And the result is this:

Mozilla/5.0 (Windows NT 6.1; WOW64; rv:21.0) Gecko/20100101 Firefox/21.0

Array ( [browser_name_regex] => �^mozilla/5\.0 \(.*windows nt 6\.1.*wow64.*\).*gecko/.*firefox/21\..*$� [browser_name_pattern] => Mozilla/5.0 (*Windows NT 6.1*WOW64*)*Gecko/*Firefox/21.* [parent] => Firefox 21.0 [platform] => Win7 [platform_version] => 6.1 [win32] => [win64] => 1 [comment] => Firefox 21.0 [browser] => Firefox [version] => 21.0 [majorver] => 21 [minorver] => 0 [beta] => 1 [frames] => 1 [iframes] => 1 [tables] => 1 [cookies] => 1 [javascript] => 1 [javaapplets] => 1 [cssversion] => 3 [alpha] => [win16] => [backgroundsounds] => [vbscript] => [activexcontrols] => [ismobiledevice] => [issyndicationreader] => [crawler] => [aolversion] => 0 ) 

I'm seeing a weird character in the [browser_name_regex] section.
Is that expected?

Also, I

标签: php browser get
2条回答
干净又极端
2楼-- · 2019-07-07 07:07

@Stano, thanks for the explanation. Based on your answer and the information found at https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=612364 which explains the bug more throughly, i've created some lines of code which solves the problem.

The same question has been asked at Getting illegal characters in get_browser() method run in php

I've posted my answer both here and there.

Code:

if (function_exists('get_browser') && ini_get('browscap')) {
    $browser_info = get_browser(null, true);
    if (function_exists('mb_convert_encoding')) $browser_info['browser_name_regex'] = mb_convert_encoding($browser_info['browser_name_regex'], "UTF-8", "ISO-8859-1");
    print_r($browser_info);
}
查看更多
够拽才男人
3楼-- · 2019-07-07 07:19

Although it's not expected, but it's allowed. My output from the get_browser() function was [browser_name_regex] => §^.*$§. It just has two paragraphs instead of exerted slashes /^.*$/.

Explanation:

Delimiters can be any non-alphanumeric, non-whitespace ASCII character except the backslash (\) and the null byte. (link)

What you see is merely a pcre pattern wrapped with delimiter which is \xA7. To get browsers matching you'll need a browscap.ini which is downloadable by the links on the doc page. (link)

查看更多
登录 后发表回答