Error executing exec ('php -v') in php returning garbage (HTTP Response)

I have a shared Linux server where I am facing some strange problem. I am trying to execute PHP, the following command and it works fine; giving me back the PHP install path /usr/bin/php

.

exec('which php');// This runs so exec is not disabled

      

but any command I try with doesnt exec('php ...');

return me an array of 98 to 114 elements, randomly that is almost entirely garbage. The examples of the commands I ran are ...

exec('php -v');
exec('php -i');
exec('/usr/bin/php -v');

      

None of the above returned anything sane. Any idea why any command executed by php is failing?

Below is var_dump()

the dataset that is being exec()

returned to me.

EDIT (after some RND)

I managed to execute

exec('php -h')

      

and he repeated the following array to me in a readable format.

string(9) "php -help"
string(47) "Usage: php [-q] [-h] [-s] [-v] [-i] [-f ]"
string(27) "       php  [args...]"
string(36) "  -a               Run interactively"
string(69) "  -b | Bind Path for external FASTCGI Server mode"
string(57) "  -C               Do not chdir to the script directory"
string(58) "  -c | Look for php.ini file in this directory"
string(47) "  -n               No php.ini file will be used"
string(56) "  -d foo[=bar]     Define INI entry foo with value 'bar'"
string(70) "  -e               Generate extended information for debugger/profiler"
string(46) "  -f         Parse .  Implies `-q'"
string(28) "  -h               This help"
string(34) "  -i               PHP information"
string(43) "  -l               Syntax check only (lint)"
string(43) "  -m               Show compiled in modules"
string(60) "  -q               Quiet-mode.  Suppress HTTP Header output."
string(60) "  -s               Display colour syntax highlighted source."
string(33) "  -v               Version number"
string(72) "  -w               Display source with stripped comments and whitespace."
string(46) "  -z         Load Zend extension ."
string(75) "  -T        Measure execution time of script repeated  times."
This page was created in 0.055487155914307 seconds

      

Oh btw script I am using the following for testing ...

<?php
$mtime = microtime(); 
$mtime = explode(" ",$mtime); 
$mtime = $mtime[1] + $mtime[0]; 
$starttime = $mtime; 


if(!empty($_GET['p']) && $_GET['p'] == true){

    //$command = 'php -help'; //this works
    //$command = 'cat ' . getcwd() . '/dummy1.txt'; //this works echo a simple text file

    //$command = 'php -q ' . getcwd() . '/dummy1.txt'; //NOT WORKING
    $command = 'php -m'; //NOT WORKING

    echo '<div><pre>';
    var_dump($command);
    echo '</pre></div>';

    exec($command, $output, $return);
    //passthru($command,$output);

    echo '<div><pre>';
    foreach($output as $key => $value){
        var_dump($output[$key]);
    }   
    echo '</pre></div>';
}


$mtime = microtime(); 
$mtime = explode(" ",$mtime); 
$mtime = $mtime[1] + $mtime[0]; 
$endtime = $mtime; 
$totaltime = ($endtime - $starttime); 
echo "This page was created in ".$totaltime." seconds";
?>

      

Link Pastebin.org

Image

+3


source to share


2 answers


Its a PHP CGI that reads the script name from the environment, thereby causing the script to be called to run multiple or sometimes infinite numbers.

The solution is just using the php-cli command instead of the php command.



replaced the line $command = 'php -m';

with $command = 'php-cli -m';

with your code and everything will work fine.

+3


source


Given that the data looks like an HTTP response, and you have Content-Encoding: gzip

, I would use gzdecode()

on strings to decode the garbage.



+3


source







All Articles