Forum Moderators: coopster
blank line
#Branch Number ###
#Branch City, Branch State
#Description
#User Name
IP
blank line
Basically, postfix reads from this list to check if the device attempting SMTP is "allowed". If so, mail is sent, otherwise rejected.
Users can populate this file thru the web front end, however I've not allowed them to delete anything, that need has now become required.
I want to have the user enter the IP they want to delete and then associate the 4 rows above the IP, as well as the blank line, with the deletion.
New to PHP so this is a bit of a leap for me.
Thought about reading the file into an array and excluding the data I didn't want and re-write the file w/ a for loop. Not sure if that will work.
Thought about reading the file into an array and excluding the data I didn't want and re-write the file w/ a for loop. Not sure if that will work.
Yep, that's as good a way as any to do it. You can explode the data into an array using "\r\n\r\n" so it'll be split on the blank lines.
$data = file_get_contents('file.txt');
$data_array = explode("\r\n\r\n",$data);
foreach($data_array as $key => $value)
{
// The following checks if the IP address you're looking for appears anywhere in the given block of data.
// It's not very neat but it should work as long as the branch numbers aren't formated the same way as IPs.
if(strpos($value,$ip_address) !== false)
{
unset($data_array[$key]);
}
}
// Rejoin the data into one string
// Write the file
This isnt working for me, this is just a mock up of what I hope to have and the formatting isnt clean but I think you'll get the idea.
The allowed_relay_ip is layed out as follows (actual snippet)
#558 somewhere
#printer
#wes
#09/10/2008, 02:31 PM
45.45.45.1
#413 Columbia, MO
#HP M4345
#Shaw
#09/12/2008, 01:24 PM
172.23.183.137
The actual code at present, I'm experimenting a bit, is below:
<?php
$ip_address = '172.23.42.44';
$data = file_get_contents('/etc/postfix/allowed_relay_ip');
$data_array = explode("\r\n\r\n",$data);
foreach($data_array as $key => $value)
{
// The following checks if the IP address you're looking for appears anywhere in the given block of data.
// It's not very neat but it should work as long as the branch numbers aren't formatted the same way as IPs.
if(strpos($value,$ip_address) !== false)
{
unset($data_array[$key]);
}
else
{
print $value;
print $key;
}
}
?>
No matter what I set the IP address to it never finds it in the array. For instance, I set the IP address in the above code and have grep the file and indeed it exists. Yet it never shows up when I browse to this page.
The print statements in the else statement are my attempts at getting something to show up.
Not familiar w/ strpos and my PHP in a Nutshell hasn't helped me on this one. Certain this is just an oversight on my part.
I'd appreciate you chiming in w/ some ideas.
Thanks
dc
This is the frustrating part of coding. I have grepped the file and the CLI and it displays the IP, running the script using the same IP, returns nothing.
I'll try using the file option instead of file_get_contents.
What did you use to verify that the IP was found, the same print statements as I used?