SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    Grumpy Minimalist
    Join Date
    Jul 2006
    Location
    Ontario, Canada
    Posts
    424
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Determine Operating System

    Hi,

    I'm trying to determine the operating system that my script is running on (for portability concerns). phpinfo() shows the correct information under Environment > OS. The php documentation suggests that this information can be found in $_ENV['OS']. However, var_dump() displays this as NULL. var_dump($_SERVER['OS']) also returns NULL. In fact, var_dump($_ENV) is NULL as well.

    How can I determine the operating system that my script is running on?

    Thanks!

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,227
    Mentioned
    194 Post(s)
    Tagged
    2 Thread(s)

    finding the OS

    Try
    PHP Code:
    <?php
    echo php_uname('s');/* Operating system name */
    echo "<br />";
    echo 
    php_uname('n');/* Host name */
    echo "<br />";
    echo 
    php_uname('r');/* Release name */
    echo "<br />";
    echo 
    php_uname('v');/* Version information */
    echo "<br />";
    echo 
    php_uname('m');/* Machine type */
    echo "<br />";
    echo 
    PHP_OS;/* constant will contain the operating system PHP was built on */
    ?>

  3. #3
    Grumpy Minimalist
    Join Date
    Jul 2006
    Location
    Ontario, Canada
    Posts
    424
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, it works great!

  4. #4
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Umm did you try print PHP_OS;
    http://www.php.net/php_uname
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  5. #5
    Grumpy Minimalist
    Join Date
    Jul 2006
    Location
    Ontario, Canada
    Posts
    424
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The page you linked says that PHP_OS refers to the operating system that PHP was built on. I need the OS it is running on. For that, it appears that php_uname('s') is correct.

  6. #6
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    No that is wrong PHP_OS = The OS PHP is running on. I don't know why to documentation says that when it isn't. In either case it will match the OS PHP is running on and built on.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  7. #7
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,227
    Mentioned
    194 Post(s)
    Tagged
    2 Thread(s)

    operating system

    On my shared server both php_uname('s') and PHP_OS give Linux
    But on my localhost php_uname('s') gives Windows 9x (yes, I still have Windows 98 ) and PHP_OS gives WIN32

  8. #8
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Perhapes a better name for it should have been PHP_PLATFORM as it mainly returns that.

    Pre NT windows will return WIN32 and Windows NT (2000, XP, Vista) will return WINNT.

    Easy enough to know the OS that PHP is running on from it.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  9. #9
    SitePoint Addict
    Join Date
    Apr 2005
    Posts
    287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mittineague View Post
    On my shared server both php_uname('s') and PHP_OS give Linux
    But on my localhost php_uname('s') gives Windows 9x (yes, I still have Windows 98 ) and PHP_OS gives WIN32
    Windows 98.. jeezz lol.
    How does that make your feel?

  10. #10
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    No that is wrong PHP_OS = The OS PHP is running on. I don't know why to documentation says that when it isn't. In either case it will match the OS PHP is running on and built on.
    You are correct about that windows version is detected at run time, however on other platforms PHP_OS is a compile time constant and can be different from run-time uname(). Just because you've never seen cross-compiled php doesn't mean there is no such thing.

  11. #11
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    But as I see it Windows is really the only OS that you have to look at for as it has the most differences like path separators and uses .dll for extensions instead of .so.

    So far mostly everything I see works in everything or everything but windows so never needed anything else but PHP_OS.

    Mmmm well its understandable.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  12. #12
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sure it is, on the other side, I don't think the lack of personal experience with cross-platform issues is a valid reason to make highly debatable statements like the one I quoted.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •