How to determine the user's browser using PHP?

How to determine the user's browser using PHP?

So, if the user's browser is IE, then the variable $ alert = "onbeforeunload", and if it is not IE, for example Firefox (else), then $ alert = "onload.

Help is much appreciated.

thank

Also note that I cannot install browsercap.ini on my PHP server.

+3
source share
4 answers

See if this code works for you

<?php
function detect_ie(){
    return (sizeof(explode("MSIE",$_SERVER['HTTP_USER_AGENT'])) > 1);
}

if (detect_ie())
    $event = "onbeforeunload";
else
    $event = "onload";

?>
+2
source

You can not. Not with 100% accuracy. The best way to check the user agent, however the user is free not to supply it or fake, so try not to rely on him.

$ua = $_SERVER['HTTP_USER_AGENT'];

If the browser is IE, it should (roughly) match (where # is the number)

Mozilla/#.0 (compatible;  MSIE #.##;

-

'~^Mozilla/[0-9]\.0 (compatible;\s+MSIE~i'

"MSIE", .

, @Michal, get_browser ()

+1

use $_SERVER['HTTP_USER_AGENT']

Check if it contains "IE" and then the number.

I would prefer to do this only in javascript.

if (typeof window.onbeforeunload !== "undefined") window.onbeforeunload = myFunc;
else window.onunload = myFunc;
0
source

Javascript libraries are much better at detecting and handling browser behavior. Just let a mature library like jQuery handle it and everything will be fine.

0
source

Source: https://habr.com/ru/post/1741571/


All Articles