Getting illegal characters in get_browser() method

2019-08-29 08:14发布

From the manuals I am running a simple code to fetch browser info using get_browser() method.

Code in the manuals -

code_in_manuals

Code I am running(almost same/i tried by removing echo "<pre>"; but no effect) -

<?php
echo $_SERVER['HTTP_USER_AGENT'] . "\n\n";

$browser = get_browser(null, true);
echo "<pre>";
print_r($browser);
?>

This is the error/illegal characters I am getting at browser_name_regex -

get_browser_error

I also tried this UTF 8 encoding for characters, but problem still persist.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test browser</title>
</head>

<body>
<?php
echo $_SERVER['HTTP_USER_AGENT'] . "\n\n";

$browser = get_browser(null, true);
echo "<pre>";
print_r($browser);
?>
</body>
</html>

Let me know what I am doing wrong and where to look for the solution?

One thing more to ask, in the same doc -

Attempts to determine the capabilities of the user's browser, by looking up the browser's information in the browscap.ini file.

Where do I find this browscap.ini in my system I am using Windows 7...although I am not very sure if it is related with this problem or not.

2条回答
Anthone
2楼-- · 2019-08-29 08:42

The same question has been asked at PHP - get_browser() results

Based on the information there 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.

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-08-29 08:49

As you told that you found browscap.in file in your given path then specify that absolute path C:\xampp\php\extras\browscap.ini in your php.ini file like

I have in my local setup (php.ini), actually I have generated browscap file from other function and then pasted it in file and then found correct output.

[browscap]
;browscap = extra/browscap.ini
browscap = D:\ZendServer\etc\browscap.ini

you can specify your absolute path like

[browscap]
;browscap = extra/browscap.ini
browscap = C:\xampp\php\extras\browscap.ini

and code to check

echo $_SERVER['HTTP_USER_AGENT'] . "\n\n";

$browser = get_browser(null, true);
echo "<pre>";print_r($browser);die;
查看更多
登录 后发表回答