|
This tutorial will show you how to run exec commands and system commands from php using a form, you do not need a database to
run these commands but you do need permission to be able to run command line commands from PHP.
This tutorial will show you how to do this using an example with the "ping" command, in short we will go through how to ping and display the
results via browser. You need to have apachee running with Linux or FreeBSD for this to work properly.
Okay, first we need two files, for this tutorials sake, we'll called them file.php and ping.php . File.php will just be a form to ask what address to ping. Ping.php will be
the action file that takes the data passed, ping the site and then results the results, lets start with the code of file.php, which is just a regular HTML form.
<form action='ping.php' method='post'>
URL to ping:<br>
<input type='text' name='address' size='20'><br>
<input type='submit' name='submit' value='submit'><br>
</form><br>
This is the basic html form, simply with one text input field that lets the user input what address they want to ping. Next we have code for ping.php
<?php
//code for actual ping
if(isset($_POST['submit']))
//if submit has been pressed
{
$address=$_POST['address'];
//above is a security measure to make sure it pings the typed address
exec("ping -c 5 $address",$results);
//the actual ping execution command
//output thrown in $results
$count=count($results);
//counts number of rows $in results array
for($i=0;$i<$count;$i++)
{
print "$results[$i]<br>";
}
}
//the loops print out all results of $results
//line by line
>
Okay I've included comment below each line to explain what each line does. First I put $address equal to its
post variable, this is a security measure to discourage malicious spiders from using the script and to stop hacking to
a degree. The exec() function actually executes the via within the quotes in command line and throws the results into the $results variable
as an array. The count function counts the number of rows in the $result array and stores that information into the $count variable. Next
we use a basic for loop to print out all the rows, on by one of the result of the exec function. Below is an example of this script If you have any questions on this tutorial, please
post in the Support Forums.
|