0

I have two rather simple files. The first is an HTML-file like this:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<html>
<body>

<form action="test.php" method="post">
Search for: <input type="text" name="search" />
<input type="submit" name="submit" value="Search" />
</form>

</body>
</html>

The second file is test.php and it looks like this:

<?
       $filepath = "/usr/sbin";
       exec("ONE $search -command $filepath ");
       fopen($filepath, rw);
?>

My problem is that I want to use the argument "search" given in the HTML-form as value of variable in the PHP-script. ONE is a search script I made that takes one argument and I want it to be "$search".

Could this be done and if so how?

Many thanks in advance.

2
  • 3
    It should be in $_POST["search"], I think. But be very wary about taking user-entered data and passing it into an exec call. Commented Sep 19, 2012 at 17:48
  • Do not post the same question more than once - it will be considered as SPAM. Commented Sep 21, 2012 at 23:17

2 Answers 2

1

You can access that variable by using $_POST['search']

Before you use it in a shell call you should make sure that it won't damage your system.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks but it doesn't work. Do I need to put it in a new variables like this: search1 = $_POST['search'] or search1 = $($_POST['search']) ?
1

Just do this:

exec("ONE " . $_POST['search']. " -command $filepath ");

UPDATE:
You have another error, your following line:

fopen($filepath, rw);

should be like this:

fopen($filepath, "rw");

cause the second parameter to fopen() should be passed as a string.

UPDATE 2:
So as it seems, the OP want the output of the command to be printed on the page, for that you have to use passthru() instead of exec() , like so:

passthru("ONE " . $_POST['search']. " -command $filepath ");

11 Comments

"it doesn't seem to work" isn't very helpful. Does it error? Do nothing?
The PHP-file should find a couple of files containing the word given in "search", but it gives a blank page. I guess the variable isn't taken.
PHP Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in test.php on line 3
It seems an error in your fopen call, see the update to my answer.
Thank you! I've done the corrections and if I run php test.php in a terminal there's no more errors except that an argument is missing and so it should be. When I fill in a searchword in the HTML-file then clicking "Search" it pops right over to the test.php - but seemingly without getting any variable since the output is blank just like when I run php test.php.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.