PHP tips & tricks – how to determine the operating system of the web server
by Maurizio Tarchini | 10 Jan 2011 | in: php, Web

A question that I often hear is: How can I determine the operating system of the server?
Many strange answers are given to this question, the most common one involves the application of improbable regular expressions to the variable
$_SERVER[‘SERVER_SOFTWARE’].
Before we see a more elegant and secure solution, we briefly answer this question:
Why do you have to determine the operating system of the server?
Simple. In rare circumstances PHP behaves differently depending on the operating system on which it runs, if it’s Linux or Windows (a significant example of this difference we’ll see in the next pill). Now, if the script that you’re developing is for you, there’s no problem. You know the operating system of your server and you’ll move accordingly. But if you intend to distribute the code, you should take in consideration all these differences and therefore you have to detect the operating system.
How to detect the operating system?
PHP provides the PHP_OS constant that returns the operating system, but… there’s always a but!
If for the Linux operating systems PHP_OS returns the string “Linux”, for the windows systems it can give us back (depending on what type of system) at least three values: WINNT, WIN32, Windows.
But this isn’t a big problem. All we have to do is capitalize the entire string and check that the first three letters are WIN.
Good. I propose a simple function to determine if the operating system is Linux, Windows or others.
function serverOS()
{
$sys = strtoupper(PHP_OS);
if(substr($sys,0,3) == "WIN")
{
$os = 1;
}
elseif($sys == "LINUX")
{
$os = 2;
}
else
{
$os = 3;
}
return $os;
}
As you can see this function returns 1 for Windows, 2 for Linux and 3 for other systems.
Conclusion
In this article we explored a particularity of PHP and we resolved in an elegant way the determination of the operating system of the web server. As I said this procedure will come handy in those cases where the type of operating system determines differences as we’ll see in the next pill.
*****************************************
L'immagine principale dell'articolo è stata fornita da @Fotolia
The Author
Maurizio is married to the triad PHP - MySql - Apache and, not enough, he has a lover called jQuery. He has a blog where he tries to describe in detail all of "his lovers". His real specialty is the realization of large business application, altough he never refuses the commitment of a website.
Author's web site | Other articles written by Maurizio Tarchini
Related Posts
You may be interested in the following articles:
PHP Tips – Warning: Cannot modify header information. Let’s get things straightTell me the truth: you have had to mix it up with this error at least once, haven't you? In this pill we'll get things straight, once and for...
PHP Tips – Manage correctly file inclusionIn this article we’ll treat a quite simple argument but if badly managed can lead to big problems, the topic is the inclusion of...
Elements of cryptography: The symmetrical cryptographyIn the previous article we saw a particular case of the cryptographyapplication, that is hashing. We saw how useful it is in coding passwords...
2 comments
Trackback e pingback
-
Tweets that mention PHP tips & tricks – how to determine the operating system of the web server | Your Inspiration Web -- Topsy.com
[...] This post was mentioned on Twitter by qualithemes, junichi_y, Toni DG and others. Toni DG said: Cómo determinar el ...

Most of the time you just need the directory separator, and in php you can just use / on both unix and windows platforms and php will figure out the rest.