Forum Moderators: phranque

Message Too Old, No Replies

htaccess include

         

dikkejoris

11:59 am on Apr 13, 2004 (gmt 0)

10+ Year Member



Hello everyone.. I'm quite new here and I've got a problem. I hope someone could help me!

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

jdMorgan

3:05 pm on Apr 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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>

then you could change that to:

SetEnvIf Remote_Addr ^192.16.0.3$ getout
SetEnvIf Remote_Addr ^127.0.0. getout
...
<FilesMatch "\.html$">
Order Deny,Allow
Deny from getout
<FilesMatch>

In this way, the Deny/Allow directives do not need to be moved, and you can just write new SetEnvIf directives to the beginning of your .htaccess file without changing the order in which the deny code is invoked. The "getout" variable then passes the needed information to the Deny from directive wherever it is in the following code.

Jim

dikkejoris

4:20 pm on Apr 13, 2004 (gmt 0)

10+ Year Member



First of all, very much thank you!

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]

stevenmusumeche

5:00 pm on Apr 13, 2004 (gmt 0)

10+ Year Member



You are using fopen with the "w" attribute which means that you are opening the file for writing only. You should use the "a" attribute which opens the file for appending. You can find more information about the fopen command by reading the php manual located here:

[php.net...]

jdMorgan

5:07 pm on Apr 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Actually, you need to prepend the new record, not append it. So the procedure is:

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