PHP tips & tricks – how to determine the operating system of the web server

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


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;
        $os = 3;

    return $os;

As you can see this function returns 1 for Windows, 2 for Linux and 3 for other systems.


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.


  • Choose the theme or the plugin that most suits to your online business
  • Download and install it freely with few clicks
  • When you will be positive about your choice, enjoy the advanced features
Your Inspiration Themes >
[pdf]Scarica articolo in PDF[/pdf]
Tags: , ,

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

Related Posts

You may be interested in the following articles:


Trackback e pingback

  1. Tweets that mention PHP tips & tricks – how to determine the operating system of the web server | Your Inspiration Web --
    [...] This post was mentioned on Twitter by qualithemes, junichi_y, Toni DG and others. Toni DG said: Cómo determinar el …

Leave a Reply