PHP browser and os detection showing chrome instead of opera

China☆狼群 提交于 2019-12-06 08:12:42

Code logic : it finds Opera first in the loop, but then meets "Chrome", which also matches. So the last result found is kept. I suggest :

foreach ($browser_array as $regex => $value) { 

    if (preg_match($regex, $user_agent) && $browser == "Unknown Browser")
        $browser = $value;

}

This way, if the browser has already been found, it won't change it to the new value. Make sure to sort your array correctly though.

In your case, this code would find Opera first (matching OPR), and ignore Chrome afterwards (even though Opera matches with "Chrome"). The problem with that code is that this time, Chrome will be the problem (it will be detected as Opera). You should use more specific REGEX.

The fact that some browsers are partially signed as others is history-related. Some browsers take their code from others, and are therefore recognized differently.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!