2011-07-15 43 views
1

喜replacment则是通过exec()命令这样PHP的exec()与proc_open()

$cmd = "php -q nah.php some args"; 
echo `$cmd`; 
echo "lalal"; 

但如果PHP文件没有获得exec() cmd并不知何故,我发现使用exec() ISN”安全。

替换我能找到的是popen()proc_open()

我可以popen()做到这一点没有争论,但我也希望一些参数(变量)传递给该文件。

我该怎么做 - 激发一个文件并将参数(变量)一起传递?

+1

当safe_mode是你不能使用命令行functon像exec,popen.proc_open ...没有办法是使用它! –

+0

@hamid opps错误的标题尝试完全读取这个问题 – kritya

+0

你试图达到什么需要系统命令? –

回答

0
// foo.php 

    function foo(){ 

    $var1 = $_GET['var1']; 
    $var2 = $_GET['var2'] 

    //do something by variables 



    } 

// bar.php 

    $ch = curl_init(); 

    curl_setopt($ch, CURLOPT_URL, "foo.php?var1=XXXXXX&var2=XXXXXXXX"); 

    //Also you could send your variables by post 
    //$data = array('var1' => 'XXXXXXXXXXXX', 'var2' => 'XXXXXXXXX'); 
    //curl_setopt($ch, CURLOPT_URL, 'foo.php'); 
    //curl_setopt($ch, CURLOPT_POST, 1); 
    //curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

    $output = curl_exec($ch); 

    curl_close($ch); 

    echo $output; 

编辑

使用此

$postdata = http_build_query(
     array(
      'var1' => 'some content', 
      'var2' => 'doh' 
     ) 
    ); 
    // If use POST 
    $opts = array('http' => 
     array(
      'method' => 'POST', 
      'header' => 'Content-type: application/x-www-form-urlencoded', 
      'content' => $postdata 
     ) 
    ); 

    $context = stream_context_create($opts); 
    $file = file_get_contents('foo.php', false, $context); 

    // USE GET 
    $opts = array('http' => 
     array(
      'method' => 'GET', 
      'header'=>"Content-Type: text/html; charset=utf-8" 
     ) 
    ); 

    $context = stream_context_create($opts); 
    $file = file_get_contents('foo.php?'.$data, false, $context); 

    echo $file; 
+0

因为我没有使用cURL。你能向我解释一下吗?我知道我是愚蠢的:P – kritya

+0

而且还给出了一个错误: PHP致命错误:调用未定义的函数curl_init()在/var/www/pbot/test1.php第2行 – kritya

+0

因此,总是用CURL来获取远程文件!你使用命令行来运行你的文件,我建议你使用curl来代替它,但我不知道你的代码是什么,为什么你使用命令行!但是当任何人使用命令行来运行一个文件,而这是不可能的我susugest使用卷曲! –

0

据我所知,在命令行参数只是命令的一部分,所以他们走在第一个参数,就像任何其他程序执行功能。