0

I'm trying to determine the CPU type of the server my PHP is running on. I'm not need the CPU usage. The determination of the CPU type should work independently of the system (Win/Linux..). The solutions I found evaluate the /proc/cpuinfo file. This solution only works for linux systems and then only if the rights for access are available. Example: PHP Script - Get Server Processor

My approach was to use php function php_uname with parameter 'm'.

<?php
var_dump(php_uname('m'));
//string(6) "x86_64"

Unfortunately, this solution fails on some host systems. An answer is then generated there which is identical to the parameter 'a'. Example:

'Linux localhost 3.10.0-1160.36.2.el7.x86_64 #1 SMP'

The solution should only require PHP and must be able to run without additional installations. I am thankful for all hints.

jspit
  • 7,276
  • 1
  • 9
  • 17
  • 1
    How about [phpSysInfo](https://phpsysinfo.github.io/phpsysinfo/), a library with a long history of probing system info? – Raptor Jul 21 '22 at 10:04
  • 1
    Why do you need this? – Barmar Jul 21 '22 at 10:05
  • I need this as pure information in a special test class. – jspit Jul 21 '22 at 10:13
  • @jspit phpSysInfo should serve your needs – Raptor Jul 25 '22 at 02:29
  • @Raptor Yes, phpsysinfo provides a lot of details. However, it is way too big to use in a very small PHP class (https://github.com/jspit-de/phpcheck) . It also requires a special phpsysinfo.ini for each platform. – jspit Jul 25 '22 at 15:05
  • It's inevitable. Hardware probing is OS-specific, and it requires you to install libraries to the target server. PHP itself cannot give CPU stats for all OS (it usually works in Linux-based servers, though) – Raptor Jul 26 '22 at 01:41
  • I don't need CPU stats or details for all operating systems. I just need a short name for the CPU like "x86_64", "AMD64".. – jspit Jul 26 '22 at 07:29

2 Answers2

0

Try phpinfo(). phpinfo(int $flags = INFO_ALL): bool.

Outputs a large amount of information about the current state of PHP. This includes information about PHP compilation options and extensions, the PHP version, server information and environment

O. Jones
  • 103,626
  • 17
  • 118
  • 172
katxeus
  • 11
  • 4
  • Check it out at php.net/manual/en/function.phpinfo.php – katxeus Jul 21 '22 at 10:20
  • 1
    The information that is in phpinfo() under System and usually also contains the CPU type at the end is always identical to the response from php_uname() and therefore doesn't bring me any closer to the solution. – jspit Jul 21 '22 at 10:50
0

I don't know if it's a bad installation or a PHP bug when php_uname('m') returns an incorrect response on some host systems. These are all GNU/Linux systems. Access to /proc/cpuinfo is forbidden. However, I can run the uname command there. This is my current solution:

function getCPU(){
  $name = php_uname('m');
  //workaround bug
  if(strlen($name) > 20 AND stripos($name,'linux') !== false){
    $name = `uname -m`;
  }
  return trim($name);  
}

The function returns a short string like "AMD64", "x86_64" or "aarch64" on success.

Demo on 3v4l.org.

jspit
  • 7,276
  • 1
  • 9
  • 17