简体   繁体   中英

Using an HTML string input, can I save that string and pass it on as parameters(java) in an PHP exec() command?

Currently, I have a java .class file that I want to run when a button is pressed and PHP exec() command calls it.

Say I have this in a .class file:

class Test {
public static void main(String[] args) {
    String a = args[0];
    System.out.println(a);
    }
}

Now I want PHP to execute this java using the exec() function. However, this class file needs parameters. I want to feed parameters through a form and submit button.

To the visitors, upon loading the site they would see the average form + submit:

 <form action="action_page.php"> First name:<br> <input type="text" name="firstname"> <br> Last name:<br> <input type="text" name="lastname"> <br><br> <input type="submit" value="Submit"> </form> 

Once they hit the submit button, I want the button to call PHP command

exec(java test username lastname)

given "username" and "lastname" is generated by the visitor.

First of all, add method="post" to your form:

<form action="action_page.php" method="post">

Then on submit you will be able to access the form inputs using the $_POST variable. Make sure you call escapeshellarg() on user input variables.

$username = escapeshellarg($_POST['username']);
$lastname = escapeshellarg($_POST['lastname']);

And then you can use exec() :

exec("java test $username $lastname");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM