English 中文(简体)
PHP 写法——检测是否在lin或Windows下运行?
原标题:PHP script - detect whether running under linux or Windows?

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.

我如何能发现我所处的环境? (最好是一种PHP,而不是cl系统黑板)

Update

为了澄清问题,该笔文字从指挥线上操作。

最佳回答

查阅PHP_OS 恒星<<><>Docs

光盘将给您各种Windows值,如WIN32WINNTWindows/code>。

另见:。 价值:PHP_OSphp_uname<<>>><>>><>>>><>Docs/em><>:

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

您可以检查一下名录分离器是否为/(关于unix/linux/mac)或/code>。 姓名为<代码> DIRECTORY_SEPARATOR

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

if (DIRECTORY_SEPARATOR ===  \ ) {
    // windows
}
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进行上述检测是最快的。

从PHP 7.2.0开始,你可以使用固定的<代码”探测O.S.。 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.

http://php.net/manual/en/Function.php-uname.php PHP_OS报告说,PHP是built on,但不一定是目前运行的同一处。

如果您在PHP >=5.3,并且仅需要了解您是否在Windows或Dod-Windows上运行,然后测试 定义可以是好的主轴,例如:

$windows = defined( PHP_WINDOWS_VERSION_MAJOR );

这一工作应在PHP4.3+中进行:

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

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在本特定情况下比>substr慢一些,但对于这种小的任务而言,其效率是足够的,而且更为合理。

www.un.org/Depts/DGACM/index_spanish.htm Core Prefin Constants: http://us3.php.net/manual/en/servd.constants.php 编号PHP_OS (string) 不变。

或者如果你想发现客户的协调人:

<?php
    echo $_SERVER[ HTTP_USER_AGENT ] . "

";

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

http://us3.php.net/manual/en/Function.get-browser.php rel=“nofollow noreferer” http://us3.php.net/manual/en/Function.get-browser.php


根据您的编辑,您可参考这一可疑的。 PHP 页: 1

You can use

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

So

php_uname("s")

s : Operating system name. eg. FreeBSD.

http://php.net/manual/en/Function.php-uname.php” rel=“nofollow noreferer” http://php.net/manual/en/Function.php-uname.php

如果你想要检查一下是否在含水层下运行,那么就在<代码>(PHP_OS===LC/LC/LC/ LC/code>上进行测试。 无需使用梯子和子体。

function isWin(){
 if (strtolower(substr(PHP_OS, 0, 3)) ===  win  || PHP_SHLIB_SUFFIX ==  dll  || PATH_SEPARATOR ==  ; ) {
    return true;
 } else {
    return false;
 }
}




相关问题
Signed executables under Linux

For security reasons, it is desirable to check the integrity of code before execution, avoiding tampered software by an attacker. So, my question is How to sign executable code and run only trusted ...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

How to write a Remote DataModule to run on a linux server?

i would like to know if there are any solution to do this. Does anyone? The big picture: I want to access data over the web, using my delphi thin clients. But i´would like to keep my server/service ...

How can I use exit codes to run shell scripts sequentially?

Since cruise control is full of bugs that have wasted my entire week, I have decided the existing shell scripts I have are simpler and thus better. Here is what I have so far svn update /var/www/...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

热门标签