Hi.
With $u_agent = $_SERVER['HTTP_USER_AGENT']; i can get the users information but i just want to echo the OS that users use, something like:

Linux,Ubuntu

How can i get this out from the $u_agent variable?

Recommended Answers

All 11 Replies

<?php

$user_agent = $_SERVER["HTTP_USER_AGENT"];
function getOS() { 
    global $user_agent;
    $os_platform    =   "Unknown OS Platform";
    $os_array       =   array(
                            '/windows nt 10/i'     =>  'Windows 10',
                            '/windows nt 6.3/i'     =>  'Windows 8.1',
                            '/windows nt 6.2/i'     =>  'Windows 8',
                            '/windows nt 6.1/i'     =>  'Windows 7',
                            '/windows nt 6.0/i'     =>  'Windows Vista',
                            '/windows nt 5.2/i'     =>  'Windows Server 2003/XP x64',
                            '/windows nt 5.1/i'     =>  'Windows XP',
                            '/windows xp/i'         =>  'Windows XP',
                            '/windows nt 5.0/i'     =>  'Windows 2000',
                            '/windows me/i'         =>  'Windows ME',
                            '/win98/i'              =>  'Windows 98',
                            '/win95/i'              =>  'Windows 95',
                            '/win16/i'              =>  'Windows 3.11',
                            '/macintosh|mac os x/i' =>  'Mac OS X',
                            '/mac_powerpc/i'        =>  'Mac OS 9',
                            '/linux/i'              =>  'Linux',
                            '/ubuntu/i'             =>  'Ubuntu',
                            '/iphone/i'             =>  'iPhone',
                            '/ipod/i'               =>  'iPod',
                            '/ipad/i'               =>  'iPad',
                            '/android/i'            =>  'Android',
                            '/blackberry/i'         =>  'BlackBerry',
                            '/webos/i'              =>  'Mobile'
                        );

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

        if (preg_match($regex, $user_agent)) {
            $os_platform    =   $value;
        }

    }   

    return $os_platform;

}

echo "Operating system is: " . getOS() . "<br />";
echo "Understood from: " . $_SERVER["HTTP_USER_AGENT"];

?>

Stolen, from here, then modified.

Be aware, this is easily spoofable. Works for me.

You can get some user agent strings here.

Thank you @Aeonix. Could you help me a little more?

When i delete line 24, the value for OS will be echo as Linux.
And if i put it back, the value will be echo as ubuntu.
I want the script to echo bout of them, LInux, Ubuntu.
How can i edit it?

Users may use different linux: arch, ubuntu, fedora and..... I want the script to echo that information.

I mean both OS and the Distribution of that.

I'm pretty sure, just out of my head, that if you replace:

                            '/linux/i'              =>  'Linux',
                            '/ubuntu/i'             =>  'Ubuntu',

With:

                            '/linux/i'              =>  'Unspecified Linux',
                            '/ubuntu/i'             =>  'Linux Ubuntu',
                            '/slackware/i'          =>  'Linux Slackware',
                            '/kali/i'               =>  'Linux Kali',
                            '/fedora/i'             =>  'Linux Fedora',
                            '/mint/i'               =>  'Linux Mint',
                            etc. etc. etc.

That it will work pretty well. There's almost no reason it shouldn't. Linux has technically always same pattern like Windows, of it's own of course. The only line that would change with same browser on same hardware, but different distribution, is just one single string that it should find.

Here's top 50 most known according to this website (maybe not trustworthy, but still covers 90% of most used Linux OSes)

Well would you please explain it for me?

  // what does /ubuntu/i means?
 '/ubuntu/i' => 'Ubuntu',

I mean / and / and i.

I'm not professor of PHP.

So far I understand it /ubuntu/ will seek for every "ubuntu" in the text, regardless whether it's at the begin, or in middle, or at the end. While just ubuntu seeks for straight text, so far example if your operating system would be called "Linux Ubuntu", /Ubuntu/ will say "Yes, I found it", while Ubuntu won't. Also i at the end indicates that you don't care about the case, so UbUNTU and ubuntu and Ubuntu and ubunTU will all be detected and treated exactly same way.

I wouldn't really be bothered with this if I were you, lets be happy together that it works as it gets sometimes as complicated as preg_match("/^[A-Za-z0-9'?!-\s\\\\]+$/", $field).

Thank you @Aeonix i undersood. The script can't echo both OS nad it's Distribution yet, i will check to see if i can solve it or have to back here for asking more helps.

And would it be ok if i ask you to explain this for me too!!
preg_match("/^[A-Za-z0-9'?!-\s\\\\]+$/", $field)
If you know it, Thank you.

So, i solved it in this way:

     '/Arch/i' => 'Arch Linux',
    '/ubuntu/i' => 'Ubuntu Linux',

So would it be correct?

And would it be ok if i ask you to explain this for me too!!
preg_match("/^[A-Za-z0-9'?!-\s\\]+$/", $field)
If you know it, Thank you.

You like torturing people, don't you :D?
Here's the webpage go suit yourself, and here's another one. The example I provided is rather "hardcore" one, that's why this one is on another website, someone made this enough complicated, he/she needed help with it.

So would it be correct?

It is supposed to work if I look at it. Why not try it?

You like torturing people, don't you :D?

Oh no! just want to learn everything deeply :D

Thank you for the links.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.