Αναζήτηση αυτού του ιστολογίου

PHP Built your own online php tester

I started this project at home, in order to be capable to play with PHP and Apache server from anywhere.
Set up : Apache 24 and PHP5.5

Target : To have one page on my browser with 2 frames. Left frame loads the content (html / php) of the file i select, right frame shows the html/php output.
I can change the code to the left and see the output on the right.

What it gets: You need 3 main files tstored in the directory of your web server.

PS: If you want you can create a seperate directory for testing , instead of default directory /usr/local/www/apache24/data. , but create a seperate directory inside /data (i.e phptest) so store all your files in usr/local/www/apache24/data/phptest

File 1 : main.html : This is the file that you will load from your browser like this:
http://iptoyourserver.com/apachedirectory/main.html
This file creates two frames in your browser. Left frame is used for editing the file , right frame for preview (actually running the file).
<html>
<frameset cols="50%,*">
<frame src="writefile.php" name="wfile" id="writecode">
<frame src="loadfile.php" name="rfile" id="runcode" >
</frameset>
</html>

File 2 : writefile.php :
This will be used on the left frame to open and save the file you want (supposed you already know the filenames in your apache server...)
<?php
session_start();
$host=gethostbyaddr($_SERVER['REMOTE_ADDR']);
echo '<center> Welcome '. $_SERVER['REMOTE_ADDR'] . ' ('.$host. ') to my server </center>';
$date=shell_exec('date');
$person = "Connected: ".$_SERVER['REMOTE_ADDR']." (".$host.") @ ".$date." \n";
$handlelog=fopen('logfile.txt', 'a');fwrite($handlelog, $person);fclose($handlelog);

if (isset($_POST))
{
 if ($_POST['savefile']=="SaveFile")
 {
  $File = $_POST['filename']; 
  $Handle = fopen($File, 'w');
     $Data = $_POST['data'];
     if(get_magic_quotes_gpc())
    {$Data = stripslashes($Data);}  
#Some servers have "magic quotes" enabled, meaning that quotes are automatically typed as slash and
#quote. If magic quotes are on i strip the slashes.
     fwrite($Handle, $Data);
     print "Data Written";
  fclose($Handle);
  $_SESSION['fname']=$_POST['filename']; #pass filename to session global php enviroments
  $date=shell_exec('date');$action = "Saved File: " . $File . " @ ". $date ." \n"; #logging
  $handlelog=fopen('logfile.txt', 'a');fwrite($handlelog, $action);fclose($handlelog); #logging
  $FileData = file_get_contents($File); #read the just saved file.
 }
 if ($_POST['openfile']=="OpenFile")
 {
  $File = $_POST['filename'];
  $FileData = file_get_contents($File);
  $_SESSION['fname']=$_POST['filename']; #pass the filename in the global session php variable
  $date=shell_exec('date');$action = "Opened File: " . $File . " @ ". $date ." \n"; #logging
  $handlelog=fopen('logfile.txt', 'a');fwrite($handlelog, $action);fclose($handlelog); #logging
 }
}
?>
<form method="post" action="">
<td>
<textarea style="width:100%;height:50%" name="data" ><?php  echo htmlentities($FileData);?></textarea>
</td>
<!-- If you don't use the htmlentities html code (or even php) is executed on your browser with echo command -->
<br>
 <input type="text" name="filename" value=<?php echo $File;?> >
 <input type="submit" name="openfile" value="OpenFile">
 <input type="submit" name="savefile" value="SaveFile">
</form>
<script type="text/javascript">
parent.document.getElementById("runcode").contentWindow.location.reload();
  <!-- this small javascript updates the right frame using the frame id applied by main.html -->
</script>

PS: A small connection/activities loging is made by this file for my personal reasons.

File 3: loadfile.php : This will be the file that will load on the right frame the file selected (open or save) on the left frame.
<?php
session_start();
$file=$_SESSION['fname'];
echo '<center>File:'.$file.'</center>';
include $file;
?>

Notice here that this file uses the php include together with session filename variable passed by writefile.php to include (and finally run) the file you opened/modified with writefile.php.
Ofcourse the file specified must exist in your apache24 directory and must be a valid html or php page.

Tips / How to:
- Provide read - write permissions to each file you need to remotely edit and change.
- If moreover you provide read - write permission to the whole directory, you will be able to open and save even new files.
- If file handled by writefile.php is not an html/php file but a single text file, then your browser will just display on the right the contents of the text file.
- I use geany on FreeBSD for html / php files editing, since geany compiles/check the files i type for synthax errors (i.e missing ; and many more).
- For sure you can call writefile.php and loadfile.php seperately.
- You can even install these three files to the public directory of your web hosting provider to provide "on the fly" file management (open and save file) by avoiding to get connected all the normal way (web hoster control panel / files management / etc). I'm sure you can identify the security risks if you do so....