OK, here is the code, I'm using file instead of file_get_contents. I can enter a valid IP and it's removed from the array as I want it to be. However, it's almost as tho the foreach I have runs over EVERY sequence in the array and produces an error for each index.
I've copied my entire code, following the code, I'll have the error.
<?php include("index.php"); ?>
<html> <head> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<link rel=StyleSheet href="style.css" type="text/css" media="screen"> </head>
<div id="rh-col">
<center><strong>Enter the IP address of the device that you would like to delete below.<br /> <br />
<form method="POST" action="<?=$PHP_SELF;?>">
IP Address:<input type="text" name="ip_address" /><br /><br />
<input type="submit" name="submit" Value="Submit" />
<input type="submit" name="submit" value="Reset" />
</form>
</strong></center>
<br /> <br />
<?php>
$button = $_REQUEST['submit'];
$ip_address = $_REQUEST['ip_address'];
$filename = file("/etc/postfix/allowed_relay_ip");
//$filename = explode("\n", $filename);
foreach($filename as $key => $value) //read each line of array and assign it a key value
//if the button clicked is Submit
if ($button == "Submit")
{
if (!isset($_POST['$ip_address']))
{
if (empty($ip_address))
{
echo "<center><strong>You did not specify an IP address.</strong></center><br />";
}
else
{
if (preg_match("{^\b((25[0-5]|2[0-4]\d|[01]\d\d|\d?\d)\.){3}(25[0-5]|2[0-4]\d|[01]\d\d|\d?\d)\b$}", $ip_address))
{
if(strpos($value, $ip_address) !==false) //check if the $ip_address is in the array
{
unset ($filename[$key], $filename[$key -1], $filename[$key -2], $filename[$key -3], $filename[$key -4], $filename[$key -5]);
}
else
{
echo "The ip address," .$ip_address. ", is not listed in the array.";
}
}
else
{
echo "<center><strong>The IP address, <font color = #0000CC>".$ip_address."</font> ,is invalid</strong</center>>";
}
}
}
}
echo "<pre>";
print_r ($filename);
?>
</div>
</html>
Error:
The ip address,11.4.36.12, is not listed in the array.The ip address,11.4.36.12, is not listed in the array.The ip address,11.4.36.12, is not listed in the array.The ip address,11.4.36.12, is not listed in the array.The ip address,11.4.36.12, is not listed in the array.
It seems to me that the foreach is causing my problem but I cant figure how to get out of that. I want the array checked until the first match, then the IP shouldn't appear again in the array (I disallow duplicate IP's from being entered into the text file in another piece of code). I've come to that conclusion b/c it keeps printing the message "The ip address 11.4.36.12, is not listed in the array." hundreds of times.
Once again looking forward to your help.