PHP: How to get system information (CPU, RAM, OS, Disk Space)

Updated: January 11, 2024 By: Guest Contributor Post a comment

Introduction

When working with PHP, there may come a time when you need to obtain various system information such as details about the CPU, available RAM, the operating system being used, and disk space. This can be crucial for creating dashboards, monitoring systems, or assessing the environment in which your application is running. In this tutorial, we’ll explore different ways to acquire this kind of information using PHP.

Gathering Information with PHP

Before we start, it’s important to note that most of these operations will work on a *nix system (like Linux or macOS) since Windows does not always support the same command line functionality. Additionally, PHP should be configured to allow the execution of system commands for some of the examples provided below.

Accessing CPU Information

To get CPU information, we can use the exec() function to run system commands and retrieve the output. For instance:

<?php
$output = shell_exec('cat /proc/cpuinfo');
echo "<pre>$output</pre>";
?>

This command will display detailed information about the system’s CPU. However, it’s Linux-specific and depends on the /proc/cpuinfo file.

Examining RAM Usage

Similar to CPU info, RAM usage can be retrieved through system commands. The free command, for instance, gives us the memory status:

<?php
$output = shell_exec('free -m');
echo "<pre>$output</pre>";
?>

This outputs the amount of free and used memory in megabytes. Again, this is specific to systems that support Unix-like commands.

Finding Out Operating System Details

PHP provides a built-in function php_uname() which can display this information:

<?php
echo php_uname();
echo PHP_OS;
?>

The php_uname() function gives you a more detailed string, while PHP_OS gives you a shorter identifier for the operating system PHP is running on.

Determining Disk Space

To check disk space, PHP has two functions: disk_free_space() and disk_total_space(). These functions take a directory as an argument and return the free and total space of the corresponding filesystem.

<?php
$dir = "/";
echo disk_free_space($dir);
echo disk_total_space($dir);
?>

Keep in mind that this is the space on the drive where the specified directory resides, and the amount is given in bytes.

Alternative Approaches

If for some reason you can’t use system commands directly, you might need to use third-party libraries or built-in functionality in PHP modules. For in-depth analysis or if you are restricted from using exec() or similar functions, you may consider using a PHP extension like ‘phpSysInfo’ to query system information.

phpSysInfo is an open-source PHP script that parses specific files in the /proc directory to acquire system information which can then be displayed in a web page. This removes the need to execute shell commands, but it also carries a limitation—it generally works only on systems that support the /proc file system.

Here’s a basic outline of how you might use phpSysInfo in your PHP application:

  1. Install phpSysInfo: You’ll need to download and include phpSysInfo in your project. You can get it from its official repository on GitHub.
  2. Integration: Once you have phpSysInfo in your project, you can integrate it to display the system information.
  3. Display System Information: Use phpSysInfo to fetch and display the desired system information.

Since phpSysInfo is a standalone script and not a typical PHP extension, you don’t install it via PECL or Composer. Instead, you clone or download it from its repository.

Here’s a hypothetical example to demonstrate how you might set it up:

<?php

require 'path/to/phpsysinfo/autoload.php';

// Create a phpSysInfo object
$sysinfo = new phpSysInfo\phpSysInfo();

// Retrieve system information
$systemInfo = $sysinfo->getSystemInfo();

// Display system information
echo '<pre>';
print_r($systemInfo);
echo '</pre>';

?>

Key Points:

  • Replace 'path/to/phpsysinfo/autoload.php' with the actual path to the phpSysInfo script in your project.
  • The $sysinfo->getSystemInfo() method is hypothetical. You’ll need to refer to the phpSysInfo documentation for the exact method names and usage.
  • phpSysInfo works primarily on Unix-like systems with the /proc file system. It might not be suitable for Windows systems or other non-Unix environments.
  • Always review and adhere to the documentation and usage guidelines provided by the phpSysInfo project.

Important: Since accessing system information can potentially expose sensitive details about your server, ensure that any implementation of such functionality strictly adheres to security best practices, especially if exposed via a web interface.

Security Concerns

It is crucial to maintain security while running system commands from PHP. Avoid arbitrary command execution by using static commands or sanitizing user input if it must be included in a system command. Furthermore, be aware that displaying detailed system information publicly may pose a security risk. Always restrict access to such sensitive information.

Let’s say you need to run a command that requires user input, like fetching a file’s details using the ls -l command. You’ll need to sanitize the user input to avoid arbitrary command execution.

<?php

// Function to sanitize user input for the system command
function sanitizeInput($input) {
    // Remove potentially dangerous characters
    return escapeshellarg(preg_replace('/[^a-zA-Z0-9_\-\.\/]/', '', $input));
}

// Assuming this input comes from a user
$userInput = $_GET['filename']; // Replace with actual user input

// Sanitize the user input
$safeInput = sanitizeInput($userInput);

// Construct the command
$command = "ls -l $safeInput";

// Execute the command
$output = shell_exec($command);

// Display the output
echo "<pre>$output</pre>";

?>

In this example:

  • User input is obtained (e.g., from a GET parameter).
  • The input is sanitized using a custom function sanitizeInput, which removes dangerous characters and escapes the input for shell use.
  • The sanitized input is then safely used in a system command.
  • shell_exec is used to execute the command, and the output is displayed.

Conclusion

Accessing system information using PHP can be done in various ways, ranging from built-in functions to executing system commands. Each method has its own advantages and limitations, and some are more platform-specific than others. Always make sure to secure your applications against any potential security risks associated with obtaining system information. With the knowledge provided in this tutorial, you should now be equipped to retrieve CPU, RAM, OS, and disk space details in your PHP applications.

Remember that the context in which you’re running PHP (web server, command line, etc.) might further restrict what information you can access and the commands you are allowed to use. Always refer to your system’s and PHP’s documentation for the most current and secure methods of retrieving system information.

Thank you for following through with this guide on how to get system information using PHP. Make sure to test your code thoroughly and handle exceptions or errors that might stem from getting system information. This can help ensure a functional and secure application.