Jump to content

How do I get PHP to invoke a Bash command when someone browses the page in a web browser?


averagecoder

Recommended Posts

I have a PHP file in /var/www/html/ called foobar.php with the following content:

Quote

<?php
shell_exec('/usr/bin/env > /tmp/output');
?>


From the backend I can run php foobar.php.  It works as expected.  When I browse to my website and go to foobar.php, the /tmp/output file does not receive anything.  

I set the permissions of the foobar.php file to different settings.  I set the owner and group of foobar.php and /tmp/output to different values.

I tried modifying the httpd.conf file.  When I placed this stanza in the httpd.conf file

Quote

LoadModule php7_module /usr/lib64/httpd/modules/libphp7.so

I could not restart the httpd service.  I tried using just

Quote

LoadModule php7_module modules/libphp7.so

  But this failed too.  

I thought PHP would interpret the file regardless of how I access it (e.g., via a web page and with the php command from a Linux terminal).  How do I get PHP to invoke a Bash command when someone visits a .php web page?

Link to comment
Share on other sites

PHP will execute the file - assuming, of course, you have a functioning PHP setup and don't mess with it.

Try executing env normally (from within PHP) and see if you get output. Otherwise try creating/writing to /tmp/output and see if that's the problem.

Link to comment
Share on other sites

The /tmp directory is typically setup with the "sticky bit" set.  Let's assume that you created the /tmp/output file using your command line execution.  What is the ownership of that file?  What web server are you running?  There are many different configurations possible.  It's possible that your script is running but it can't overwrite the original file due to ownership issues and the sticky bit.  

Also you might attempt to capture the return value from shell_exec and look at that:

$output = shell_exec('/usr/bin/env > /tmp/output');

echo "<pre>$output</pre>";

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.