Help on fwrite in between first and last lines.

I have info being added via fopen fwrite to a file in a directory. I need help in knowing how to implent skipping the first and last lines like the following below :

// This is an example of first line
#This is the line the script will write to
#This would be the line the script will write to when triggered another time
// This is an example of last line

I need it to increment as well, as show in the example above. I’m guessing fseek() will play a role in this. That’s the part I’m needing help with. Thanks in advance to all who give assistance on this subject, always appreciated.

I have never done anything like this before but I think what you would have to do is break the file down into an chunked array. What I would do is read every line and search for a “start line”. The “end line” is unnecessary since you would always have a “start line” and “end line” one after another.

So while cycling through each line from the file, you could build a chunked array, once the line matches a “start line” you could increment a counter. For example:

[php]
$chunk = 0;
$chunked = array();
foreach($lines as $line) {
if ($line == “whatever string your start line is”) {
$chunk++; // Increment the array to start a new “chunk”
}
$chunked[$chunk][] = $line; // Add line to the current chunk
}
[/php]

This should give you an array of all the chunks. Then you would have to determine which chunk you want to write to, then rebuild the entire file by looping the $chunked array.

I’m not sure if this will make sense :slight_smile:

if i would have to do what you want to do i would do it the matt mentioned, it seems the easiest way to approach

I tried using the following below and it does ignore the first line but it also ignores the last line with :

[php]
$examplehandle = fopen($examplefilename, “a”);
fwrite($examplehandle, $contenttowrite . “\n”);
[/php]

I know that ‘a’ places the file pointer at the end of the file. I’ve also tried ‘r’ which will do nothing at all and ‘r+’ overwrites the first line and will actually take off two strings from the last line. So it seems that I’m still stuck on writing to the file ignoring the first line and above the last line (in between first and last lines). I’ve attempted the other modes as well, with no success. Again below is an example of the file to write to.

[php]
// Top line / line1/single line
// Bottom line / line2/single line
[/php]

More feedback on this will be much appreciated. @m@tt thanks again for your help and efforts.

Hey tony, I’m not sure if you understood what I meant.

Basically what I suggested is that you read the entire file into an array. Then you create “blocks” from the array based on the start/end line(s). After that, you would scan that array to find the “block” you want to add to or edit. Once you find it you would modify the array values for that block.

Then, you would rewrite the entire file (truncate file) from the array you just modified. I could possibly write up an example if you can post your files format.

Here’s what I’m working with:

[php]
$chunk = 0;
$chunked = array();
foreach($lines as $line) {
if ($line == “whatever string your start line is”) {
$chunk++; // Increment the array to start a new “chunk”
}
$chunked[$chunk][] = $line; // Add line to the current chunk
}

$HTAfilename = $_SERVER[‘DOCUMENT_ROOT’] . “/.htaccess”;
$HTAhandle = fopen($HTAfilename, “a”);
$HTAcontent = "deny from " . $_SERVER[‘REMOTE_ADDR’];
fwrite($HTAhandle, $HTAcontent . “\n”);
fclose($HTAhandle);
header( “Location: index.php” );
[/php]

The .htaccess file :

<Files admin.php>
</Files>

Just blocking users on too many attempts on admin login form. Of course admin will have a way to remove the block via email or call to server admin.

I’m not sure if there is a breakdown in communication here. You just pasted my example into your code but made no actual use of it. What is your goal with the .htaccess file? Do you want to be able to add/remove IPs from the section?

So would be the “start line” and would be the “end line”?

I wasn’t even sure on how to apply your script and yes I wanted to write in between the Files tags.

<Files>
// Add line
// Add another line here when when user triggers <blockquote>the too many attempts on login form</blockquote> script
</Files>

I’m not asking for the whole script, just the part on how to write in between the two tags is all. Again I apologize for any communication confusion. Also yes the script will also eventually be able to delete with a form of some type reading the string of their IP, matching that string in the file and removing the whole line attached to it ($match). I’m hoping this isn’t confusing you at all. Write now the way I have it setup here is as follows.

[php]
$HTAfilename = $_SERVER[‘DOCUMENT_ROOT’] . “/.htaccess”;
$HTAhandle = fopen($HTAfilename, “a”);
$HTAcontent = “” . “\n” . "deny from " . $_SERVER[‘REMOTE_ADDR’] . “\n” . “”;
fwrite($HTAhandle, $HTAcontent . “\n”);
fclose($HTAhandle);
header( “Location: index.php” );
[/php]

Results are as follows :

<Files loginform.php>
deny from 127.0.0.1
</Files>
// Add when my script is triggered again
<Files loginform.php>
deny from 555.5.5.5
</Files>
// Add when my script is triggered again
<Files loginform.php>
deny from 222.2.2.2
</Files>
// And so forth and so on

I mean this works as it is but I’m not sure if this is more work on the server or if just calling the tags once and writing in between more work. Your input on that would be great as well. Again I do appreciate your time m@tt. If you have something more clever on the idea I’m more than happy to hear it out, though.

I think what you are asking might be a little too advanced for you. I can’t just write the script for you since you will not learn much that way. The best way to accomplish what you want is still to read the entire file into an array, manipulate the array, then re-write the entire file from that array.

Is there a specific reason you need to use htaccess for this? You could just use PHP. For example, in loginform.php you could simply have an IP check at the top. Maybe something simple like this:

[php]
$banned_ips = array
(
‘127.0.0.1’,
‘127.0.0.2’,
‘127.0.0.3’,
‘127.0.0.4’,
‘127.0.0.5’
);
if (in_array($_SERVER[‘REMOTE_ADDR’], $banned_ips)) {
exit(“You are banned.”);
}
[/php]

$banned_ips could be built from a database or file.

Go with Matt method since is easier and managable

Sponsor our Newsletter | Privacy Policy | Terms of Service