views:

310

answers:

2

I am dealing with Windows here.

I know you can use the $_SERVER['HTTP_USER_AGENT'] variable to detect the OS of the browser viewing the page, but is the any way that PHP can detect the server's OS?

For my program's UI I am using a php webpage. I need to read a key in the registry that is in a different location on a 64-bit OS (It is under the 'Wow6432Node' Key).

Can PHP tell what OS it is running on? Can PHP tell if the OS is 64-bit or 32-bit?

Thanks in advance for the help.

+3  A: 

Try using the php_uname function...

<?php
echo php_uname('s');/* Operating system name */
echo "<br />";
echo php_uname('n');/* Host name */
echo "<br />";
echo php_uname('r');/* Release name */
echo "<br />";
echo php_uname('v');/* Version information */
echo "<br />";
echo php_uname('m');/* Machine type */
echo "<br />";
echo PHP_OS;/* constant will contain the operating system PHP was built on */
?>

Source - Determine Operating System - http://www.sitepoint.com/forums/showthread.php?t=510565

Another method is to use...

 echo $_SERVER['SERVER_SOFTWARE']; 

This returns the following string on my ibm t400 running Win 7 (64bit)...

Apache/2.2.12 (Win32) DAV/2 mod_ssl/2.2.12 OpenSSL/0.9.8k mod_autoindex_color PHP/5.3.0 mod_perl/2.0.4 Perl/v5.10.0

Unfortunately, its returning WIN32 because I'm running the 32bit version of apache.

You can get general processor info (on a *nix server), by using the cmd...

echo system('cat /proc/cpuinfo');

You'll probably need to use a combination of the methods if you're planning on supporting many different OSes.

John Himmelman
Using `system()` and relying on the layout of `/proc`. Two of my least favorite things.
asveikau
I agree, you're going to need to try a system cmd to get the right info, and even then, if your Apache is 32bit, you are limited within that server app.
TravisO
Asveikau, agreed, but unfortunately there isn't a single reliable approach. The only solution is to use a mashup, and perhaps rely on system() as a last-resort.
John Himmelman
+1  A: 

you can use some script to output the Os type

here there is an example of how to get that information using WMI.

You can call this script using exec and read the output.

RageZ
+1 WMI is the right, "Windows-y" way to do this.
asveikau