Get the web server version in PHP - 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?

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.

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

To query the version of MySQL and/or MariaDB in PHP, you could use mysqli_get_server_info() or (if you are on still using the deprecated older mysql API) 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);
?>

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'];

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.

Related

Cannot get next results set when calling db2 stored procedure using php from Debian container to IBMi server

I am trying to call a db2 stored procedure using php (ibm_db2 v2.0.2 extension) from my Debian container to IBMi server.
I have installed ibm_db2 (v2.0.2) php extension on Debian container (docker) to request against a db2 database which is hosted on an IBMi server through TCP/IP protocol and it works just fine and I can make queries correctly.
My issue arise when I try to call a stored procedure with multiple results sets.
When I call a stored procedure, I am able to get the first results set using db2_fetch_assoc function but I cannot get the next results sets with db2_next_result function. There is no error thrown, the db2_fetch_assoc simply return false for the next results sets as if there was no subsequent sets.
The app calling theses stored procedure is deployed on an ZendServer on IBMi and everything works perfectly. The problem arise on my "docker" environment.
I tried many things like changing the version of ibm_db2 extension, trying another ODBC driver version, changing php configuration but I can't get it working.
The odd things is that when I run the stored procedure with db2 cli from my docker, I am able to get all my results sets correctly. It just doesn't work with php.
Coming to have some help. Did someone run into the same issue?
Thanks a lot,
EDIT 03/18/2020
Drivers info from db2_client_info php function
This is my code to retrieve my results sets:
while ($row = db2_fetch_assoc($statement)) {
print_r($row);
}
while ($row = db2_fetch_assoc(db2_next_result($statement))) {
print_r($row);
}
while ($row = db2_fetch_assoc(db2_next_result($statement))) {
print_r($row);
}
EDIT 03/18/2020 10:50
The php version is 7.1
The code now look like follow:
try {
db2_execute($statement);
while ($row = db2_fetch_assoc($statement)) {
print_r($row);
}
$nextResults = db2_next_result($statement);
while ($row = db2_fetch_assoc($nextResults)) {
print_r($row);
}
$nextResults = db2_next_result($statement);
while ($row = db2_fetch_assoc($nextResults)) {
print_r($row);
}
} catch (\Exception $e) {
echo $e->getMessage();
}
As per comment thread, the symptom was caused by incorrect coding.
For the db2_fetch_* calls to work, the preceding db2_next_result must return a result-set and you have to test for that.
In your original code you were unconditionally calling the db2_fetch_assoc with an invalid argument, as the db2_next_result did not return a result-set.
As per the code in both the IBM example, and in the PHP documentation, it is necessary to test the result of db2_next_result before calling any fetch action on.
The CLI trace showed a function sequence error, which tells you that the function is being called in the wrong circumstances.

Query IIS FastCGI settings from PHP

I am trying to programmatically query the FastCGI settings configured in IIS through PHP's COM API using WMI.
Using WMI CIM Studio I can see there is a FastCgiSection class which has a FastCgi member array that contains exactly the settings I want (specifically ActivityTimeout and RequestTimeout): http://msdn.microsoft.com/en-us/library/bb422421(v=vs.90).aspx
However, any attempt at querying this so far has not succeeded. The examples of querying Win32_Processor and so on that you can find online work fine, but translating that into a query of the FastCgiSection isn't working out.
So far I have this, which isn't outputting anything:
$wmi = new \COM('winmgmts:{impersonationLevel=Impersonate}//./root/WebAdministration');
$arrData = $wmi->ExecQuery("SELECT * FROM FastCgiSection");
foreach ($arrData as $obj) {
echo "has result";
}
How do I access this API through WMI in PHP?
Just in case you haven't already seen it, here is a pretty good link going into a lot of detail about doing very close to the same thing you're trying: http://www.sitepoint.com/php-wmi-dig-deep-windows-php/
One thing that I'd say to do ( if you haven't already ) is to check and verify the following is enabled in your php.ini: extension=php_com_dotnet.dll
Your query returns FastCgiSection objects, while the FastCGI application settings are stored in FastCgiApplicationElement class.
Your code doesn't access FastCgi member, only the objects returned by the WMI query. You need another loop over the FastCgi property in order to get what you want:
$wmi=new COM('winmgmts:{impersonationLevel=Impersonate}//./root/WebAdministration');
foreach($wmi->ExecQuery('SELECT * FROM FastCgiSection') as $section) {
foreach($section->FastCgi as $application) {
echo $application->ActivityTimeout, PHP_EOL;
echo $application->RequestTimeout, PHP_EOL;
}
}
Note that, in order for this code to work, you'll need to:
enable COM/.NET extension in "php.ini" file (as stated by Jonathon Hibbard).
install IIS 7's WMI Provider.

mssql_execute failing with, "stored procedure execution failed"

I am currently connecting sucessfully to an SQL database sat on a Windows 2008 using the following query;
$result = mssql_query("EXEC dbo.stored_procedure_name #param_level = 2");
I am basing my queries on existing code written in VB / ADO which looks like;
If level = "" Then level = 1
cmdTT.ActiveConnection = connStrTest1
set objParam=cmdTT.CreateParameter("#param_level", adInteger, adParamInput, 4, level)
cmdTT.Parameters.Append objParam
set rsTT = cmdTT.Execute
So what I attempted was the following;
$f = 2;
$stmt = mssql_init('dbo.stored_procedure_name', $mssql_link);
mssql_bind($stmt, "#param_level", $f, SQLINT4, false);
mssql_execute($stmt);
But no matter what the variation it always seems to print the print the screen the warning, "Warning: mssql_execute() [function.mssql-execute]: stored procedure execution failed in ...".
Whats the best way for me to debug the issue here? Can anyone see a clear fix to my problem?
I'm currently connecting remotely to the database from a LAMP stack.
Many Thanks
Ian
Is this from a linux server using FreeTDS? If so, I wonder if this is related to TDS Version. Try tds version = 8.0 in you /etc/freetds.conf
Run the contents of the stored procedure from w/in a sql editor with the parameters hard coded in. You'll get more verbose error messages that way.
I know it's an old post but am sure it will help someone.
You have to add mssql_free_statement($stmt) after executing.

PHP mysqli reconnect problem

I am having trouble using the mysqli class in PHP and I haven't been able to find the answer anywhere.
In my script a class creates a mysqli connection that it uses throughout it's functions. Afterward, this script forks. The connection is used by the children as well, but I'm running into the problem of the connection being closed (MYSQL Server Has Gone Away) in the parent when the children die.
Before I switched to mysqli (was just using mysql) I simply called mysql_ping to assure that the db connection was there before performing the query in the parent process. Mysqli has a similar ping function BUT it doesn't actually reconnect if the connection is gone. I tried using the mysqli.reconnect=ON global setting with no luck (using php.ini and ini_set).
The php mysql_connect function allows you to grab an already-existing connection, so if I was using mysql instead of mysqli, I could simply reuse the connection in the child right after the process forked. BUT mysqli doesn't seem to have any such functionality...
The only thing I was able to do was call mysqli->ping() and if that returned false then reconnect to the database in the parent. This is terribly inefficient, and I would much rather figure out how to do it correctly with mysqli (and no need for manual reconnects) that having to change back to mysql..
Any suggestions?
The doc for mysqli_ping() says that if you set the global option mysqli.reconnect to 1 (in your php.ini) then mysqli_ping() will reconnect when it detects the connection has gone away.
Update: Since I answered this question in 2009, PHP has mostly moved on to use the mysqlnd driver by default instead of libmysql. As mentioned in the comment below, mysqlnd does not support the mysqli_ping() function, and the PHP developers did this intentionally, according to their "Won't Fix" answer in https://bugs.php.net/bug.php?id=52561
So there appears to be no solution for automatic reconnect in PHP anymore.
u can try something a bit different .. instead of ping .. try to send a really simple low intensity query like "select now()" and see if you get any better results.
Can't you use persistent connections? Also, is that really necessary to fork() ?
function IsConnected() {
if (empty($this->_connectionID) === FALSE && mysqli_ping($this->_connectionID) === TRUE) {
return true; // still have connect
}
$this->_connectionID = false;
return false; // lose connection
}
Use http://www.php.net/manual/en/mysqli.real-connect.php ... reinstall php and check your php.ini settings
I think you need to set mysqli.reconnect in my.cng, which is the mysql config, rather than php.ini, I couldn't manage to change reconnect via ini_set, but my sys admin did it in my.cnf.
So, tad confused that others have done it within php.ini....

How to do command line from PHP script

I need to write a script that will give users info on a given Unix account (the same Unix server that the script lives on). Mostly thing kinds of things that are in the passwd file or available via finger.
PHP is in safe-mode, so I can't access the passwd file via something built into php like file_get_contents(). Also, because it's in safe mode, various other command-line functions are disabled.
I thought I could get the info via a socket (no clue yet what that means, but I thought I'd try) but I get a fatal error that socket_create() is an unknown function. I pulled up the php-config file (which I can't change, FYI), and sure enough, sockets are not enabled.
However, while I was in there, I saw the line '--with-exec-dir=' with no actual directory set.
So then I remembered that when I was trying EVERY command line function, that some threw "not allowed in safe-mode" type errors, while others did nothing at all. If I put something like:
echo "[[";
exec("finger user");
echo "]]";
I'd end up with [[]]. So no errors, just no results either.
Bottom line:
Is there something I haven't tried? (in general)
Is there a runtime config option I can set to make exec() work?
quick note: I tried passthru() as well, specifically passthru("pwd") with still no output.
update
based on feedback, I tried both of the following:
$stuff = exec("pwd", $return);
echo "stuff=".$stuff."\n";
echo "return=";
print_r($return);
which results in:
stuff=
return=Array
(
)
and
$stuff = passthru("pwd", $return);
echo "stuff=".$stuff."\n";
echo "return=";
print_r($return);
which results in:
stuff=
return=1
The 1 sounds hopeful, but not what I want yet.
Idea
So this is actually an update of an already existing script that (please don't ask) I don't have access to. It's a perl script that's called via cgi. Is there a way to do php via cgi (so I don't have to deal with perl or rely on the older code)?
I'm afraid you can't do that in safe-mode. You have to remove the safe-mode if you have control of the server configuration.
I think you can't rely on sockets to read local files, sockets are used for network related things.
exec doesn't inherently return any data.
Try something like,
exec("finger user",$output);
echo "[[";
foreach($output as $key => $value){
echo $value;
}
echo "]]";
Exec returns a value, so do:
$var = exec("finger user");
and then parse the output to get what you want. You can get return status by adding in an optional variable thus:
exec("finger user", $var, $return_status);
or just:
echo exec("finger user");
if all you want is to see the output.
Thanks to all that responded, the following is what finally worked:
Create a cgi-bin folder
Add the following to the top of the php script:
#!/usr/local/bin/php-cgi
I don't know if this is something special on my server configuration, but I can run exec() and get what I'm after.

Categories