PHP 스크립트-Linux 또는 Windows에서 실행 중인지 감지합니까?
Windows 시스템 또는 Linux 시스템에 배치 할 수있는 PHP 스크립트가 있습니다. 두 경우 모두 다른 명령을 실행해야합니다.
내가 어떤 환경에 있는지 어떻게 알 수 있습니까? (영리한 시스템 해킹보다는 PHP가 바람직합니다)
최신 정보
명확히하기 위해 스크립트는 명령 행에서 실행 중입니다.
PHP_OS
상수 Docs 값을 확인하십시오 .
그것은 윈도우와 같은 당신에게 다양한 값을 줄 것이다 WIN32
, WINNT
또는 Windows
.
뿐만 아니라 참조 : 가능한 값을 경우 : PHP_OS 및 php_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!';
}
디렉토리 구분자가 /
(unix / linux / mac의 경우) 또는 \
창에 있는지 확인할 수 있습니다 . 상수 이름은 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_OS는 PHP가되었다는 OS보고 내장 에 반드시 현재 실행되고있는 동일한 OS 아니다.
PHP가 5.3 이상이고 Windows에서 실행 중인지 여부를 알아야하는 경우 Windows 관련 상수 중 하나 가 정의되어 있는지 테스트 하는 것이 좋습니다.
$windows = defined('PHP_WINDOWS_VERSION_MAJOR');
php_uname의 기능이 검출하는데 사용될 수있다.
echo php_uname();
이것은 PHP 4.3 이상에서 작동합니다 :
if (strtolower(PHP_SHLIB_SUFFIX) === 'dll')
{
// Windows
}
else
{
// Linux/UNIX/OS X
}
에 따라 미리 정의 된 상수 : 사용자가 노트 기여 폴커의와 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
}
Core Predefined Constants: http://us3.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 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
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
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.
You can check if a constant exists in PHP >5.3.0 (manual)
if (defined('PHP_WINDOWS_VERSION_BUILD')) {
// is Windows
}
이전에는이 방법이 Symfony에서 사용되었습니다. 이제 그들은 다른 방법 을 사용합니다 .
if ('\\' === DIRECTORY_SEPARATOR) {
// is Windows
}
Linux에서 실행 중인지 확인하려면 if 만 테스트하십시오 (PHP_OS === 'Linux')
. strtolower () 및 substr ()을 사용할 필요가 없습니다.
PHP 7.2.0부터는 상수를 사용하여 실행중인 OS를 감지 할 수 있습니다 PHP_OS_FAMILY
:
if (PHP_OS_FAMILY === "Windows") {
echo "Running on Windows";
} elseif (PHP_OS_FAMILY === "Linux") {
echo "Running on Linux";
}
가능한 값 은 공식 PHP 문서 를 참조하십시오 .
function isWin(){
if (strtolower(substr(PHP_OS, 0, 3)) === 'win' || PHP_SHLIB_SUFFIX == 'dll' || PATH_SEPARATOR == ';') {
return true;
} else {
return false;
}
}
에서 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>"
?>
ArtWorkAD가 지적했듯이 이것은 전체 $ _SERVER 배열입니다. HTTP_USER_AGENT 키를 사용하여 OS를보다 명시 적으로 추출 할 수 있습니다.
참고 URL : https://stackoverflow.com/questions/5879043/php-script-detect-whether-running-under-linux-or-windows
'Programming' 카테고리의 다른 글
Android Studio-모호한 메소드 호출 getClass () (0) | 2020.06.30 |
---|---|
Spring을 사용하여 프로그래밍 방식으로 속성 파일에 액세스 하시겠습니까? (0) | 2020.06.30 |
React Native에서 어떻게 플로팅 할 수 있습니까? (0) | 2020.06.30 |
이름과 버전 쌍으로 설치된 Jenkins 플러그인 목록을 얻는 방법 (0) | 2020.06.30 |
“Build and Run”없이 iPhone 시뮬레이터를 시작할 수 있습니까? (0) | 2020.06.30 |