Improve company productivity with a Business Account.Sign Up


PHP: Detect operating system from command line

Posted on 2007-11-21
Medium Priority
Last Modified: 2010-04-21
I could do with a routine to be able to detect the current operating system from which php when it is run external to the web server.  Please note: I am not interested in browser detection routines - they are not applicable and I know there are plenty of resources available for that.  I am interested solely in detecting the operating system on which php is running upon.

So far, for MS Windows, the _ENV["OS"] and _SERVER["OS"] are useful, but these do not necessarily exist on other operating systems.
Question by:Barthax
  • 3
  • 3
LVL 11

Expert Comment

ID: 20327038
It is difficult to answer your question when you have already discounted the three most common methods for finding the operating system.  For those who are reading this for uncomplicated reasons, these are:

1) The variable _ENV["OS"] holds the value of the 'OS' variable from the environment under which the PHP server process is running.  If this variable is not set in the environment, it will not be available in the _ENV array.

2) The _SERVER[...] array should hold all values specified in CGI 1.1, and *may* include other variables, including the Operating System sometimes named "OS".  The existence of this variable is at the discretion of the server administrator.

3) The  'browser detection' method uses a variable guarenteed to be in the _SERVER array: "HTTP_USER_AGENT", and will normally include the operating system, although this can be over-ridden by the server administrator.

All three methods are subject to the server administrator allowing you to know what operating system your process is running on.

There are methods available to help you, but this is not guarenteed.  In order for me to answer your question, you will need to answer a few of mine.  I am more interested in what you are trying to achieve, and may be able to give you an answer to your problem, rather than some method to set a variable when the OS is unknown.

1) What operating systems do the _ENV["OS"] or _SERVER["OS"] variables NOT exist on?

2) Is this a server of your own, or are you using shared hosting?  Or are you writing an application which will be distributed to many people, and you can't guarentee which OS it is running on?

3) Why do you need to know what the OS is?

4) If it is for a single server, will you have access to run system commands?

LVL 19

Author Comment

ID: 20327840
To answer your queries:

1) "So far, for MS Windows, the _ENV["OS"] and _SERVER["OS"] are useful"

2) Under my control, they are my servers and I have full control, but the application is being written with distribution in mind (as yet the audience is undefined) and therefore cannot make any guarantee.

3) The application has already been evolving for a couple of years for my personal use and I have a desire to expand its use beyond its current Win32 GnuWin32/Cygwin environments.  It is run via command line - there is no web server involved & therefore no CGI wrapper nor browser to detect.  The reason for the detection of the OS is to support external commands to be executed against files or data where direct php support is missing or unconfigured, or otherwise not possible.  While PHP happily accepts either \\ or / for paths, it is not possible to write an exec() (for example) which runs on all possible platforms.  Therefore I need to detect the OS in order to have a chance of getting the command line correct for the external application (via conditional include).

To give a simple example, assuming a GnuWin32 vs. Amiga vs. Linux environments, if I run a legacy compression utility (LHA for example but the app is extensible & therefore must be written to be adaptable), the option parameters' format may be the same across both platforms, but the path statements to the files in use will not be.

4) As 2).
LVL 11

Expert Comment

ID: 20327865

Hi Barthax,

Sorry for my stupidity if I mesunderstand it, but your answer in (1) does not seem to answer the question:

   What operating systems do the _ENV["OS"] or _SERVER["OS"] variables NOT exist on?

Can you elaborate please?

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

LVL 11

Accepted Solution

siliconbrit earned 2000 total points
ID: 20328136

If you are running PHP from the command line, then the information is known to the user before you execute the command, in this case, you can pass in a statement to the php command-line interpreter to state what operating system you are using:

  php -d os=windows myapplication.php

Then use ini_get ('os') to retrieve that value.

Having said that, you are also in control of the environment when you run this command, so you can set and environment variable _ENV["OS"] if it doesn't already exist:

Example from unix:

   export OS="linux"
   php myapplication.php


However, each of these has it's own overhead for a distributed application, and you still need to keep a static table somewhere in your code that tells you which commands to use or what path delimiter you need.

Before I move on, the PEAR package has some useful OS interrogation functions among other things.

So, basically there are options available but your choice will be determined by what specifically you need to know about each OS.  If your problem is limited to paths, then you can use the forward slash on most operating systems (including windows) to build a UNC path.  Depending on why you need the path, it may be sufficient to get the current working directory using getcwd() and then extend or edit that string to build the path you need.

For system specific commands, you are probably best advised to provide a configuration file and have the user enter the command syntax for every external command that might be called via the PHP function system().  However there are platform specific issues that need to be dealt with that are different for each OS, so even if you know which operating system you are on, and the correct command syntax required to run that command, the platform may not be permissioned or configured for you to do so.

For this reason, my advice would be one of:

1) Write platform specific distributions with clear and complete installation/configuration instructions for each.  Include a platform configuration file for all platforms you have already tested on, and a blank one for those you haven't.

2) Think again about what you need to achieve and see if there are standard modules in PHP or Pear that can be used to achieve the same or a similar result.

3) Consider a richer platform-independent programming language with lower-level native modules such as Java.

There are other options, but I'll leave it here for the moment to see if any of this was helpful to you, or you would like me to take a different direction.
LVL 19

Author Comment

ID: 20328338
> What operating systems do the _ENV["OS"] or _SERVER["OS"] variables NOT exist on?
- MS Windows (specifically MS Windows 32-bit) can be relied upon to have an environment label called OS - which PHP pulls in as _ENV["OS"] and makes available through the _SERVER["OS"] also.  (BTW: I've just realised I left out the $ before _ENV/_SERVER).

Already the app has a wealth of configuration possibilities & is extensible.  I favour exec() over system() as the entire output can be returned if necessary and passed onto a proprietary function should such be configured...

The problem is principally limited to paths but not within PHP - I know I can execute any command within PHP via either slash type, but the parameter passed to the external app must be of particular form.  Add to that under MS Windows, various legacy CLI apps require the short name paths which I have already coped with - but this is obviously not required under (for example) linux.

I figured being able to detect the OS would aid in the segregation of the configurations via includes enabling simpler pooling of most of the functions. I guess I will have to live with separate subdirectories where the OS-specific stuff lives and have the user specify the OS through some means as you suggest.

Many thanks.
LVL 19

Author Closing Comment

ID: 31410334
The was no single answer & therefore cannot be complete nor accurate - it is, however, as complete and as accurate as possible within the confines of the query.

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
I recently worked on a Wordpress site that utilized the popular ContactForm7 ( plug-in that only sends an email and does not save data. The client wanted the data saved to a custom CRM database. This is my solution.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

587 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question