Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 845
BLAKE Avatar asked Mar 01 '10 01:03

BLAKE


People also ask

How can I tell if my server is 64-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.

How can I tell if PHP is 64-bit?

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.

How do I know if my OS is x86 or x64?

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.

How can I tell if my virtual machine is 32 or 64-bit?

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


2 Answers

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

like image 62
Kristoffer la Cour Avatar answered Sep 23 '22 12:09

Kristoffer la Cour


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.

like image 28
Salman A Avatar answered Sep 21 '22 12:09

Salman A