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

后端 未结 12 570
北荒
北荒 2020-12-09 08:52

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

相关标签:
12条回答
  • 2020-12-09 09:15

    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.

    0 讨论(0)
  • 2020-12-09 09:20

    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)

    0 讨论(0)
  • 2020-12-09 09:21

    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";
    }
    
    0 讨论(0)
  • 2020-12-09 09:22

    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;
    }
    
    0 讨论(0)
  • 2020-12-09 09:29

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

    0 讨论(0)
  • 2020-12-09 09:33

    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;
    }
    
    0 讨论(0)
提交回复
热议问题