(help) js to php function

irfan666

Newbie
Joined
Oct 3, 2015
Messages
20
Reaction score
1
please help me to convert js to php function thanks function get_browser(){ var N=navigator.appName, ua=navigator.userAgent, tem; var M=ua.match(/(opera|chrome|safari|firefox|msie)\/?\s*(\.?\d+(\.\d+)*)/i); if(M && (tem= ua.match(/version\/([\.\d]+)/i))!= null) M[2]= tem[1]; M=M? [M[1], M[2]]: [N, navigator.appVersion, '-?']; return M[0]; }
 
Please post your code in
Code:
 tags in the future.

Your js code for browser detection:

[CODE] function get_browser() {
     var N = navigator.appName,
         ua = navigator.userAgent,
         tem;
     var M = ua.match(/(opera|chrome|safari|firefox|msie)\/?\s*(\.?\d+(\.\d+)*)/i);
     if (M && (tem = ua.match(/version\/([\.\d]+)/i)) != null) M[2] = tem[1];
     M = M ? [M[1], M[2]] : [N, navigator.appVersion, '-?'];
     return M[0];
 }

PHP alternative for browser detection (using user agent):
Code:
[COLOR=#000000][COLOR=#0000BB]<?pphp
[/COLOR]echo $_SERVER['HTTP_USER_AGENT'];[COLOR=#007700]
[/COLOR][COLOR=#0000BB]?>[/COLOR] [/COLOR]

This will output full user Agent, for example:
Mozilla/5.0 (Windows NT 6.3; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0

If you want to output just browser name (like on your javascript code) use this:

Code:
<?php
if(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE)
echo 'msie';
elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox') !== FALSE)
echo 'firefox';
elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'Chrome') !== FALSE)
echo 'chrome';
elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'Opera') !== FALSE)
echo "Opera";
elseif(strpos($_SERVER['HTTP_USER_AGENT'], 'Safari') !== FALSE)
echo "safari";
else
echo 'Unkown browser';
?>
 
Last edited:
thanks Nitros but how about to make php function with that script ? thanks again
 
thanks Nitros but how about to make php function with that script ? thanks again

Code:
function browsercheck(){
if(strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== FALSE)
$browser = 'msie';
else if(strpos($_SERVER['HTTP_USER_AGENT'], 'Firefox') !== FALSE)
$browser = 'firefox';
else if(strpos($_SERVER['HTTP_USER_AGENT'], 'Chrome') !== FALSE)
$browser = 'chrome';
else if(strpos($_SERVER['HTTP_USER_AGENT'], 'Opera') !== FALSE)
$browser = "Opera";
else if(strpos($_SERVER['HTTP_USER_AGENT'], 'Safari') !== FALSE)
$browser = "safari";
else
$browser = 'Unkown browser';
return $browser;
}
 
It's not bad, but I think it's a very basic way to for checking the UA (user-agent). If you're lucky, it will be correct in 90% of cases, not more. Because UA sometimes get extremely complex and confusing.

If you want something more advanced, I would suggest you to use this php library: serbanghita/Mobile-Detect (you can find it on GitHub). Its primary purpose is mobile detection, but you can use it for identifying all kind of devices by calling the appropriate methods, like: isChrome(), isSafari(), etc.
 
Back
Top