shams Posted November 20, 2013 Share Posted November 20, 2013 I want to get the values of variables from html form and then pass to the command of exec function in the php script, but it is not working: <?php $host = $_POST['localhost']; exec ("echo "My host name is" $host ");?> Quote Link to comment https://forums.phpfreaks.com/topic/284107-how-to-pass-variable-to-exec-command-in-php-script/ Share on other sites More sharing options...
Ch0cu3r Posted November 20, 2013 Share Posted November 20, 2013 (edited) What do you need to use exec for? If you want to display what the user entered use. <?php if(isset($_POST['submit'])) { $host = $_POST['host']; echo "My hostname is $host"; } ?> <form mehtod="post"> Hostname: <input type="text" name="host" /> <input type="submit" name="submit" /> </form> Edited November 20, 2013 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/284107-how-to-pass-variable-to-exec-command-in-php-script/#findComment-1459221 Share on other sites More sharing options...
shams Posted November 20, 2013 Author Share Posted November 20, 2013 (edited) Thanks for reply, i want to use the variable inside the exec function not for the echo but for ssh just for easy i use the echo command inside the exec function, I will use your script to edit for my need. Sorry for the inconvinence. Edited November 20, 2013 by shams Quote Link to comment https://forums.phpfreaks.com/topic/284107-how-to-pass-variable-to-exec-command-in-php-script/#findComment-1459223 Share on other sites More sharing options...
Ch0cu3r Posted November 20, 2013 Share Posted November 20, 2013 Ok. The problem with your code was the quotes exec ("echo 'My host name is $host'"); //OR exec ("echo \"My host name is $host\""); //OR exec ('echo "My host name is '.$host.'"'); Quote Link to comment https://forums.phpfreaks.com/topic/284107-how-to-pass-variable-to-exec-command-in-php-script/#findComment-1459225 Share on other sites More sharing options...
shams Posted November 20, 2013 Author Share Posted November 20, 2013 I want to use with ssh but it didn't get the variable: -L 9001:$host:80 and get this error: [Wed Nov 20 23:18:10.476065 2013] [:error] [pid 4690] [client 192.168.1.2:35511] PHP Parse error: syntax error, unexpected '$host' (T_VARIABLE) in /home/www/example.net/htdocs/php/ssht.php on line 8, referer: http://example.net/php/index.html Quote Link to comment https://forums.phpfreaks.com/topic/284107-how-to-pass-variable-to-exec-command-in-php-script/#findComment-1459236 Share on other sites More sharing options...
Ch0cu3r Posted November 20, 2013 Share Posted November 20, 2013 DId you even try the three examples I posted? exec ("echo 'My host name is $host'"); //OR exec ("echo \"My host name is $host\""); //OR exec ('echo "My host name is '.$host.'"'); Quote Link to comment https://forums.phpfreaks.com/topic/284107-how-to-pass-variable-to-exec-command-in-php-script/#findComment-1459246 Share on other sites More sharing options...
shams Posted November 21, 2013 Author Share Posted November 21, 2013 (edited) I tried the all 3 above lines one by one and no one work and this is the script i run: <?php $host = $_POST['localhost']; exec ("echo 'My host name is $host'"); ?> and these are the errors from apache log for the all 3 above lines one by one: [Thu Nov 21 05:17:11.159618 2013] [mpm_prefork:notice] [pid 2749] AH00163: Apache/2.4.6 (Ubuntu) PHP/5.5.3-1ubuntu2 configured -- resuming normal operations[Thu Nov 21 05:17:11.295630 2013] [core:notice] [pid 2749] AH00094: Command line: '/usr/sbin/apache2'[Thu Nov 21 05:22:17.744680 2013] [:error] [pid 3410] [client 192.168.1.2:45137] PHP Notice: Undefined index: localhost in /home/www/example.net/htdocs/php/t1.php on line 3[Thu Nov 21 05:22:17.744768 2013] [:error] [pid 3410] [client 192.168.1.2:45137] PHP Stack trace:[Thu Nov 21 05:22:17.744781 2013] [:error] [pid 3410] [client 192.168.1.2:45137] PHP 1. {main}() /home/www/example.net/htdocs/php/t1.php:0[Thu Nov 21 05:25:08.683203 2013] [:error] [pid 3411] [client 192.168.1.2:45142] PHP Notice: Undefined index: localhost in /home/www/example.net/htdocs/php/t1.php on line 3[Thu Nov 21 05:25:08.683268 2013] [:error] [pid 3411] [client 192.168.1.2:45142] PHP Stack trace:[Thu Nov 21 05:25:08.683287 2013] [:error] [pid 3411] [client 192.168.1.2:45142] PHP 1. {main}() /home/www/example.net/htdocs/php/t1.php:0[Thu Nov 21 05:33:58.989598 2013] [:error] [pid 3413] [client 192.168.1.2:45191] PHP Notice: Undefined index: localhost in /home/www/example.net/htdocs/php/t1.php on line 3[Thu Nov 21 05:33:58.989644 2013] [:error] [pid 3413] [client 192.168.1.2:45191] PHP Stack trace:[Thu Nov 21 05:33:58.989668 2013] [:error] [pid 3413] [client 192.168.1.2:45191] PHP 1. {main}() /home/www/example.net/htdocs/php/t1.php:0 Edited November 21, 2013 by shams Quote Link to comment https://forums.phpfreaks.com/topic/284107-how-to-pass-variable-to-exec-command-in-php-script/#findComment-1459293 Share on other sites More sharing options...
brandon66 Posted November 21, 2013 Share Posted November 21, 2013 (edited) Try this maybe? <?php if(isset($_POST['host'])) { $host = htmlspecialchars($_POST['host']); echo "My hostname is" . $host; }else{ echo "Not receiving POST variable"; } ?> Otherewise it seems like your locahost or host variable is not getting set. Edited November 21, 2013 by brandon66 Quote Link to comment https://forums.phpfreaks.com/topic/284107-how-to-pass-variable-to-exec-command-in-php-script/#findComment-1459295 Share on other sites More sharing options...
shams Posted November 21, 2013 Author Share Posted November 21, 2013 This is the output: Not receiving POST variable This is the apache error.log: [Thu Nov 21 09:00:28.701411 2013] [mpm_prefork:notice] [pid 2746] AH00163: Apache/2.4.6 (Ubuntu) PHP/5.5.3-1ubuntu2 configured -- resuming normal operations[Thu Nov 21 09:00:28.754984 2013] [core:notice] [pid 2746] AH00094: Command line: '/usr/sbin/apache2 Quote Link to comment https://forums.phpfreaks.com/topic/284107-how-to-pass-variable-to-exec-command-in-php-script/#findComment-1459316 Share on other sites More sharing options...
JIXO Posted November 21, 2013 Share Posted November 21, 2013 Why are you trying to echo a string using exec(), exec() will deliver whatever you give to system's shell, try this : <?php exec ('reboot'); Quote Link to comment https://forums.phpfreaks.com/topic/284107-how-to-pass-variable-to-exec-command-in-php-script/#findComment-1459317 Share on other sites More sharing options...
shams Posted November 21, 2013 Author Share Posted November 21, 2013 (edited) I want to pass the variable $host to the php script: <form mehtod="post"name="ssht" action="ssht.php"> Hostname: <input type="text" name="host" /> <input type="submit" name="submit" /></form> <?php $host = $_POST['localhost']; exec ("sshpass -p my_pass ssh -L 9001:$host:80 user@shell.example.net -N"); ?> The above script didn't get the variable $host, with this error of apache log: Wed Nov 20 23:18:10.476065 2013] [:error] [pid 4690] [client 192.168.1.2:35511] PHP Parse error: syntax error, unexpected '$host' (T_VARIABLE) in /home/www/example.net/htdocs/php/ssht.php on line 8, referer: http://example.net/php/index.html Edited November 21, 2013 by shams Quote Link to comment https://forums.phpfreaks.com/topic/284107-how-to-pass-variable-to-exec-command-in-php-script/#findComment-1459325 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.