0

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?

3
Contributors
11
Replies
33
Views
2 Years
Discussion Span
Last Post by Niloofar24
Featured 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' … Read More

  • 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 … Read More

  • > 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](http://php.net/manual/en/function.preg-match.php) go suit yourself, and here's [another one](http://webcheatsheet.com/php/regular_expressions.php). The example I provided is rather "hardcore" … Read More

2
<?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.

Edited by Aeonix

0

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.

Edited by Niloofar24

0

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)

Edited by Aeonix

0

Well would you please explain it for me?

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

I mean / and / and i.

Edited by Niloofar24

1

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).

Edited by Aeonix

0

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.

Edited by Niloofar24

0

So, i solved it in this way:

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

So would it be correct?

2

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?

Edited by Aeonix

0

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

Oh no! just want to learn everything deeply :D

Thank you for the links.

Edited by Niloofar24

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.