Solved

PHP: Detect operating system from command line

Posted on 2007-11-21
6
3,419 Views
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.
0
Comment
Question by:Barthax
  • 3
  • 3
6 Comments
 
LVL 11

Expert Comment

by:siliconbrit
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?

0
 
LVL 19

Author Comment

by:Barthax
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).
0
 
LVL 11

Expert Comment

by:siliconbrit
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?

Thanks
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 11

Accepted Solution

by:
siliconbrit earned 500 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

   <?php
      OS=_ENV["OS"];
   ?>

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.
0
 
LVL 19

Author Comment

by:Barthax
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.
0
 
LVL 19

Author Closing Comment

by:Barthax
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.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now