-1

I have a simple need to execute a "server side" / "non http facing" php script to generate an encrypted HASH that can be used by multiple scripts. My problem is that I can't seem to figure out the PHP exec command to get this accomplished.

I've tried five different formats suggested by multiple threads on StackOverflow, and none see to pass back a result, and nearly half of them freeze the browser with no output pipe.

I don't understand how to do ARGs nor the method of which order execution parameters go inside the exec() command. Here are my variables:

$filename = "/home/me/serverside_scripts/myScript.php";
$options = "value=999"; // I've also tried the Array('value' => 99) to avail

myScript is returning a JSON string that needs to be decoded, so all I want back is a STRING, or an array with a [0] equaling a string.

What do the > && 2>&1 mean?

The reason I'm using the exec() is to test the results, because this script will be executed by Java scripts (NOT JavaScript) as well.

Thanks for you help!

Mark Löwe
  • 572
  • 1
  • 10
  • 24
  • you say "non http facing" but you are trying to execute it from a script that is being triggered from a browser, since you say your browser freezes when executing your script. Why not just include the script into the script you are executing from the browser and use the functions/code? – Patrick Evans Jan 31 '14 at 00:46
  • Sorry, that was confusing wasn't it. I'm using (temporarily) a web script to exec() the script that's behind the public_html directory. This script is a unified way to create a php HASH that can be requested by a Java server side script, then passed to a PHP outside script for decryption. – Mark Löwe Jan 31 '14 at 01:11

2 Answers2

1

If you want to execute this script from PHP as well as from Java, then I'd take this approach:

  • put your code which does the work into a function within the file "myfunc.php" (give it a more meaningful name). Make the function take whatever arguments it needs, but it shouldn't try to accss $_GET parameters because these will only exist when called through the web.
  • now create a PHP file which includes "myfunc.php" and calls the function. This file can access $_GET and other web-specific variables, passing them as parameters to your function.
  • To also be able to execute from Java, create a 3rd file called "mycommand.php", which you can execute from the command line. If you need to pass command line arguments, see documentation on $argv. This script should include "myfunc.php" and call the function, passing any necessary parameters.

Develop and test this last script by running your script from the command line:

$ php5 mycommand.php <args go here>

After you get it working like this, then you can invoke it from java by using this solution.

There is no reason to invoke a PHP command as a subprocess from a PHP script - using "include" as I've described above is easier and more efficient. However here is an example for testing purposes:

test.php (invoke this through the browser):

<?
exec("php5 test2.php", $ret);
foreach ($ret as $line) {
  print $line . '<br>';
}
?>

This script invokes a second PHP script called "test2.php" (in the same directory), and prints the output from that command.

test2.php simply produces some output:

<?  
print "foo\n"; 
print "bar\n"; 
print "glorp\n"; 
?>
Community
  • 1
  • 1
jdhildeb
  • 3,322
  • 3
  • 17
  • 25
  • Thank you, good information. What I'm looking for is the exact exec() line that takes into account my needs based on the vars above. – Mark Löwe Jan 31 '14 at 01:11
  • Okay, I used your telnet method of testing, and it works perfect. Thanks for the trick. Still doesn't work via PHP, but this is conformation that the master script is outputting JSON HASH perfectly. – Mark Löwe Jan 31 '14 at 01:28
  • Re: "still doesn't work via PHP", can you post some of your code? – jdhildeb Jan 31 '14 at 01:48
  • print exec("php {$fileName} -v 9999",$returnJSON); hangs the script. I've tried every piping technique that I could find, and nothing makes it simply return the text string output. Works perfect in Telnet. – Mark Löwe Jan 31 '14 at 02:12
  • I've added an example of exec() to my answer. – jdhildeb Jan 31 '14 at 02:48
  • Scratch my last comment. For some reason my session_start() was hanging the script. WTF? Your example worked perfectly. – Mark Löwe Jan 31 '14 at 09:52
  • Calling session_start() doesn't make sense unless your script is called through the web. It will try to send a cookie back to the browser, and I suspect this somehow caused your hang. – jdhildeb Jan 31 '14 at 16:09
1

If you are feeding the php exec command with a variable, and that variable is coming from some form of user input, you can get yourself into a heap of trouble. A user could input a command that really messes with your system.

As for the &&2>&1, see

In the shell, what does " 2>&1 " mean?

These are redirect commands used in a bourne shell (not javascript that I know of) to send the stdout or stderr to a designated, not-normal place.

R

Community
  • 1
  • 1
Ramblin
  • 171
  • 1
  • 11
  • I've tried to understand the exec manual pages in PHP, but they lack all of the localized UNIX command pipe etc. – Mark Löwe Jan 31 '14 at 01:23
  • I could simplify my goal to: How do I pipe a print statement return to my $output variable within the exec command? Nothing works. – Mark Löwe Jan 31 '14 at 01:41