PHP 能否判断服务器操作系统是否为 64 位? [英] Can PHP tell if the server os is 64-bit?

查看:26
本文介绍了PHP 能否判断服务器操作系统是否为 64 位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里处理的是 Windows.

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

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

PHP 能知道它在什么操作系统上运行吗?PHP 能否判断操作系统是 64 位还是 32 位?

解决方案

注意:这个方案比 @Salman A 的回答.我建议您使用他的解决方案并检查 PHP_INT_SIZE == 8 以查看您是否使用 64 位操作系统.

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

您可以在此处查看正在运行的代码:http://ideone.com/JWKIf

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

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 the any way that PHP can detect the server's OS?

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

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

解决方案

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

这篇关于PHP 能否判断服务器操作系统是否为 64 位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆