Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Thursday, December 24, 2015

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

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?

Answer by John Himmelman for Can PHP tell if the server os it 64-bit?


Try using the php_uname function...

";  echo php_uname('n');/* Host name */  echo "
"; echo php_uname('r');/* Release name */ echo "
"; echo php_uname('v');/* Version information */ echo "
"; echo php_uname('m');/* Machine type */ echo "
"; 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.

Answer by RageZ for Can PHP tell if the server os it 64-bit?


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.

Answer by Kristoffer la Cour for Can PHP tell if the server os it 64-bit?


Note: This solution is a bit less convenient and slower than @Salman A's answer. I would advice you to use his solution and check for PHP_INT_SIZE == 8 to see if you're on a 64bit os.

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.)

  

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)...

Answer by pinktrink for Can PHP tell if the server os it 64-bit?


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)

Answer by Salman A for Can PHP tell if the server os it 64-bit?


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.

Answer by Chris0 for Can PHP tell if the server os it 64-bit?


A slightly shorter and more robust way to get the number of bits.

    strlen(decbin(~0));  

How this works:

The bitwise complement operator, the tilde, ~, flips every bit.

@see http://php.net/manual/en/language.operators.bitwise.php

Using this on 0 switches on every bit for an integer.

This gives you the largest number that your PHP install can handle.

Then using decbin() will give you a string representation of this number in its binary form

@see http://php.net/manual/en/function.decbin.php

and strlen will give you the count of bits.

Here is it in a usable function

function is64Bits() {      return strlen(decbin(~0)) == 64;  }  

Answer by user1568407 for Can PHP tell if the server os it 64-bit?


Or use PHP COM to call wmi

$obj = new COM('winmgmts://localhost/root/CIMV2');  $wmi = $obj->ExecQuery('Select * from Win32_OperatingSystem');  foreach($wmi as $wmiCall)  {      $architecture = $wmiCall->OSArchitecture;  }  

Answer by emcconville for Can PHP tell if the server os it 64-bit?


I've had luck with bit-shifting, and taking advantage boolean casting.

function is64bit()  {    return (bool)((1<<32)-1);  }  // or  function is32bit()  {    return 1<<32 === 1;  }  

Answer by doublehelix for Can PHP tell if the server os it 64-bit?


if you have the COM extension installed (in php.ini) you can call the windows WMI service.

To check the OS:

function getOsArchitecture() {      $wmi = new COM('winmgmts:{impersonationLevel=impersonate}//./root/cimv2');      $wmi = $obj->ExecQuery('SELECT * FROM Win32_OperatingSystem');      if (!is_object($wmi)) {          throw new Exception('No access to WMI. Please enable DCOM in php.ini and allow the current user to access the WMI DCOM object.');      }      foreach($wmi as $os) {          return $os->OSArchitecture;      }      return "Unknown";  }  

or, check the physical processor:

function getProcessorArchitecture() {      $wmi = new COM('winmgmts:{impersonationLevel=impersonate}//./root/cimv2');        if (!is_object($wmi)) {          throw new Exception('No access to WMI. Please enable DCOM in php.ini and allow the current user to access the WMI DCOM object.');      }      foreach($wmi->ExecQuery("SELECT Architecture FROM Win32_Processor") as $cpu) {          # only need to check the first one (if there is more than one cpu at all)          switch($cpu->Architecture) {              case 0:                  return "x86";              case 1:                  return "MIPS";              case 2:                  return "Alpha";              case 3:                  return "PowerPC";              case 6:                  return "Itanium-based system";              case 9:                  return "x64";          }      }      return "Unknown";  }  

Answer by Chandra Nakka for Can PHP tell if the server os it 64-bit?


Try this simple PHP function


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.