How can You use php to determine the type of server operating system you are running

  • 2020-06-15 07:56:19
  • OfStack

I use winXP for native development, but the server I upload to is linux, and every time I upload, I always have to change 1 configuration file, 1 other thing,
Now that you can determine what kind of program to execute by determining the type of server you are on, how does php determine what type of server you are on
php has a number of system predefined variables that can easily be used to determine whether the system is windows or *unix
The relevant functions or predefined variables are as follows

php_uname();
PHP_OS
DIRECTORY_SEPARATOR
PHP_SHLIB_SUFFIX
PATH_SEPARATOR

Specific procedures:

<?
if(PATH_SEPARATOR==':') echo 'Linux';
else echo 'Windows';
?>

or

<?php
echo php_uname();
echo PHP_OS;
/* Some possible outputs:
Linux localhost 2.4.21-0.13mdk #1 Fri Mar 14 15:08:06 EST 2003 i686
Linux
FreeBSD localhost 3.2-RELEASE #15: Mon Dec 17 08:46:02 GMT 2001
FreeBSD
Windows NT XN1 5.1 build 2600
WINNT
*/
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
    echo 'This is a server using Windows!';
} else {
    echo 'This is a server not using Windows!';
}
?>

Related articles: