Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8136279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T10:42:46+00:00 2026-06-06T10:42:46+00:00

I am making a script in which php will fwrite and it ads it

  • 0

I am making a script in which php will fwrite and it ads it after the </file> tag and it doesnt get included. The main purpose is to log each IP in this .htaccess file in order to not have access to this specific file. Here is my code: (Searching about 3+ hours on google & php.net). I have thought if there is a way to read from the file .htaccess where the “word” </file> is and before it add the $ip. Or some other way to get the $badpersonip inside the <file forum.php
*(I can not use a database so it needs to be done solely from PHP and .htaccess)

<?php

$badpersonip = $_SERVER['REMOTE_ADDR'];

echo "You have been banned $badpersonip , dont spam!! <br> ";
$ip = "deny from $badpersonip \n";
$banip = '.htaccess';
$fp = fopen($banip, "a");
$write = fputs($fp, $ip);

?>

Also here is my .htaccess code:

<files forum.php>
Order Allow,Deny
Allow from all

deny from 127.0.2.1
deny from 127.1.2.1

</files>
deny from 127.0.0.3

As you see it ads the new IP but on the bottom AFTER the files tag has closed. 🙁

Thank you for your help really appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T10:42:47+00:00Added an answer on June 6, 2026 at 10:42 am

    If instead of using fwrite() you read the whole thing into a string with file_get_contents(), you can easily do str_replace() to replace the existing </files> with the new line and </files>

    // Read the while file into a string $htaccess
    $htaccess = file_get_contents('.htaccess');
    // Stick the new IP just before the closing </files>
    $new_htaccess = str_replace('</files>', "deny from $badpersonip\n</files>", $htaccess);
    // And write the new string back to the file
    file_put_contents('.htaccess', $new_htaccess);
    

    This isn’t recommended if you expect the file to become very large, but for a few dozen or few hundred IPs it should work fine. This won’t work exactly as is if you have more than one </files> in that .htaccess file. That would require more careful parsing to find the correct closing tag.

    If preserving whitespace (if you have indentation before </files>) is important to you, you look into using preg_replace() in lieu of the simpler str_replace().

    Another method to this would be to use file() to read the .htaccess into an array of its lines, locate the line containing </files> and insert a new array element before it then join the lines back together and write it to the file.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I making a PHP script which is powered by a POST request mainly, and
I'm making a simple PHP script which lets the user upload an image. These
I am new to Object Oriented PHP. Currently I am making a login script
I am making a web page which will allow users to input and view
This is my first php script. I actually code in vb.net.. I am making
I'm making a cURL post request with the following script which posts to a
I have written a script using my local PHP 5.3 installation making use of
I am making a registration table which will check if the cellphone number typed
I'm trying to create a mosaic type script in Javascript and PHP, which makes
I'm trying to implement some kind of caching in a PHP script that will

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.