load .profile with proc_open()

后端 未结 1 1287
忘掉有多难
忘掉有多难 2020-12-01 22:36

Here\'s the situation: I wrote a back end application, that runs on a certain server. On this server, there is a script that can be executed from the front end server, over

相关标签:
1条回答
  • 2020-12-01 23:37

    I don't have a ksh, but I've managed to do it with bash.

    /home/galymzhan/.bash_profile:

    export VAR_FROM_PROFILE="foobar"
    

    /home/galymzhan/test.php:

    #!/usr/bin/php -n
    <?php
    if (!isset($_SERVER['VAR_FROM_PROFILE'])) {
      $descriptors = array(0 => array('pipe', 'r'), 1 => array('pipe', 'w'));
      $process = proc_open('bash', $descriptors, $pipes);
      fwrite($pipes[0], escapeshellcmd('source /home/galymzhan/.bash_profile') . "\n");
      fwrite($pipes[0], escapeshellcmd('/home/galymzhan/test.php') . "\n");
      fclose($pipes[0]);
      echo "Output:\n";
      echo stream_get_contents($pipes[1]);
      echo "\n";
      fclose($pipes[1]);
      proc_close($process);
      exit;
    }
    print "Got env var {$_SERVER['VAR_FROM_PROFILE']}\n";
    // Useful part of the script begins
    

    Output I've got:

    [galymzhan@dinohost ~]$ ./test.php 
    Output:
    Got env var foobar
    
    0 讨论(0)
提交回复
热议问题