Manipulate / Edit .htaccess through php web form
posted on: 15:08, March 26th , 2013Here's the thing.
I needed a web form to manipulate ...or edit if you like the .htaccess apache configuration file so one can easaly add/remove "deny from" lines in that file.
Simply said web fronted of .htaccess file for banning people.

<?php
$opt = $_GET['opt'];
$row = $_GET['row'];
//open file for reading and detect lines in the file starting with "deny from"
//also display those lines in a tiny list with a link to delete one
echo "<p>";
$handle = @fopen(".htaccess", "r");
$i = 0;
if ($handle) {
while (($buffer = fgets($handle, 4096)) !== false) {
if (preg_match("/deny from/i","$buffer")) { echo "<a href=\"?page=admin&opt=delete&row=$i\">" . $buffer . "</a><br />"; }
$i++;
}
if (!feof($handle)) {
echo "Error: unexpected fgets() fail\n";
}
fclose($handle);
}
echo "</p>";
//delete a line of the file
if ($opt == "delete") {
// The file
$filePath = ".htaccess";
// Grab file into an array, by lines
$fileArr = file( $filePath );
// Remove desired line
unset( $fileArr["$row"] ); // $fileArr[3] == line #4
// put back with PHP5
$success = FALSE;
if ( file_put_contents( $filePath, implode( '', $fileArr ), LOCK_EX ) )
{
$success = TRUE;
}
?>
<script language='javascript' type="text/javascript">
<!-- Begin
window.location = "banadmin.php";
// End -->
</script>
<?
}//end delete
//add "deny from" lines to the file
//add lines at the bottom
if ($_SERVER['REQUEST_METHOD'] == "POST") {
$ip = $_POST['ip'];
$com = $_POST['com'];
$filename = '.htaccess';
$somecontent = "deny from " . $ip . " #" . $com ." \n";
//check if ip is valid
//this is vitale because if you add invalid ip it will break your web site
//with apache 500 server error
if (!is_valid_ipv4($ip)) { die('ip not valid'); }
// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {
// In our example we're opening $filename in append mode.
// The file pointer is at the bottom of the file hence
// that's where $somecontent will go when we fwrite() it.
if (!$handle = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
exit;
}
// Write $somecontent to our opened file.
if (fwrite($handle, $somecontent) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
echo "Success, wrote ($somecontent) to file ($filename)";
?>
<script language='javascript' type="text/javascript">
<!-- Begin
window.location = "banadmin.php";
// End -->
</script>
<?
fclose($handle);
} else {
echo "The file $filename is not writable";
}
#############################
}
else {
//next is the HTML form for adding the appropriate ip's
//post method is used
?>
<p>
<form action="<? echo "banadmin.php"; ?>" method="post">
IP:
<input class="login" name="ip" type="text" id="ip">
<br />
Comment:
<input class="login" name="com" type="text" id="com">
<br />
<input class="login" type="submit" name="Submit" value="Submit">
</form>
</p>
<? }
}
}
}
?>
Comments:
All materials on this site are licensed under the following license: "Steal every piece of information you can get your hands on and run as fast as you can "