BLAKE
BLAKE

Reputation: 505

Can PHP tell if the server os is 64-bit?

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?

Upvotes: 24

Views: 14730

Answers (12)

Salman Arshad
Salman Arshad

Reputation: 272106

Windows

That is actually two questions (both are wrong):

Determine if Operating System is 32 or 64-bit

Look at the output of php_uname; it will look something like:

Windows NT DESKTOP-4R78KQM 10.0 build 19045 (Windows 10) AMD64

Somewhere in the operating system description you will find the necessary information, in the above example it would be AMD64.

Determine if PHP executable is 32 or 64-bit

Starting from PHP 7, 64-bit builds support 64-bit integers. So you could just check if PHP_INT_SIZE is 4 (32-bit) or 8 (64-bit).


Note about Environment Variables, Registry APIs etc

Some architecture specific environment variables such as PROCESSOR_ARCHITECTURE are changed for 32-bit programs running on 64-bit windows:

C:\Windows\System32>echo %PROCESSOR_ARCHITECTURE%
AMD64

C:\Windows\SysWOW64>echo %PROCESSOR_ARCHITECTURE%
x86

Likewise, registry APIs will transparently return data from the correct node depending on the bitness of the program calling those functions. There is no need to specify Wow6432Node in the path.

Upvotes: 30

Ivijan Stefan Stipić
Ivijan Stefan Stipić

Reputation: 6668

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.

Upvotes: 0

Tim Penner
Tim Penner

Reputation: 3541

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

Upvotes: 0

Simon Rig&#233;t
Simon Rig&#233;t

Reputation: 2895

No need to do calculations. Just check the PHP_INT_SIZE constant:

if(PHP_INT_SIZE>4)
  // 64 bit code
else
  // 32 bit code

The size of integers is a good indicator, but not bulletproof. Someone might run a 32 bit app on a 64 bit system.

$_SERVER['SERVER_SOFTWARE'] and $_SERVER['SERVER_SIGNATURE'] might tell you something useful, depending on the implementation of the server.

Upvotes: 3

emcconville
emcconville

Reputation: 24419

I've had luck with bit-shifting, and taking advantage boolean casting.

function is64bit()
{
  return (bool)((1<<32)-1);
}
// or
function is32bit()
{
  return 1<<32 === 1;
}

Upvotes: 4

Kristoffer la Cour
Kristoffer la Cour

Reputation: 2576

Note: This solution is a bit less convenient and slower than @Salman A's answer. I would advice you to use his solution and check for PHP_INT_SIZE == 8 to see if you're on a 64bit os.

If you just want to answer the 32bit/64bit question, a sneaky little function like this would do the trick (taking advantage of the intval function's way of handling ints based on 32/64 bit.)

<?php
function is_64bit()
{
    $int = "9223372036854775807";
    $int = intval($int);
    if ($int == 9223372036854775807) {
        /* 64bit */
        return true;
    } elseif ($int == 2147483647) {
        /* 32bit */
        return false;
    } else {
        /* error */
        return "error";
    }
}
?>

You can see the code in action here: http://ideone.com/JWKIf

Note: If the OS is 64bit but running a 32 bit version of php, the function will return false (32 bit)...

Upvotes: 30

doublehelix
doublehelix

Reputation: 2322

if you have the COM extension installed (in php.ini) you can call the windows WMI service.

To check the OS:

function getOsArchitecture() {
    $wmi = new COM('winmgmts:{impersonationLevel=impersonate}//./root/cimv2');
    $wmi = $obj->ExecQuery('SELECT * FROM Win32_OperatingSystem');
    if (!is_object($wmi)) {
        throw new Exception('No access to WMI. Please enable DCOM in php.ini and allow the current user to access the WMI DCOM object.');
    }
    foreach($wmi as $os) {
        return $os->OSArchitecture;
    }
    return "Unknown";
}

or, check the physical processor:

function getProcessorArchitecture() {
    $wmi = new COM('winmgmts:{impersonationLevel=impersonate}//./root/cimv2');

    if (!is_object($wmi)) {
        throw new Exception('No access to WMI. Please enable DCOM in php.ini and allow the current user to access the WMI DCOM object.');
    }
    foreach($wmi->ExecQuery("SELECT Architecture FROM Win32_Processor") as $cpu) {
        # only need to check the first one (if there is more than one cpu at all)
        switch($cpu->Architecture) {
            case 0:
                return "x86";
            case 1:
                return "MIPS";
            case 2:
                return "Alpha";
            case 3:
                return "PowerPC";
            case 6:
                return "Itanium-based system";
            case 9:
                return "x64";
        }
    }
    return "Unknown";
}

Upvotes: 1

Chris0
Chris0

Reputation: 473

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;
}

Upvotes: 15

user1568407
user1568407

Reputation: 29

Or use PHP COM to call wmi

$obj = new COM('winmgmts://localhost/root/CIMV2');
$wmi = $obj->ExecQuery('Select * from Win32_OperatingSystem');
foreach($wmi as $wmiCall)
{
    $architecture = $wmiCall->OSArchitecture;
}

Upvotes: 2

A bit of a late answer, but if you just want to determine the word size, you can use this: (log(PHP_INT_MAX + 1, 2) + 1)

Upvotes: 0

John Himmelman
John Himmelman

Reputation: 22000

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.

Upvotes: 7

RageZ
RageZ

Reputation: 27313

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.

Upvotes: 1

Related Questions