1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

(help) js to php function

Discussion in 'PHP & Perl' started by irfan666, Dec 7, 2015.

  1. irfan666

    irfan666 Newbie

    Joined:
    Oct 3, 2015
    Messages:
    14
    Likes Received:
    0
    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]; }
     
  2. Nitros

    Nitros Power Member

    Joined:
    Jan 30, 2009
    Messages:
    580
    Likes Received:
    298
    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:
    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';
    ?>
     
    • Thanks Thanks x 2
    Last edited: Dec 7, 2015
  3. irfan666

    irfan666 Newbie

    Joined:
    Oct 3, 2015
    Messages:
    14
    Likes Received:
    0
    thanks Nitros but how about to make php function with that script ? thanks again
     
  4. Nitros

    Nitros Power Member

    Joined:
    Jan 30, 2009
    Messages:
    580
    Likes Received:
    298
    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;
    }
     
  5. irfan666

    irfan666 Newbie

    Joined:
    Oct 3, 2015
    Messages:
    14
    Likes Received:
    0
    thanks a lot Nitro, youre my hero
     
  6. flaveris

    flaveris Newbie

    Joined:
    Jul 12, 2013
    Messages:
    16
    Likes Received:
    2
    Occupation:
    Web Developer
    Home Page:
    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.