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

2022-08-30 17:21:52

I am dealing with Windows here.

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

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

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


答案 1

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 to see if you're on a 64bit os.PHP_INT_SIZE == 8

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


答案 2

To check the size of integer (4/8 bytes) you can use the constant. If then you have a 64-bit version of PHP. 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.PHP_INT_SIZEPHP_INT_SIZE===8PHP_INT_SIZE===4

On Windows+IIS there is a variable that contains when tested on my system (WinXP-32bit). I think it will contain when running on a 64bit OS.$_SERVER["PROCESSOR_ARCHITECTURE"]x86x64


推荐