John Davidson

foreach - How do I replace string in htaccess with PHP?

0 comments
Message:


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


My Answer:
Your code looks mostly correct, but there are a few things you can adjust to make it work properly. Here are some suggestions:

1. Make sure that the IP address you are trying to replace in the htaccess file matches exactly the format in the file. For example, if the IP address in the file is "deny from 10.10.76.194", make sure that the IP address you are trying to replace is in the same format.

2. Check if the IP address is being passed correctly from your jQuery code to the PHP script. You can do this by adding an `echo $ipSelected;` before the `foreach` loop in your PHP code.

3. Instead of using `file_get_contents` and `file_put_contents` to read and write the htaccess file, you can use `fopen`, `fwrite`, and `fclose` functions to make sure the file is properly opened, written to, and closed.

Here is an updated version of your PHP code:

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

$file = fopen($htaccess_file, 'r+');
$file_contents = fread($file, filesize($htaccess_file));

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

fseek($file, 0);
fwrite($file, $file_contents);
fclose($file);

echo json_encode($ipSelected);
exit;
}


Make sure to also update your jQuery code to handle the response from the PHP script correctly. You can use `console.log(data);` in the success function to see if the IP address is being returned properly.

With these adjustments, your code should be able to replace the selected IP addresses with a blank space in the htaccess file.

Rate this post

4 of 5 based on 5948 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us