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 t
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.
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.
Here is a one line solution to determine if PHP is executing in 64 bit or 32 bit mode:
empty(strstr(php_uname("m"), '64')) ? $php64bit = false : $php64bit = true;
After executing the line of code above the $php64bit
variable will be set to either true
or false
To check the size of integer (4/8 bytes) you can use the PHP_INT_SIZE
constant. If PHP_INT_SIZE===8
then you have a 64-bit version of PHP. PHP_INT_SIZE===4
implies that a 32-bit version of PHP is being used but it does not imply that the OS and/or Processor is 32-bit.
On Windows+IIS there is a $_SERVER["PROCESSOR_ARCHITECTURE"]
variable that contains x86
when tested on my system (WinXP-32bit). I think it will contain x64
when running on a 64bit OS.
A slightly shorter and more robust way to get the number of bits.
strlen(decbin(~0));
How this works:
The bitwise complement operator, the tilde, ~, flips every bit.
@see http://php.net/manual/en/language.operators.bitwise.php
Using this on 0 switches on every bit for an integer.
This gives you the largest number that your PHP install can handle.
Then using decbin() will give you a string representation of this number in its binary form
@see http://php.net/manual/en/function.decbin.php
and strlen will give you the count of bits.
Here is it in a usable function
function is64Bits() {
return strlen(decbin(~0)) == 64;
}
Looking into all of this answers and some other solutions + my idea to "ask computer directly" i get this universal solution:
function is_64bit()
{
// Let's ask system directly
if(function_exists('shell_exec'))
{
if (in_array(strtoupper(substr(PHP_OS, 0, 3)), array('WIN'), true) !== false || defined('DIRECTORY_SEPARATOR') && '\\' === DIRECTORY_SEPARATOR)
{
// Is Windows OS
$shell = shell_exec('wmic os get osarchitecture');
if(!empty($shell))
{
if(strpos($shell, '64') !== false)
return true;
}
}
else
{
// Let's check some UNIX approach if is possible
$shell = shell_exec('uname -m');
if(!empty($shell))
{
if(strpos($shell, '64') !== false)
return true;
}
}
}
// Check is PHP 64bit (PHP 64bit only running on Windows 64bit version)
if (version_compare(PHP_VERSION, '5.0.5') >= 0)
{
if(defined('PHP_INT_SIZE') && PHP_INT_SIZE === 8)
return true;
}
// bit-shifting can help also if PHP_INT_SIZE fail
if((bool)((1<<32)-1))
return true;
// Let's play with bits again but on different way
if(strlen(decbin(~0)) == 64)
return true;
// Let's do something more worse but can work if all above fail
// The largest integer supported in 64 bit systems is 9223372036854775807. (https://en.wikipedia.org/wiki/9,223,372,036,854,775,807)
$int = '9223372036854775807';
if (intval($int) == $int)
return true;
return false;
}
Is tested on various machines and for now I have positive results. Fell free to use if you see any purpose.