Link to home
Start Free TrialLog in
Avatar of Barthax
BarthaxFlag for United Kingdom of Great Britain and Northern Ireland

asked on

PHP: Detect operating system from command line

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.
Avatar of siliconbrit
siliconbrit

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?

Avatar of Barthax

ASKER

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).

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?

Thanks
ASKER CERTIFIED SOLUTION
Avatar of siliconbrit
siliconbrit

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Barthax

ASKER

> 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.
Avatar of Barthax

ASKER

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.