PHP 脚本 - 检测是在 linux 还是 Windows 下运行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5879043/
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
PHP script - detect whether running under linux or Windows?
提问by siliconpi
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 脚本,可以放在 windows 系统或 linux 系统上。在任何一种情况下,我都需要运行不同的命令。
How can I detect which environment I am in? (preferably something PHP rather than clever system hacks)
如何检测我所处的环境?(最好是 PHP 的东西,而不是聪明的系统黑客)
Update
更新
To clarify, the script is running from the command line.
澄清一下,脚本是从命令行运行的。
采纳答案by Sander Marechal
Check the value of the PHP_OS
constantDocs.
检查PHP_OS
常量Docs的值。
It will give you various values on Windows like WIN32
, WINNT
or Windows
.
它会在 Windows 上为您提供各种值,例如WIN32
,WINNT
或Windows
.
See as well: Possible Values For: PHP_OSand php_uname
Docs:
另请参阅:可能的值: PHP_OS和php_uname
Docs:
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
echo 'This is a server using Windows!';
} else {
echo 'This is a server not using Windows!';
}
回答by Upvote
Core Predefined Constants:http://us3.php.net/manual/en/reserved.constants.phpwhich has the PHP_OS (string)
constant.
核心预定义常量:http : //us3.php.net/manual/en/reserved.constants.php具有PHP_OS (string)
常量。
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 http://us3.php.net/manual/en/function.get-browser.php
来自http://us3.php.net/manual/en/function.get-browser.php
According to your edit you can refer to this dublicate PHP Server Name from Command Line
根据您的编辑,您可以从命令行引用这个重复的PHP 服务器名称
You can use
您可以使用
string php_uname ([ string $mode = "a" ] )
So
所以
php_uname("s")
's': Operating system name. eg. FreeBSD.
's':操作系统名称。例如。FreeBSD。
Would do the trick for you, see here http://php.net/manual/en/function.php-uname.php
回答by Anders Lindahl
回答by Ibu
You can check if the directory separator is /
(for unix/linux/mac) or \
on windows. The constant name is DIRECTORY_SEPARATOR
.
您可以检查目录分隔符是/
(对于 unix/linux/mac)还是\
在 Windows 上。常量名称是DIRECTORY_SEPARATOR
.
if (DIRECTORY_SEPARATOR === '/') {
// unix, linux, mac
}
if (DIRECTORY_SEPARATOR === '\') {
// windows
}
回答by Jordan Arseno
From http://www.php.net/manual/en/reserved.variables.server.php#102162:
来自http://www.php.net/manual/en/reserved.variables.server.php#102162:
<?php
echo '<table border="1">';
foreach ($_SERVER as $k => $v){
echo "<tr><td>" . $k ."</td><td>" . $v . "</td></tr>";
}
echo "</table>"
?>
This is the entire $_SERVER array... as ArtWorkAD has noted, by using the HTTP_USER_AGENT key, you can extract the OS more explicitly.
这是整个 $_SERVER 数组...正如 ArtWorkAD 所指出的,通过使用 HTTP_USER_AGENT 键,您可以更明确地提取操作系统。
回答by Ond?ej Bouda
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.
似乎比接受的答案更优雅。不过,上述使用 DIRECTORY_SEPARATOR 的检测是最快的。
回答by Jonathon Hill
This should work in PHP 4.3+:
这应该适用于 PHP 4.3+:
if (strtolower(PHP_SHLIB_SUFFIX) === 'dll')
{
// Windows
}
else
{
// Linux/UNIX/OS X
}
回答by ejn
Note that PHP_OS reports the OS that PHP was builton, which is not necessarily the same OS that it is currently running on.
需要注意的是PHP_OS报告操作系统,PHP是建立在,这是不一定相同的操作系统,它当前正在运行。
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 constantsis defined may be a good bet, e.g.:
如果您使用的是 PHP >= 5.3 并且只需要知道您是在 Windows 上运行还是在非 Windows 上运行,那么测试是否定义了特定于Windows 的常量之一可能是一个不错的选择,例如:
$windows = defined('PHP_WINDOWS_VERSION_MAJOR');
回答by Akam
function isWin(){
if (strtolower(substr(PHP_OS, 0, 3)) === 'win' || PHP_SHLIB_SUFFIX == 'dll' || PATH_SEPARATOR == ';') {
return true;
} else {
return false;
}
}
回答by artnikpro
To detect whether it's Windows, OS X or Linux:
要检测它是 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
}
stripos
is a bit slower than substr
in this particular case, yet it's efficient enough for such a small task, and more elegant.
stripos
比substr
在这种特殊情况下慢一点,但它对于这样一个小任务来说足够有效,而且更优雅。