Am a newbie in PHP.
I wanted to have a dropdown list for specific solaris servers, in which when a server is selected from the drop down list, the server will execute a script on itself. How do I even do that?
<?php
session_start();
echo ("Select a Solaris System to audit");
?>
<form action="report.php" method="POST">
<div>
<select name="options">
<option value="1">Solaris 001</option>
<option value="2">Solaris 002</option>
<option value="3">Solaris 003</option>
</select>
<br>
<br>
<input type="submit" value="Audit"/>
</div>
</form>
Any help is deeply appreciated.
First of all, important disclaimer :)
Note, shell_exec
used in a backend is rather dangerous function, as it might pose security threats. Isolate shell_exec
from user input as much as possible and apply strict security policies
You have to implement two "artifacts":
On each server you need a backend file that execute desired script. This file should be accessible over the network. Lets say it's accessible via url http://*solarisservername*/justdoit
It might be something like
//Check for authentication, if authenticated set $authenticated = true if ($authenticated){ $script_response = shell_exec("<script to execute>"); } //if you need, you may return result to the calling script //I'm using json format, but you may want to use plain text, XML or whatever. $result = array("script_response"=>$script_responce,"success"=>true); echo json_encode($result);
On your page you need to implement quering the URL we set up in a previous step. You have two options: using cURL
and using AJAX
. I'll describe AJAX
method with jQuery
as it is much simpler and fancy :). Note, it's Javascript
<script type='text/javascript'> $(document).ready(function(){ $("#submit").click(function(){ var url = $("select[name='options']").val(); url = url + "justdoit"; //url now contains http://*solarisservername*/justdoit $.ajax({ type:post, dataType: 'json' url: url, success: function(data,textStatus){ //process your response here //access script response as data.script_response. } }); }); }); </script> <!--you dont't need method and action here, as we don't even post the form--> <form> <div> <select name="options"> <option value="http://solarisserver1">Solaris 001</option> <option value="http://solarisserver2">Solaris 002</option> <option value="http://solarisserver3">Solaris 003</option> </select> <br> <br> <!--onclick='return false' prevents form from submitting--> <input type="submit" id='submit' onclick='return false' value="Audit"/> </div> </form>
add this to your report.php
<?php
if (isset($_POST['options'])){
// you can get value now
$server = $_POST['options'];
echo $server; // will print the current selected value
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.