簡體   English   中英

使用 PHP 查找 Windows 32 位或 64 位

[英]Find Windows 32 or 64 bit using PHP

是否可以獲得 window 處理器位? 我想使用 php 找到 window 處理器位? 我有編碼來查找操作系統和其他屬性。 好心勸告。 謝謝 - 韓

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

此代碼片段至少會告訴您 PHP 的 32/64 位版本是否正在運行。

一種更短且更健壯的獲取位數的方法。

    strlen(decbin(~0));

這是如何工作的:

按位補碼運算符,波浪號~翻轉每一位。

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

在 integer 的每個位上使用這個0開關。

這為您提供了 PHP 安裝可以處理的最大數量。

然后使用 decbin() 會給你這個數字的二進制形式的字符串表示

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

strlen 會給你位數。

這是可用的 function

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

如果您安裝了COM擴展(在 php.ini 中),您可以調用 windows WMI 服務。

(請記住,如果您有 64 位處理器、64 位操作系統和 64 位 PHP,您的整數仍將是 32 位,因為 Windows 上的 x64-PHP 限制。)

反正...

檢查操作系統:

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

或者,檢查物理處理器:

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

你可以這樣寫一個 function :

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

然后你可以像這樣使用它:

if( is_32bit() ) {
    // do 32 bit stuffs
}
function bits() {
  return PHP_INT_SIZE * 8;
}

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

另一種選擇:

function IsAtLeast64Bit()
{
    return defined('PHP_INT_SIZE') && PHP_INT_SIZE >= 8;
}

如果定義了 PHP_INT_SIZE,它只返回 TRUE。 這應該適用於所有 64 位系統。 未來超過 64 位的系統也得到管理。

<?php echo $_SERVER['HTTP_USER_AGENT']; ?>
它包含在變量中,您可以將其分解並從中派生

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM