On a Linux system, there are usually files like /etc/lsb-release
or /etc/os-release
which contain information about the distribution.
You can read them in PHP and extract their values:
if (strtolower(substr(PHP_OS, 0, 5)) === 'linux')
{
$vars = array();
$files = glob('/etc/*-release');
foreach ($files as $file)
{
$lines = array_filter(array_map(function($line) {
// split value from key
$parts = explode('=', $line);
// makes sure that "useless" lines are ignored (together with array_filter)
if (count($parts) !== 2) return false;
// remove quotes, if the value is quoted
$parts[1] = str_replace(array('"', "'"), '', $parts[1]);
return $parts;
}, file($file)));
foreach ($lines as $line)
$vars[$line[0]] = $line[1];
}
print_r($vars);
}
(Not the most elegant PHP code, but it gets the job done.)
This will give you an array like:
Array
(
[DISTRIB_ID] => Ubuntu
[DISTRIB_RELEASE] => 13.04
[DISTRIB_CODENAME] => raring
[DISTRIB_DESCRIPTION] => Ubuntu 13.04
[NAME] => Ubuntu
[VERSION] => 13.04, Raring Ringtail
[ID] => ubuntu
[ID_LIKE] => debian
[PRETTY_NAME] => Ubuntu 13.04
[VERSION_ID] => 13.04
[HOME_URL] => http://www.ubuntu.com/
[SUPPORT_URL] => http://help.ubuntu.com/
[BUG_REPORT_URL] => http://bugs.launchpad.net/ubuntu/
)
The ID
field is best suited to determine the distribution, as it's defined by the Linux Standard Base and should be present on common distributions.
By the way, I would recommend not using exec()
or system()
to read files, because they are disabled on many servers for security reasons. (Also, it doesn't make sense, because PHP can natively read files. And if it can't read them, then it also won't be possible though a system call.)