windows PHP 能否判断服务器操作系统是否为 64 位?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2353473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:02:34  来源:igfitidea点击:

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

phpwindows32bit-64bit

提问by BLAKE

I am dealing with Windows here.

我在这里处理 Windows。

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?

我知道您可以使用该$_SERVER['HTTP_USER_AGENT']变量来检测查看页面的浏览器的操作系统,但是 PHP 可以通过任何方式检测服务器的操作系统吗?

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

对于我的程序的用户界面,我使用的是 PHP 网页。我需要读取注册表中位于 64 位操作系统不同位置的Wow6432Node键(它在键下)。

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

PHP 可以告诉它在什么操作系统上运行吗?PHP 能否判断操作系统是 64 位还是 32 位?

回答by Kristoffer la Cour

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

注意:这个解决方案比@Salman A 的答案更不方便和慢。我建议您使用他的解决方案并检查PHP_INT_SIZE == 8您是否使用 64 位操作系统。

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

如果您只想回答 32 位/64 位问题,那么像这样一个狡猾的小函数就可以解决问题(利用 intval 函数处理基于 32/64 位的整数的方式。)

<?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

您可以在此处查看正在运行的代码: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)...

注意:如果操作系统是 64 位但运行 32 位版本的 php,该函数将返回 false(32 位)...

回答by Salman A

To check the size of integer (4/8 bytes) you can use the PHP_INT_SIZEconstant. If PHP_INT_SIZE===8then you have a 64-bit version of PHP. PHP_INT_SIZE===4implies that a 32-bit version of PHP is being used but it does not imply that the OS and/or Processor is 32-bit.

要检查整数(4/8 字节)的大小,您可以使用PHP_INT_SIZE常量。如果PHP_INT_SIZE===8您有 64 位版本的 PHP。PHP_INT_SIZE===4暗示正在使用 32 位版本的 PHP,但并不暗示操作系统和/或处理器是 32 位。

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

在 Windows+IIS 上,有一个$_SERVER["PROCESSOR_ARCHITECTURE"]变量包含x86在我的系统(WinXP-32bit)上测试时。我认为它会x64在 64 位操作系统上运行时包含。

回答by Chris0

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

@see http://php.net/manual/en/language.operators.bitwise.php

Using this on 0switches on every bit for an integer.

0上使用它会打开整数的每一位。

This gives you the largest number that your PHP install can handle.

这为您提供了 PHP 安装可以处理的最大数量。

Then using decbin() will give you a string representation of this number in its binary form

然后使用 decbin() 将以二进制形式为您提供此数字的字符串表示

@see http://php.net/manual/en/function.decbin.php

@see http://php.net/manual/en/function.decbin.php

and strlen will give you the count of bits.

和 strlen 会给你位数。

Here is it in a usable function

这是一个可用的功能

function is64Bits() {
    return strlen(decbin(~0)) == 64;
}

回答by John Himmelman

Try using the php_unamefunction...

尝试使用php_uname函数...

<?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

来源 - 确定操作系统 - 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)...

这将在运行 Win 7(64 位)的 ibm t400 上返回以下字符串...

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

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.

不幸的是,它返回 WIN32,因为我运行的是 32 位版本的 apache。

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

您可以获得一般处理器信息(在 *nix 服务器上),通过使用 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.

如果您计划支持许多不同的操作系统,您可能需要使用这些方法的组合。

回答by emcconville

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

回答by user1568407

Or use PHP COM to call wmi

或者使用PHP COM调用wmi

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

回答by doublehelix

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

如果您安装了COM扩展程序(在 php.ini 中),您可以调用 Windows WMI 服务。

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

回答by Simon Rigét

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

无需进行计算。只需检查 PHP_INT_SIZE 常量:

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.

整数的大小是一个很好的指标,但不是防弹的。有人可能会在 64 位系统上运行 32 位应用程序。

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

$_SERVER['SERVER_SOFTWARE'] 和 $_SERVER['SERVER_SIGNATURE'] 可能会告诉您一些有用的信息,具体取决于服务器的实现。

回答by RageZ

you can use some script to output the Os type

您可以使用一些脚本来输出 Os 类型

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

这里有一个示例,说明如何使用 WMI 获取该信息。

You can call this script using execand read the output.

您可以使用调用此脚本exec并读取输出。

回答by Ain't Nobody Special

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)

有点晚的答案,但如果你只是想确定字的大小,你可以使用这个: (log(PHP_INT_MAX + 1, 2) + 1)