0

I want to take these:

$xp = '(Windows NT 5.1)';
$vista = '(Windows NT 6.0)';
$win7 = '(Windows NT 6.1)';

And put it into a function win{}.

Basically I want it so that if a person is using a windows' OS the output would be $win so I can then use it as an if else listing...

Or would I be better off using this in an array?

Is this possible? I know it may sound confusing so I am sorry if it is, I really don't know how to explain this correctly.

Matt Ridge
  • 3,633
  • 16
  • 45
  • 63
  • 2
    Can't you just check for presence of string `'windows'` in user agent string? – anubhava Jan 05 '12 at 18:08
  • @anubhava I'm using this as an example. Replace it with anything else you want. – Matt Ridge Jan 05 '12 at 18:12
  • @MattRidge: What are you trying to do then? Replace what with anything? Aren't you checking for Windows? – gen_Eric Jan 05 '12 at 18:13
  • 1
    As @Rocket said, it would be better if you tell us what exactly are you trying to achieve by declaring these variables or an array? – anubhava Jan 05 '12 at 18:16
  • I'm trying to make it so that if a person is using an OS and browser combination that it will use a specific .css script. I also wanted to stay generic so it could be used elsewhere as well. – Matt Ridge Jan 05 '12 at 18:31
  • If I understood your question you can have the OS Name doing this -> $_SERVER['HTTP_USER_AGENT'] and you got -> Mozilla/5.0 (Windows NT 5.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1nn – devasia2112 Jan 05 '12 at 18:32

3 Answers3

2

I'm not sure about the exact platform results available when calling get_browser, but this should be close:

$win = Array('WinXP','WinVista','Win7');
$browser = get_browser(null, true);
if(array_search($browser['platform'],$win))
{
   echo('The user is using ' . $browser['platform'] . 'and it is contained in my array.');
}

Determine client OS in PHP

Community
  • 1
  • 1
Jeremy Harris
  • 24,318
  • 13
  • 79
  • 133
  • the ones I am using are the results if you use HTTP_USER_AGENT. So for your example I'd have the three listed in what you have in place there? Just curious how does that actually work? I know it works, I just don't understand exactly how yet. Can you explain a little? – Matt Ridge Jan 05 '12 at 18:17
  • In the get_browser documentation, I see them using "WinXP", "WinNT", and "Windows" as some of the options returned for Windows platforms. I'd imagine there are more. You would just add/modify the $win array to include expected platform types you'd like to search for. – Jeremy Harris Jan 05 '12 at 18:20
  • I know what it means, I am saying that with the code above I can't do what is required.I'm not opening up my server to more tweaks. The last thing I need right now is the accidental ability to open a back door and let someone in by accident. – Matt Ridge Jan 07 '12 at 17:17
1

Instead of putting them into an array, if you don't need the exact version of windows the person is using, I would run something like a strpos to see if windows exists in their user agent.

If you do need to know the exact version they're using, I would recommend putting them in a key => value pair array since you'll have a lot more versions of windows than that.

Francis Lewis
  • 8,872
  • 9
  • 55
  • 65
1

Yes, use an array. Otherwise when Windows 8 comes out, you'll need to add new code.

Even better code a dedicated function - something like isWindows - which receives a string and returns a Boolean based on whether the string contains Windows. Then, how you actually do the detection is contained within the function.

Dan Blows
  • 20,846
  • 10
  • 65
  • 96
  • On an iphone so excuse quality, but something ike $windows = array(windows1,windows2); return in_array($string, $windows); – Dan Blows Jan 05 '12 at 18:26