phpforeachfile-get-contentsstr-replacefile-put-contents

How do I replace string in htaccess with PHP?


I am trying to manage blocked IPs on my server. I list the blocked IPs from the htaccess file and would like to select the IPs that I want to allow by replacing the selected IP from the above list example deny from 123.4.56.789 with blank space (to remove the deny from IP command.

I have tried everything but my code does not replace the selected IP with a blank space. The error_log does not show any errors.

Here is my code to list the IP addresses:

$base = $_SERVER["DOCUMENT_ROOT"];
$htaccess = $_SERVER["DOCUMENT_ROOT"]."/.htaccess";
$file = $htaccess;
$contents = file_get_contents($file);
$lines = explode("\n", $contents); // this is your array of words
foreach($lines as $line) {
    if (preg_match('/^deny from \d{1,3}(?:\.\d{1,3}){3}$/', $line)) {
        echo "<table width='50%'>
                <tr>
                    <td><r>$line</r></td>
                    <td style='text-align:left'><center><input type='checkbox' id='ipCheck' name='ipCheck' value='$ip'></center></td>
                </tr>
            </table>";
    }   
}



<!--Button below-->
  <table>
  <tr>
    <td></td>
    <td><button id="allowIPbtn" name="allowIPbtn" class='btn'>Allow selected IPs</button></td>
  </tr>
 </table>

JQuery when button is clicked:

<script>
$(document).ready(function () {
    $('#allowIPbtn').click(function () {
      if($("input:checkbox[name=ipCheck]").is(":checked")){
            $("input:checkbox[name=ipCheck]:checked").each(function () {
            $("#overlay").show();
            $("#overlay").delay(120000).hide(0);    
                    $.ajax({
                    type: 'post',
                    data: {iplist: $(this).val()},
                        success: function(data){
                        allowIP();
                        }
                    });
        });
      }

        
    });
});

</script>

My attempt to replace the selected IP with a blankspace:

ob_start();
if(isset($_POST['iplist']) ){
    $ipSelected = $_POST['iplist'];
    $htaccess_file = $htaccess;
    $file_contents = file_get_contents($htaccess_file);

    foreach((array)$ipSelected as $selectedIP){
        $file_contents = str_replace($selectedIP, "",$file_contents);
        file_put_contents($htaccess_file,$file_contents);
        echo $ipSelected;
    }
    exit;
}

Sample IP list:

deny from 10.10.76.194
deny from 10.10.85.70
deny from 10.10.63.174
deny from 10.10.56.77
deny from 10.10.15.196

Solution

  • For future reference if someone has the same issue: File_put_content and file_get_content have characters length limits. My issue was solved by using fopen().

    if(isset($_POST['mylist'])){
    $ipSelected = $_POST['mylist'];
    $root = $_SERVER["DOCUMENT_ROOT"];
    $htFile = $_SERVER["DOCUMENT_ROOT"]."/.htaccess";
    $accessfile = file_get_contents($htFile);
    $iplines = explode("\n", $ipSelected); // this is your array of words
    foreach($iplines as $selectedIP){
        $htaccessfile = fopen($htFile, "c+") or die("Unable to open file!");
        $str = str_replace($ipSelected," ",$accessfile); 
        fwrite($htaccessfile, $str);
        fclose($htaccessfile);
        echo "Done";
        }
    }