Friday, May 4, 2012

Can PHP tell if the server os it 64-bit?


I am dealing with Windows here.



I know you can use the $_SERVER['HTTP_USER_AGENT'] variable to detect the OS of the browser viewing the page, but is the any way that PHP can detect the server's OS?



For my program's UI I am using a php webpage. I need to read a key in the registry that is in a different location on a 64-bit OS (It is under the 'Wow6432Node' Key).



Can PHP tell what OS it is running on? Can PHP tell if the OS is 64-bit or 32-bit?



Thanks in advance for the help.


Source: Tips4all

5 comments:

  1. if you just want to answer the 32bit/64bit question, a sneaky little function like this would do the trick (taking advantage of the intval function's way of handling ints based on 32/64 bit.)

    <?php function is_64bit() {
    $int = "9223372036854775807";
    $int = intval($int);
    if ($int == 9223372036854775807) {
    /* 64bit */
    return true;
    }
    elseif ($int == 2147483647) {
    /* 32bit */
    return false;
    }
    else {
    /* error */
    return "error";
    } }
    ?>


    You can see the code in action here: http://ideone.com/JWKIf

    Note: If the OS is 64bit but running a 32 bit version of php, the function will return false (32 bit)...

    ReplyDelete
  2. Try using the php_uname function...

    <?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 */
    ?>


    Source - Determine Operating System - http://www.sitepoint.com/forums/showthread.php?t=510565

    Another method is to use...

    echo $_SERVER['SERVER_SOFTWARE'];


    This returns the following string on my ibm t400 running Win 7 (64bit)...


    Apache/2.2.12 (Win32) DAV/2 mod_ssl/2.2.12 OpenSSL/0.9.8k mod_autoindex_color PHP/5.3.0 mod_perl/2.0.4 Perl/v5.10.0


    Unfortunately, its returning WIN32 because I'm running the 32bit version of apache.

    You can get general processor info (on a *nix server), by using the cmd...

    echo system('cat /proc/cpuinfo');


    You'll probably need to use a combination of the methods if you're planning on supporting many different OSes.

    ReplyDelete
  3. To check the size of integer (4/8 bytes) you can use the PHP_INT_SIZE constant. If PHP_INT_SIZE===8 then you have a 64-bit version of PHP. PHP_INT_SIZE===4 implies that a 32-bit version of PHP is being used but it does not imply that the OS and/or Processor is 32-bit.

    On Windows+IIS there is a $_SERVER["PROCESSOR_ARCHITECTURE"] variable that contains x86 when tested on my system (WinXP-32bit). I think it will contain x64 when running on a 64bit OS.

    ReplyDelete
  4. you can use some script to output the Os type

    here there is an example of how to get that information using WMI.

    You can call this script using exec and read the output.

    ReplyDelete
  5. A bit of a late answer, but if you just want to determine the word size, you can use this:
    (log(PHP_INT_MAX + 1, 2) + 1)

    ReplyDelete