I am writing a web app that will use HTML to display a form. Then the user completes the form, and on submit the form is sent to PHP. In the PHP file I have it set up to call and exec() command to a Python file that will return data. But for some reason I am not getting anything back. When I execute the command in terminal it works great, but no response we ran in the web browser.
HTML:
<form id ="nameForm" action="python.php" method="post" target="_blank">
<label>First Name:</label><input type="text" id="firstName" name="firstName"><br>
<label>Last Name:</label><input type="text" id="lastName" name="lastName"><br>
<input type="submit" id="submitName" value="Submit">
</form>
PHP:
$firstName = $_REQUEST['firstName'];
$lastName = $_REQUEST['lastName'];
exec("/opt/http/PythonWeb/web/python pythonName.py " . $firstName . " " . $lastName, $response);
echo $response;
Python:
import sys
def main():
firstName = sys.argv[1]
lastName = sys.argv[2]
output = ""
output += "<html>
"
output += " <head><title></title></head>
"
output += " <body>
"
output += " <p>Hi " + firstName + " " + lastName + "</p>
"
output += " </body>
"
output += "</html>
"
print output
if __name__ == '__main__': main()
Please give me some suggestions as to what may be wrong. I am new to Python, but it's what the boss man wants... so I am going to make this work. Thanks!
According to this http://www.php.net/manual/en/function.passthru.php, you're using passthrough in a wrong way. It is for binary data...
This function should be used in place of exec() or system() when the output from the Unix command is binary data which needs to be passed directly back to the browser.
Try Exec().
Hope I was helpful.
Ensure your not being stopped by safe mode.
Ensure your escaping your arguments. Try:
$response = shell_exec("/opt/http/PythonWeb/web/python pythonName.py " . escapeshellarg($firstName) . " " . escapeshellarg($lastName));
echo $response;
If problems still occur, you can investigate further by ensuring error reporting is on by place this at the top of your PHP script:
ini_set('display_errors', 1);
error_reporting(E_ALL);
I'm not sure why it's not working but you should be using EscapeShellArgs and possibly EscapeShellCmd.
These will protect you against any dodgy user input, and they will often help to ensure that the exec actually works properly.
Also putting the string you're passing to 'Exec' into a variable, and echoing out that variable, and then cut and paste the exact output into a shell will often help figure out what the problem is.