I am looking for a way to have like a button in a web page, only when you press it the event handling is a program in a server. Like a trigger to execute a program in the server.
Is there any way to do this?
A PHP script can execute a command on the server using one of the various functions like exec, system, and passthru. It can be a security nightmare, so be careful.
You might want to look into something like: http://php.net/manual/en/book.exec.php
AJAX for the client part (sending notification about the button being pressed), and PHP (function exec() ou system() ).
Like the others have said, exec, system, and passthru can be used for that, but I believe you will need to disable safe mode.
safe_mode = Off
http://php.net/manual/en/features.safe-mode.php
To add on to this answer, you can make a form
Page1.html
<html>
<head>
</head>
<body>
<form action="script.php" method="post">
<input type="submit" />
</form>
</body>
</html>
script.php
<?php
shell_exec("cmd");
echo "done";
?>
Someone correct me if I am wrong here.
Related
I'm trying to execute a shell script from an HTML page using PHP. I have found a previous example here that I have been trying to follow but I'm having an issue. I'm not sure what the cause is but no errors are returned and no file is created from the bash script.
index.php:
<!DOCTYPE html>
<html>
<head>
<style></style>
</head>
<body>
<form action="./test.php">
<input type="submit" value="Open Script">
</form>
</body>
</html>
test.php
<?php
exec("./bash_script.sh");
header('Location: http://local.server.edu/ABC/abc_test/');
?>
bash_script.sh
#!/bin/bash
touch ./test_file.txt
One thing I have noticed that may be the cause of the issue is that it seems the path on the local server doesn't match with the file system exactly.
If I switch all the relative paths in the scripts to absolute paths such as:
/local/sequence/temp/abc_test/file.exe
Then after clicking the button to run the script I get an error saying:
The requested URL /local/sequence/temp/abc_test/test.php was not found on this server
Edit:
The three files They're located at /local/sequence/temp/abc_test And there is a symbolic link pointing to that directory at /export/www/htdocs/ABC
The error message seems to be indicating that test.php is not being found. As written, it needs to be in the same directory as index.php
You’ve tested the actual bash script, so we can proceed with the assumption that it’s in the execution of the script receiving the submission.
I would suggest putting all the web stuff into one page, because you can test sending and receiving input.
<?php
// for testing
// exec("./bash_script.sh");
// check for POST submission (this is not just reading data)
if(isset($_POST['runScript'])) {
// die('Request received');
exec("./bash_script.sh");
// It’s always proper to redirect after post :)
header('Location: http://local.server.edu/ABC/abc_test/');
die;
}
// finished with logic; show form
?>
<!DOCTYPE html>
<html>
<head>
<style></style>
</head>
<body>
<form method="POST">
<input type="submit" name="runScript" value="Open Script">
</form>
</body>
</html>
Note that I added the name attribute to the submit button, and made the form use POST method while submitting to the calling page (no action means submit to yourself).
I’ve also left a few commented actions to aid in debugging if necessary
You may have to adjust the path to the bash script. Currently it’s going to look in the same directory as index.php, which is not something you’d want to do in production.
You will be able to do that somehow, but its always very risky to allow such operations to execute from the php page.
I am building an HTML document that is meant to run locally. On it is a button that I would like to have run a Python script when clicked. I'm trying to use a PHP-generated button. There's no input or output that I want to associate with the button; I just want it to run the script, which produces charts as image files that the rest of the page uses. I have tried a couple different ways to do it, including putting the PHP part in the HTML document, before the HTML:
<?php
if (isset($_POST['update']))
{
exec('python myScript.py');
}
>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form name="update" method="post" >
<button name = "update" type="submit"> Update charts </button>
</form>
</body>
I've also tried making the PHP code its own .php document in the same directory and calling it from the HTML code as so:
<form action = "updateCharts.php" method="post">
<input type="submit" name="update" />
</form>
where updateCharts.php is:
<?php
system('cd C:\My\Script\Path');
system('python MyScript.py');
?>
I've also tried substituting "system" with "exec" but to no avail. In the first part, I click the button and nothing happens. In the second, I click the button and I am taken to the text of the PHP document. In neither case does my Python script run!
Does anyone see what I'm missing? I'm admittedly a novice with php so it may be something glaring. Thanks for the help!
What this:
$command = escapeshellcmd('python /My/Script/Path/myscript.py');
// or
// $command = escapeshellcmd('/My/Script/Path/test.py');
// But you have to make your script executable doing: chmod +x myscript.py
$output = shell_exec($command);
echo $output;
This can not work.
<?php
system('cd C:\My\Script\Path');
system('python MyScript.py');
?>
You can not have a "session" over multiple system calls. Every system call has its own shell environment.
At least on Linux systems you can cascade commands by semicolons. If you need a work directory to be set before script execution, you could either do
system('cd C:\My\Script\Path ; MyScript.py');
or
chdir('C:\My\Script\Path');
system('MyScript.py');
However, script execution from PHP can be blocked, or apache might not have appropriate file permissions to the script, or, or, or.
You need to check log files and also post what actually is output.
I am attempting to load a webpage on my own server which will run a .bat script (on the same server) as below.
When I access the page, called test.php, it display the 'DO IT!' button and when I press it, it just display the content on the .bat file rather than executing it on the server...
What do I need to configure on the server, I assume in the PHP settings, to force it to run the script rather than just display it on the webpage?
For the purpose of the question, I am happy about the security implications of what I am doing.
I am running a Windows machine with IIS and PHP.
<html>
<head>
<title>Restarting</title>
</head>
<body>
<?php
if(isset($_POST['submit']))
{
echo exec('c:\scripting.bat');
echo "Done!";
} else {
// display the form
?>
<form action="" method="post">
<input type="submit" name="submit" value="DO IT!">
</form>
<?php
}
?>
</body>
</html>
I think that the echo exec('c:\scripting.bat'); line it's causing you the problem. Try to just execute it without the echo statement.
If you trying to see the output of the function, you must use the second functions parameter: &$output, acording to the documentation itself. See it in the docs here.
I hope it will be useful to you! :D
I've never met such approach before, but I'm wondering why nothing happens after clicking the "Say hello" button.
<html>
<head>
</head>
<body>
<button onclick="myscript.php">Say hello</button>
</body>
</html>
<?php
echo "Hello";
?>
It would look very "bombastic" if I used the "form" markup construction in this case.
Could you point out my mistake? Or if my vision is totally hopeless, what are the unconventional ways to run the php script?
Why are you using onclick? onclick executes javascript code.
Just link to the file
Say Hello
If you must use javascript, you have to redirect to that page using something like window.location
Here's a tutorial on redirecting with javascript
Because onclick events evaluate JScript or JavaScript code, not file name.
http://www.w3schools.com/jsref/event_onclick.asp
onclick is a so called Event-Property. That expects javascript code by default.
this example is equivalent to including a js file with the content
test.php
that would raise an error as there is no object called test... You could put it in quotes which would make it a string and solve the problem... at least no error would be raised.
But back to your question: You don't launch a php script do you? with
<form method=GET action="scriptname.php">
<input type="text" name="name" />
<input type="submit" value="submit" />
</form>
you tell your browser to open a php-file with additional informations from the form.
It is of course possible to communicate with a php file in the background, search for ajax in order to do that.
I have a simple HTML form, sending a post request to a php script. In IE8, the form only works intermittently - most of the time the PHP script sees an empty $_POST variable.
Here's my code:
<html>
<head>
<title>Post test</title>
</head>
<body style="text-align: center;">
<?php
echo "<pre>".print_r($_POST, TRUE)."</pre>";
?>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<input type="text" name="name">
<input type="hidden" name="hidden" value="moo" >
<input type="submit" value="Search" >
</form>
</body>
</html>
Sometimes the print_r gives the response you'd expect (i.e. it's populated with the data from the form), most of the time it's empty.
Not being able to use POST is a bit of a problem for web applications - anyone got any ideas what's going on, and how to fix it?
Thanks everyone for wading in on this one.
It turns out the problem lay in an Apache module I had enabled.
It's a module to allow apache to use Windows authentication to identify a user via their Windows User id - mod_auth_sspi
The effect is caused by a known bug, in the module, but with a simple extra directive this can be worked around, until a fix is added in the next update, as described here:
http://sourceforge.net/projects/mod-auth-sspi/forums/forum/550583/topic/3392037
That sounds very very bizarre. Does it happen in other versions of IE as well?
I can't tell you what the problem is, but here are my suggestions on how to diagnose it:
Print $_REQUEST rather than just $_POST, to see if the data is coming in via another method.
Use a tool like Fiddler or Wireshark to track exactly what is actually being sent by the browser.
Fiddler in particular has been very helpful for me a few times (mainly when debugging Ajax code), and will tell you exactly what was posted by the browser. If your web server is localhost, you can also use Fiddler to track what is received before PHP gets its hands on it. If not, you can use wireshark on the server if you have permissions for installing that sort of thing.
In addition to Fiddler, I would have suggested a browser-based tool like Firebug, but I don't know of one for IE that is good enough (The IE dev toolbar doesn't give you details of request and response data, as far as I know).
I'm suspicious that when the script is telling you that $_POST is empty, you did not actually POST the form. You can check by adding print($_SERVER['REQUEST_METHOD']); after your print_r($_POST);
If you are posting a file some of the time (i.e. with a file input) then make sure you set enctype="multipart/form-data" in your <form> element.
Have you checked the generated html? Is it possible that echo $_SERVER['PHP_SELF'] isn't producing the output you're after, which messes up the form html, which messes up the POST?