PHP脚本-检测是否在Linux或Windows下运行?

发布于 2024-11-04 19:06:52 字数 144 浏览 4 评论 0 原文

我有一个PHP脚本,可以放在windows系统上,也可以放在linux系统上。在这两种情况下我都需要运行不同的命令。

如何检测我所处的环境? (最好是 PHP,而不是聪明的系统黑客)

更新

澄清一下,脚本是从命令行运行的。

I have a PHP script that may be placed on a windows system or a linux system. I need to run different commands in either case.

How can I detect which environment I am in? (preferably something PHP rather than clever system hacks)

Update

To clarify, the script is running from the command line.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

瑶笙 2024-11-11 19:06:52

检查 PHP_OS 常量的值 文档

它将为您提供 Windows 上的各种值,例如 WIN32WINNTWindows

另请参阅:可能的值:PHP_OSphp_uname文档

if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
    echo 'This is a server using Windows!';
} else {
    echo 'This is a server not using Windows!';
}

Check the value of the PHP_OS constantDocs.

It will give you various values on Windows like WIN32, WINNT or Windows.

See as well: Possible Values For: PHP_OS and php_unameDocs:

if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
    echo 'This is a server using Windows!';
} else {
    echo 'This is a server not using Windows!';
}
╄→承喏 2024-11-11 19:06:52

您可以检查目录分隔符在 Windows 上是否为 /(对于 unix/linux/mac)或 \。常量名称是DIRECTORY_SEPARATOR

if (DIRECTORY_SEPARATOR === '/') {
    // unix, linux, mac
}

if (DIRECTORY_SEPARATOR === '\\') {
    // windows
}

You can check if the directory separator is / (for unix/linux/mac) or \ on windows. The constant name is DIRECTORY_SEPARATOR.

if (DIRECTORY_SEPARATOR === '/') {
    // unix, linux, mac
}

if (DIRECTORY_SEPARATOR === '\\') {
    // windows
}
软糯酥胸 2024-11-11 19:06:52
if (strncasecmp(PHP_OS, 'WIN', 3) == 0) {
    echo 'This is a server using Windows!';
} else {
    echo 'This is a server not using Windows!';
}

似乎比接受的答案更优雅一些。不过,上述使用 DIRECTORY_SEPARATOR 的检测是最快的。

if (strncasecmp(PHP_OS, 'WIN', 3) == 0) {
    echo 'This is a server using Windows!';
} else {
    echo 'This is a server not using Windows!';
}

seems like a bit more elegant than the accepted answer. The aforementioned detection with DIRECTORY_SEPARATOR is the fastest, though.

南薇 2024-11-11 19:06:52

从 PHP 7.2.0 开始,您可以使用常量 PHP_OS_FAMILY 检测正在运行的操作系统:

if (PHP_OS_FAMILY === "Windows") {
  echo "Running on Windows";
} elseif (PHP_OS_FAMILY === "Linux") {
  echo "Running on Linux";
}

请参阅 官方 PHP 文档 了解其可能的值。

Starting with PHP 7.2.0 you can detect the running O.S. using the constant PHP_OS_FAMILY:

if (PHP_OS_FAMILY === "Windows") {
  echo "Running on Windows";
} elseif (PHP_OS_FAMILY === "Linux") {
  echo "Running on Linux";
}

See the official PHP documentation for its possible values.

濫情▎り 2024-11-11 19:06:52

请注意,PHP_OS 报告 PHP构建的操作系统,其中不一定是当前运行的操作系统。

如果您使用的是 PHP >= 5.3,并且只需要知道您是否在 Windows 上运行,则测试 Windows 特定常量 的定义可能是一个不错的选择,例如:

$windows = defined('PHP_WINDOWS_VERSION_MAJOR');

Note that PHP_OS reports the OS that PHP was built on, which is not necessarily the same OS that it is currently running on.

If you are on PHP >= 5.3 and just need to know whether you're running on Windows or not-Windows then testing whether one of the Windows-specific constants is defined may be a good bet, e.g.:

$windows = defined('PHP_WINDOWS_VERSION_MAJOR');
凉城已无爱 2024-11-11 19:06:52

根据 预定义常量:用户贡献笔记 Volker 和 rdcapasso 解决方案,您可以简单地创建像这样的帮助器类:

<?php

class System {

    const OS_UNKNOWN = 1;
    const OS_WIN = 2;
    const OS_LINUX = 3;
    const OS_OSX = 4;

    /**
     * @return int
     */
    static public function getOS() {
        switch (true) {
            case stristr(PHP_OS, 'DAR'): return self::OS_OSX;
            case stristr(PHP_OS, 'WIN'): return self::OS_WIN;
            case stristr(PHP_OS, 'LINUX'): return self::OS_LINUX;
            default : return self::OS_UNKNOWN;
        }
    }

}

用法:

if(System::getOS() == System::OS_WIN) {
  // do something only on Windows platform
}

According to Predefined Constants: User Contributed Notes Volker's and rdcapasso solution, you can simply create helper class like this:

<?php

class System {

    const OS_UNKNOWN = 1;
    const OS_WIN = 2;
    const OS_LINUX = 3;
    const OS_OSX = 4;

    /**
     * @return int
     */
    static public function getOS() {
        switch (true) {
            case stristr(PHP_OS, 'DAR'): return self::OS_OSX;
            case stristr(PHP_OS, 'WIN'): return self::OS_WIN;
            case stristr(PHP_OS, 'LINUX'): return self::OS_LINUX;
            default : return self::OS_UNKNOWN;
        }
    }

}

Usage:

if(System::getOS() == System::OS_WIN) {
  // do something only on Windows platform
}
雅心素梦 2024-11-11 19:06:52

php_uname 函数可用于检测这一点。

echo php_uname();

The php_uname function can be used to detect this.

echo php_uname();
鲜肉鲜肉永远不皱 2024-11-11 19:06:52

这应该适用于 PHP 4.3+:

if (strtolower(PHP_SHLIB_SUFFIX) === 'dll')
{
    // Windows
}
else
{
    // Linux/UNIX/OS X
}

This should work in PHP 4.3+:

if (strtolower(PHP_SHLIB_SUFFIX) === 'dll')
{
    // Windows
}
else
{
    // Linux/UNIX/OS X
}
回眸一遍 2024-11-11 19:06:52

要检测它是 Windows、OS X 还是 Linux:

if (stripos(PHP_OS, 'win') === 0) {
    // code for windows
} elseif (stripos(PHP_OS, 'darwin') === 0) {
    // code for OS X
} elseif (stripos(PHP_OS, 'linux') === 0) {
    // code for Linux
}

在这种特殊情况下,stripossubstr 慢一点,但对于如此小的任务来说,它足够高效,而且更优雅。

To detect whether it's Windows, OS X or Linux:

if (stripos(PHP_OS, 'win') === 0) {
    // code for windows
} elseif (stripos(PHP_OS, 'darwin') === 0) {
    // code for OS X
} elseif (stripos(PHP_OS, 'linux') === 0) {
    // code for Linux
}

stripos is a bit slower than substr in this particular case, yet it's efficient enough for such a small task, and more elegant.

So尛奶瓶 2024-11-11 19:06:52

您可以检查 PHP >5.3.0 中是否存在常量(手册

if (defined('PHP_WINDOWS_VERSION_BUILD')) {
    // is Windows
}

此前,Symfony 中使用过此方法。现在他们使用不同的方法

if ('\\' === DIRECTORY_SEPARATOR) {
    // is Windows
}

You can check if a constant exists in PHP >5.3.0 (manual)

if (defined('PHP_WINDOWS_VERSION_BUILD')) {
    // is Windows
}

Previously, this method was used in Symfony. Now they use a different method:

if ('\\' === DIRECTORY_SEPARATOR) {
    // is Windows
}
开始看清了 2024-11-11 19:06:52

核心预定义常量: https://www.php .net/manual/en/reserved.constants.php,其中包含 PHP_OS(字符串) 常量。

或者,如果您想检测客户端的操作系统:

<?php
    echo $_SERVER['HTTP_USER_AGENT'] . "\n\n";

    $browser = get_browser(null, true);
    print_r($browser);
?>

来自 https:// /www.php.net/manual/en/function.get-browser.php


根据您的编辑,您可以参考此重复的命令行中的 PHP 服务器名称

可以使用

string php_uname ([ string $mode = "a" ] )

php_uname("s")

's':操作系统名称。例如。
FreeBSD。

可以为您解决这个问题,请参阅此处 http://php.net/manual /en/function.php-uname.php

Core Predefined Constants: https://www.php.net/manual/en/reserved.constants.php which has the PHP_OS (string) constant.

Or if you want to detect the OS of the client:

<?php
    echo $_SERVER['HTTP_USER_AGENT'] . "\n\n";

    $browser = get_browser(null, true);
    print_r($browser);
?>

From https://www.php.net/manual/en/function.get-browser.php


According to your edit you can refer to this dublicate PHP Server Name from Command Line

You can use

string php_uname ([ string $mode = "a" ] )

So

php_uname("s")

's': Operating system name. eg.
FreeBSD.

Would do the trick for you, see here http://php.net/manual/en/function.php-uname.php

最佳男配角 2024-11-11 19:06:52

如果您想检查是否在 Linux 下运行,只需测试 if (PHP_OS === 'Linux') 即可。无需使用 strtolower() 和 substr()。

If you want to check if running under Linux, just test if (PHP_OS === 'Linux'). No need to use strtolower() and substr().

香橙ぽ 2024-11-11 19:06:52
function isWin(){
 if (strtolower(substr(PHP_OS, 0, 3)) === 'win' || PHP_SHLIB_SUFFIX == 'dll' || PATH_SEPARATOR == ';') {
    return true;
 } else {
    return false;
 }
}
function isWin(){
 if (strtolower(substr(PHP_OS, 0, 3)) === 'win' || PHP_SHLIB_SUFFIX == 'dll' || PATH_SEPARATOR == ';') {
    return true;
 } else {
    return false;
 }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文