Breith
Breith

Reputation: 2298

Get the web server version in PHP

I would like the version of a web server (Nginx, MySQL, MariaDB, ...) in PHP.

I know the function for Apache: apache_get_version(). There are many phpinfo() which returns all values but how to exploit?

You would have an idea or it is not possible for the moment?

Upvotes: 5

Views: 4911

Answers (5)

Striezel
Striezel

Reputation: 3758

To query the version of MySQL and/or MariaDB in PHP, you could use mysqli_get_server_info() or (if you are still using the older mysql API which was deprecated in PHP 5.5.0 and removed in PHP 7.0.0) mysql_get_server_info(). The PDO API has no similar function or class for that purpose, but in that case you could just use the result of the SQL query

SELECT VERSION();

It returns something like 5.5.50-0+deb7u2. Here's a quick example:

<?php
  $user = 'username_here';
  $pass = 'your_db_password';
  // create DB connection
  $dbh = new PDO('mysql:host=localhost;dbname=mysql', $user, $pass);
  $stmt = $dbh->query('SELECT VERSION();');
  //fetch first column of first result row and print it out
  echo $stmt->fetchColumn();
  //unset PDOStatement and PDO to close DB connection
  unset($stmt);
  unset($dbh);
?>

Upvotes: 0

Shaun Cockerill
Shaun Cockerill

Reputation: 926

The other answers mention different methods on how to get the web server and database versions, however there is a function which will get the operating system itself.

// Gets the "System" row shown at the top of the `phpinfo()` table.
$system = php_uname( $mode = 'a' );

You can specify the mode to select different components. By default it'll display all of the components, separated by spaces and in the following order:

  • s: Operating System Name
  • n: Host name
  • r: Release Name
  • v: Version Information
  • m: Machine Type

You can view more information about the php_uname function here.

Upvotes: 1

user6535042
user6535042

Reputation:

You can retrieve the web server's version by using the $_SERVER superglobal, more specifically by using:

 $_SERVER['SERVER_SIGNATURE']

As per PHPs Documentation:

SERVER_SIGNATURE

String containing the server version and virtual host name which are added to server-generated pages, if enabled.

You can find more info on the official PHP Documentation site: http://php.net/manual/en/reserved.variables.server.php

Upvotes: 2

scrowler
scrowler

Reputation: 24406

A simple shell_exec would do the trick (assuming you're on a unix based server). Just don't put any user data into the command, and be aware that this approach may not work in shared hosting environments:

$nginxVersion = shell_exec('nginx -v 2>&1');
$mysqlVersion = shell_exec('mysql --version');

Note that nginx sends version output to stderr, so you need to pipe it to stdout to capture it.

Upvotes: 3

Joshua Klein
Joshua Klein

Reputation: 199

Address the $_SERVER super-global http://php.net/manual/en/reserved.variables.server.php

I believe the setting you want is:

echo $_SERVER['SERVER_NAME'];

Upvotes: 1

Related Questions