首頁 >後端開發 >php教程 >獲取用戶的瀏覽器以及版本信息

獲取用戶的瀏覽器以及版本信息

WBOY
WBOY原創
2016-07-29 08:58:591345瀏覽
<span>function </span><span>determinebrowser () {
</span><span>$agent </span><span>= </span><span>isset</span><span>(</span><span>$_SERVER</span><span>[</span><span>'HTTP_USER_AGENT'</span><span>]) ? </span><span>$_SERVER</span><span>[</span><span>'HTTP_USER_AGENT'</span><span>] : </span><span>'' </span><span>;
</span><span>$browseragent </span><span>= </span><span>""</span><span>;   </span><span><em>//</em></span><span><em>浏览器
</em></span><span><em></em></span><span>$browserversion </span><span>= </span><span>""</span><span>; </span><span><em>//</em></span><span><em>浏览器的版本
</em></span><span><em></em></span><span>if </span><span>(</span><span><em>preg_match</em></span><span>(</span><span>'/360SE/i'</span><span>,</span><span>$agent</span><span>)) {
</span><span>$browserversion </span><span>= </span><span>''</span><span>;
</span><span>$browseragent </span><span>= </span><span>"360</span><span>浏览器</span><span>"</span><span>;
</span><span>    } </span><span>else if</span><span>(</span><span><em>preg_match</em></span><span>(</span><span>'/Maxthon (([0-9_.]{1,2}+))/i'</span><span>,</span><span>$agent</span><span>,</span><span>$version</span><span>)){
</span><span>$browserversion </span><span>= </span><span>$version</span><span>[</span><span>1</span><span>];
</span><span>$browseragent </span><span>= </span><span>"Maxthon"</span><span>;
</span><span>    }</span><span>else if </span><span>(</span><span><em>preg_match</em></span><span>(</span><span>'/MSIE (([0-9_.]{1,2})+)/i'</span><span>,</span><span>$agent</span><span>,</span><span>$version</span><span>)) {
</span><span>$browserversion </span><span>= </span><span>$version</span><span>[</span><span>1</span><span>];
</span><span>$browseragent </span><span>= </span><span>"Internet Explorer"</span><span>;
</span><span>    } </span><span>else if </span><span>(</span><span><em>preg_match</em></span><span>( </span><span>'/iOpera\/(([0-9_.]{1,2})+)/i'</span><span>,</span><span>$agent</span><span>,</span><span>$version</span><span>)) {
</span><span>$browserversion </span><span>= </span><span>$version</span><span>[</span><span>1</span><span>];
</span><span>$browseragent </span><span>= </span><span>"Opera"</span><span>;
</span><span>    } </span><span>else if </span><span>(</span><span><em>preg_match</em></span><span>( </span><span>'/Firefox\/(([0-9_.]{1,3})+)/i'</span><span>,</span><span>$agent</span><span>,</span><span>$version</span><span>)) {
</span><span>$browserversion</span><span>=</span><span>$version</span><span>[</span><span>1</span><span>];
</span><span>$browseragent </span><span>= </span><span>"Firefox"</span><span>;
</span><span>    } </span><span>else if </span><span>(</span><span><em>preg_match</em></span><span>( </span><span>'/Chrome\/(([0-9_.]{1,5})+)/i'</span><span>,</span><span>$agent</span><span>,</span><span>$version</span><span>)) {
</span><span>$browserversion </span><span>= </span><span>$version</span><span>[</span><span>1</span><span>];
</span><span>$browseragent </span><span>= </span><span>"Chrome"</span><span>;
</span><span>    } </span><span>else if </span><span>(</span><span><em>preg_match</em></span><span>( </span><span>'/Safari\/(([0-9_.]{1,5})+)/'</span><span>,</span><span>$agent</span><span>,</span><span>$version</span><span>)) {
</span><span>$browserversion </span><span>= </span><span>$version</span><span>[</span><span>1</span><span>];
</span><span>$browseragent </span><span>= </span><span>"Safari"</span><span>;
</span><span>    } </span><span>else </span><span>{
</span><span>$browserversion </span><span>= </span><span>""</span><span>;
</span><span>$browseragent </span><span>= </span><span>"Unknown"</span><span>;
</span><span>    }
</span><span>return </span><span>$browseragent</span><span>.</span><span>" "</span><span>.</span><span>$browserversion</span><span>;
</span><span>}</span>

以上就介紹了獲取用戶的瀏覽器以及版本信息,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn