rushikesh988
January 1st, 2013, 06:45 AM
I am trying to make online compiler using PHP which will compile the code given in the text box and show the output. here $souce is a code entered by user.right now the status is it can give the output without any input. is there is any way to link the inputs entered by the user in the text box to produce the output .
The code is
<?php
require 'settings.php';
echo "<html>";
echo "<head>";
echo "<title>$site_title </title>";
echo "</head>";
echo "<body >";
echo " $header";
require 'header.php';
if(!empty($source)){
$filename=rand();
$file="$filename.c";
echo "<br>Click on filename to download your file <a href=$file >$file </a><br>";
$fp=fopen($file, "w");
$data=$_POST['source'];
fwrite($fp, $data);
fclose($file);
echo `gcc -o $filename $file 2> $filename.err `;
$ex=`gcc -o $filename $file ;echo $?`;
echo "<br>";
if ($ex==0) //if source code is compiled without errors then $ex has 0 value.
{
echo "your executable file is on <a href=$filename> $filename </a> <br>";
$out= `./$filename`;
}
$ercc= `cat $filename.err`;
$outnerr=$ercc . $out;
echo "<br/>";
echo "<form action=submit_c.php method= post >";
echo "Yours Source code is <br>";
echo "<textarea name=source1 cols=60 rows=20 readonly=readonly>$data</textarea><br>";
echo "And your output/error is ::: <br>";
echo "<textarea name=outnerr cols=60 rows=2 readonly=readonly>$outnerr</textarea><br>";
echo "</form> ";
}
else
echo "<strong>Please write something before compiling</strong><br>";
require 'footer.php';
echo "</body";
echo "</html>";
?>
The code is
<?php
require 'settings.php';
echo "<html>";
echo "<head>";
echo "<title>$site_title </title>";
echo "</head>";
echo "<body >";
echo " $header";
require 'header.php';
if(!empty($source)){
$filename=rand();
$file="$filename.c";
echo "<br>Click on filename to download your file <a href=$file >$file </a><br>";
$fp=fopen($file, "w");
$data=$_POST['source'];
fwrite($fp, $data);
fclose($file);
echo `gcc -o $filename $file 2> $filename.err `;
$ex=`gcc -o $filename $file ;echo $?`;
echo "<br>";
if ($ex==0) //if source code is compiled without errors then $ex has 0 value.
{
echo "your executable file is on <a href=$filename> $filename </a> <br>";
$out= `./$filename`;
}
$ercc= `cat $filename.err`;
$outnerr=$ercc . $out;
echo "<br/>";
echo "<form action=submit_c.php method= post >";
echo "Yours Source code is <br>";
echo "<textarea name=source1 cols=60 rows=20 readonly=readonly>$data</textarea><br>";
echo "And your output/error is ::: <br>";
echo "<textarea name=outnerr cols=60 rows=2 readonly=readonly>$outnerr</textarea><br>";
echo "</form> ";
}
else
echo "<strong>Please write something before compiling</strong><br>";
require 'footer.php';
echo "</body";
echo "</html>";
?>