Does PHP have a function to detect the OS it's running on?

后端 未结 13 2425
北荒
北荒 2021-02-14 17:00

I wouldn\'t know under what keyword to look for this in the PHP database, so I\'m asking here.

Reason I want to know is because of how different Operating Systems handle

相关标签:
13条回答
  • 2021-02-14 17:11

    You could use the predefined constant PHP_OS.

    I'm using

    if (PHP_OS === 'WINNT') {...}

    0 讨论(0)
  • 2021-02-14 17:13
    <?php
    $OSList = array
    (
            // Match user agent string with operating systems
            'Windows 3.11' => 'Win16',
            'Windows 95' => '(Windows 95)|(Win95)|(Windows_95)',
            'Windows 98' => '(Windows 98)|(Win98)',
            'Windows 2000' => '(Windows NT 5.0)|(Windows 2000)',
            'Windows XP' => '(Windows NT 5.1)|(Windows XP)',
            'Windows Server 2003' => '(Windows NT 5.2)',
            'Windows Vista' => '(Windows NT 6.0)',
            'Windows 7' => '(Windows NT 7.0)',
            'Windows NT 4.0' => '(Windows NT 4.0)|(WinNT4.0)|(WinNT)|(Windows NT)',
            'Windows ME' => 'Windows ME',
            'Open BSD' => 'OpenBSD',
            'Sun OS' => 'SunOS',
            'Linux' => '(Linux)|(X11)',
            'Mac OS' => '(Mac_PowerPC)|(Macintosh)',
            'QNX' => 'QNX',
            'BeOS' => 'BeOS',
            'OS/2' => 'OS/2',
            'Search Bot'=>'(nuhk)|(Googlebot)|(Yammybot)|(Openbot)|(Slurp)|(MSNBot)|(Ask Jeeves/Teoma)|(ia_archiver)'
    );
    
    // Loop through the array of user agents and matching operating systems
    foreach($OSList as $CurrOS=>$Match)
    {
            // Find a match
            if (eregi($Match, $_SERVER['HTTP_USER_AGENT']))
            {
                    // We found the correct match
                    break;
            }
    }
    // You are using Windows Vista
    echo "You are using ".$CurrOS;
    ?>
    
    0 讨论(0)
  • 2021-02-14 17:16
    $svr_os=strtolower(reset(explode(' ',php_uname('s'))));
    
    $isLinux=$svr_os==='linux';
    
    $isWindows=$svr_os==='windows';
    
    0 讨论(0)
  • 2021-02-14 17:19

    You may also want to do a php info call to have a look at a lot of the configuration settings on your PHP setup, code is simple:

    phpinfo();
    
    0 讨论(0)
  • 2021-02-14 17:19

    *"BTW, nix OS use \n as new line. Mac usees \r, Windows - \r\n"

    ARRRGH! PLEASE STOP PERPETUATING THIS MYTH!

    Mac OS 9 used that like 10 years ago, but no one uses OS9 anymore. MACS USE UNIX LINE ENDINGS. \n. "Mac" used today should refer to contemporary computers, just as "Windows" refers to XP or vista unless otherwise qualified.

    Saying Macs use \r is about as correct as saying that "Windows runs on top of MS-DOS, supports only the FAT16 filesystem, and has no 64-bit support."

    Nobody should ever ever use \r for anything under any circumstances. Unless they are targeting old-ass macs.

    0 讨论(0)
  • 2021-02-14 17:21

    Check the $_SERVER variable.

    echo "<pre>";
    print_r($_SERVER);
    

    You can then use strstr (or any string comparison function) to check if you are on Windows. In this example, I checked the SERVER_SIGNATURE but you can use whatever key you want.

    $isWindows = strstr($_SERVER[SERVER_SIGNATURE], "Win32") !== FALSE;
    
    0 讨论(0)
提交回复
热议问题