Accurately determine the type of OS PHP is running on

北城以北 提交于 2019-12-21 20:17:41

问题


I need to determine the type of OS the PHP server is running on. By type, I mean strings like "windows" or "linux", not "wince", "winnt" etc.

So far, I have to leads: PHP_OS and uname(), the later being more reliable than the earlier (PHP_OS says what OS PHP was built on - according to documentation).


回答1:


It's important to know that no non-Windows OS string is going to contain the text "win", and no non-OSX OS string is going to contain the word "darwin", and so on. Detecting the OS is easy.

$uname = strtolower(php_uname());
if (strpos($uname, "darwin") !== false) {
    // It's OSX
} else if (strpos($uname, "win") !== false) {
    // It's windows
} else if (strpos($uname, "linux") !== false) {
    // It's Linux
} else {
    // It's something your script won't run on
}


来源:https://stackoverflow.com/questions/3948567/accurately-determine-the-type-of-os-php-is-running-on

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!