使用 PHP 查找 Windows 32 或 64 位

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6303241/
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 17:02:42  来源:igfitidea点击:

Find Windows 32 or 64 bit using PHP

phpwindows

提问by hjaffer2001

Is it possible to get the window processor bit?? I want to find the window processor bit using php?? I have coding to find the operating system and other properties. Kindly advice. Thanks - Haan

是否有可能获得窗口处理器位?我想使用 php 找到窗口处理器位?我有编码来查找操作系统和其他属性。友善的建议。谢谢 - 韩

回答by Salman A

<?php
switch(PHP_INT_SIZE) {
    case 4:
        echo '32-bit version of PHP';
        break;
    case 8:
        echo '64-bit version of PHP';
        break;
    default:
        echo 'PHP_INT_SIZE is ' . PHP_INT_SIZE;
}

This code snippet will at-least tell you if a 32/64 bit version of PHP is running.

此代码片段至少会告诉您是否正在运行 32/64 位版本的 PHP。

回答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 doublehelix

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

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

(Remember though that you event if you have a 64-bit processor, 64-bit OS and 64-bit PHP, your integers are still going to be 32-bit due to a limitation in x64-PHP on Windows.)

(请记住,如果您拥有 64 位处理器、64 位操作系统和 64 位 PHP,但由于 Windows 上 x64-PHP 的限制,您的整数仍将是 32 位。)

Anyway...

反正...

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 Tim Penner

You could write a function like this:

你可以写一个这样的函数:

function is_32bit(){
  return PHP_INT_SIZE === 4;
}

Then you could use it like this:

然后你可以像这样使用它:

if( is_32bit() ) {
    // do 32 bit stuffs
}

回答by Marcodor

function bits() {
  return PHP_INT_SIZE * 8;
}

echo 'Php running on '.bits(). ' bits system';

回答by Rohan Verma

<?php echo $_SERVER['HTTP_USER_AGENT']; ?>
it is contained in the variable, you could explode that and derive it from that

<?php echo $_SERVER['HTTP_USER_AGENT']; ?>
它包含在变量中,您可以将其分解并从中派生