Forum Moderators: phranque
I want to include a txt or php file in a .htaccess file. This is how the .htaccess file should be:
-----
<Limit GET>
order allow,deny
**include /banned_ips.txt**
allow from all
</Limit>
ErrorDocument 403 /ban.php
-----
And this is how 'banned_ips.txt' should like:
-----
deny from x.x.x.x
deny from x.x.x.x
deny from x.x.x.x
-----
My problem is how to include an external file (banned_ips.txt) in the .htaccess file.
Does someone know the answer?
Thx anyway! Joris
Welcome to WebmasterWorld [webmasterworld.com]!
As far as I know, it can't be done.
The usual approach is to use a script that reads the current .htaccess file, prepends (adds) new records to it at the beginning, and then writes it.
The Allow and Deny directives are usually enclosed in a container such as <Files> and follow an Order directive, so this prepend-to-file operation could be a bit complex, requiring your script to actually parse the existing .htaccess file. A good work-around is to leave the Allow and Deny directives where they are now, but change them to use a variable that you define. This makes adding entries to the file much easer.
So, if you now have:
<FilesMatch "\.html$">
Order Deny,Allow
Deny from 192.168.0.3
Deny from 127.0.0.
</FilesMatch>
SetEnvIf Remote_Addr ^192.16.0.3$ getout
SetEnvIf Remote_Addr ^127.0.0. getout
...
<FilesMatch "\.html$">
Order Deny,Allow
Deny from getout
<FilesMatch>
Jim
I made several files:
[joris.example.nl...]
[joris.example.nl...]
[joris.example.nl...]
ip.php wich just is a form wich runs script.php
script.php add's a new row to .htaccess but he also delete all the other (old) stuff in .htaccess.. So what am I doing wrong?
script.php
-----
<?php
if ($ipadres!= '')
{
$newRow = 'SetEnvIf Remote_Addr ^' . ($ipadres) .'$ getout';
$fileName = fopen ('.htaccess', 'w');
fputs ($fileName, $newRow . chr(13) . chr(10) . $oldRows);
fclose ($fileName);
}
?>
-----
.htaccess
-----
**
<Limit GET>
order allow,deny
deny from getout
allow from all
</Limit>
-----
the stars (*) above (don't know the name for it) will be replaced by
SetEnvIf Remote_Addr ^' . ($ipadres) .'$ getout
[edited by: jdMorgan at 5:01 pm (utc) on April 13, 2004]
[edit reason] Removed URLs per TOS [/edit]
[php.net...]
Lock the file
Open the file for read
Read the current contents into an array
Close the file
Open the file for write
Write the new record
Write the array containing the previous records to the file
Close the file
Unlock the file.
The file locking is needed to prevent multiple server processes from accessing the file while it is being modified - this could lead to the loss of one or more of the new records.
You could also open the file for read/write, and reset the current file pointer after reading it and before writing it. This might be easier in PHP -- The above procedure works better when using PERL.
If you need more expert help with PHP, see our PHP forum.
Jim