3

I need to determine the type of OS the PHP server is running on. By type, I mean strings like "windows" or "linux", not "wince", "winnt" etc.

So far, I have to leads: PHP_OS and uname(), the later being more reliable than the earlier (PHP_OS says what OS PHP was built on - according to documentation).

ThinkingStiff
  • 64,767
  • 30
  • 146
  • 239
Christian
  • 27,509
  • 17
  • 111
  • 155
  • Does `php_uname()` not fulfill your requirement? If not, why not? – Pekka Oct 16 '10 at 10:49
  • 1
    I need the OS type, but php_uname returns a lot of extra details which makes detection hard (eg: winnt vs wince vs darwin => windows windows osx). Plus, I also don't (can't) know all types of strings I can get. For instance, I don't run solaris or bsd - and at this point it is impossible to run my own php on those systems. – Christian Oct 16 '10 at 10:52
  • @Christian this seems to be one of the things that look totally easy, but at the same time are impossible to do 100% reliably. Care to elaborate what exactly you need this for? Maybe that leads to an idea about some perfect indicator – Pekka Oct 16 '10 at 11:28
  • @Pekka - It's a generic function to distinguish between OSes. I want a guy detecting hpux to be able to this as easily as the guy detecting windows. Predictable results are relatively important. – Christian Oct 16 '10 at 11:33
  • @Christian hmm, the only idea that comes to mind is to open a Community Wiki question asking people to report what `uname()` returns on their system. I imagine it's going to be relatively consistent on Unixes/Linuxes where there actually *is* a built-in uname(), and I'm sure it follows some sort of convention on Windowses as well. It's just going to be a question of collecting as many possible outcomes as possible. Let me know if you decide to do this, I can contribute info on Win7, Server 2003 and 2008 – Pekka Oct 16 '10 at 11:38
  • @Pekka - That is actually an excellent idea. I never did this before, but is it possible to involve serverfault users as well? Sounds like lots of people from there have access to this info. – Christian Oct 16 '10 at 11:42
  • @Christian good idea, but SO's strict rules about duplicate will make it difficult to ask the same question on both sites. I would focus on one site - Serverfault could be a good pick too, but it may have more traction on SO – Pekka Oct 16 '10 at 11:46
  • OK, but how do I do the community question thingy? – Christian Oct 16 '10 at 11:48
  • @Christian ask it and tick the "Community Wiki" checkbox in the form – Pekka Oct 16 '10 at 11:49
  • Hmm, I suspect I don't have enough score to do this - I don't see that tick box. – Christian Oct 16 '10 at 11:52
  • @Christian oh, indeed, I don't see it either. Wtf? Hang on, I'll check – Pekka Oct 16 '10 at 11:52
  • @Christian there has been a [recent change](http://meta.stackexchange.com/questions/392/should-the-community-wiki-police-be-shut-down). I'd ask it as a normal question and if anybody complains that it should be CW, point them to that page – Pekka Oct 16 '10 at 11:54

1 Answers1

8

It's important to know that no non-Windows OS string is going to contain the text "win", and no non-OSX OS string is going to contain the word "darwin", and so on. Detecting the OS is easy.

$uname = strtolower(php_uname());
if (strpos($uname, "darwin") !== false) {
    // It's OSX
} else if (strpos($uname, "win") !== false) {
    // It's windows
} else if (strpos($uname, "linux") !== false) {
    // It's Linux
} else {
    // It's something your script won't run on
}
Yarin
  • 173,523
  • 149
  • 402
  • 512
mellowsoon
  • 22,273
  • 19
  • 57
  • 75
  • It detects OSX as windows ;-). I've got this code already, but as I said, it ain't reliable. – Christian Oct 16 '10 at 11:31
  • Now I'm not calling you a liar or crazy, but that sounds impossible! haha – mellowsoon Oct 16 '10 at 11:35
  • 3
    P.S. Note the order of the if than statements. It will grab the "win" in "darwin" if you're checking for OSX first. – mellowsoon Oct 16 '10 at 11:36
  • Oh and by the way, do you know the above conditionals for other OSes (hpux, xenix, haiku...)? – Christian Oct 16 '10 at 11:39
  • 2
    With regards to your PS, that is exactly why I replied so above. It will see "win" in "darwin" and think it is "windows". – Christian Oct 16 '10 at 11:41
  • "With regards to your PS, that is exactly why I replied so above. It will see "win" in "darwin" and think it is "windows"." Er, no. This is why you first check for windows. If the first if/then statement is satisfied, the rest are ignored. If will only check the "darwin" string if the windows string failed. – mellowsoon Oct 16 '10 at 11:57
  • As far as other OS's.. There's no point in checking for OS's that PHP can't even be installed on. – mellowsoon Oct 16 '10 at 12:02
  • Pseudocode 1: "windows" contains "win"? Yes, therefore it is windows. Pseudocode 2: "darwin" contains "win"? Yes, therefore it is windows. – Christian Oct 16 '10 at 12:11
  • 2
    the correct order would be: check for "darwin", check for "win", check for "linux"... – slosd Oct 16 '10 at 12:30
  • slosd is right. You DO need to check for Darwin first. My bad ;) – mellowsoon Oct 16 '10 at 12:38
  • Tehehe. Just edit the code and we'll try to forget it ever happened :P – Christian Oct 16 '10 at 12:43
  • ahah Well now that you wrote that I can't! Maybe next time. ;) – mellowsoon Oct 16 '10 at 12:54
  • Instead of `if (strpos($uname, "win") !== false)` you can say `if (strpos($uname, "win") === 0)` to check that the string _starts_ with "win" and doesn't match "darwin". – CJ Dennis Sep 27 '19 at 05:32