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?
Click Start, type system in the search box, and then click System in the Control Panel list. The operating system is displayed as follows: For a 64-bit version operating system: 64-bit Operating System appears for the System type under System.
To check if you are running on 64-bit, just look under “Architecture” in phpinfo() – X86 is 32-bit, and X64 is 64-bit. Otherwise, if you want to test it out programmatically, you can check the PHP_INT_SIZE constant. It should be 4 for 32-bit, and 8 for 64-bit.
Open the Settings app. Navigate to System > About. On the right, check out the System type value. It shows either a x86-based processor (32-bit), x64-based processor (64-bit), or ARM-based processor depending on the hardware you have.
Go to Windows Explorer, right click on This PC and then select Properties. You'll see the system information on the next screen. In here, you should look for System Type. As you can see in the image above, it says “64-bit Operating System, x64-based processor”.
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.)
<?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)...
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With